Konova views
* splits konova/views.py into separate files in new module * view files can now be found in /konova/views/... * introduces first class based view AbstractLogView * implemented for Ema, Intervention, Compensation and EcoAccount
This commit is contained in:
parent
d168ec47ce
commit
3c416fa264
@ -16,7 +16,7 @@ from compensation.views.compensation.action import action_edit_view, action_new_
|
||||
from compensation.views.compensation.state import state_new_view, state_remove_view, state_edit_view
|
||||
from compensation.views.compensation.compensation import index_view, new_view, new_id_view, detail_view, edit_view, \
|
||||
remove_view
|
||||
from compensation.views.compensation.log import log_view
|
||||
from compensation.views.compensation.log import CompensationLogView
|
||||
|
||||
urlpatterns = [
|
||||
# Main compensation
|
||||
@ -25,7 +25,7 @@ urlpatterns = [
|
||||
path('new/<intervention_id>', new_view, name='new'),
|
||||
path('new', new_view, name='new'),
|
||||
path('<id>', detail_view, name='detail'),
|
||||
path('<id>/log', log_view, name='log'),
|
||||
path('<id>/log', CompensationLogView.as_view(), name='log'),
|
||||
path('<id>/edit', edit_view, name='edit'),
|
||||
path('<id>/remove', remove_view, name='remove'),
|
||||
|
||||
|
@ -10,7 +10,7 @@ from django.urls import path
|
||||
from compensation.autocomplete.eco_account import EcoAccountAutocomplete
|
||||
from compensation.views.eco_account.eco_account import index_view, new_view, new_id_view, edit_view, remove_view, \
|
||||
detail_view
|
||||
from compensation.views.eco_account.log import log_view
|
||||
from compensation.views.eco_account.log import EcoAccountLogView
|
||||
from compensation.views.eco_account.record import record_view
|
||||
from compensation.views.eco_account.report import report_view
|
||||
from compensation.views.eco_account.resubmission import create_resubmission_view
|
||||
@ -28,7 +28,7 @@ urlpatterns = [
|
||||
path('new/', new_view, name='new'),
|
||||
path('new/id', new_id_view, name='new-id'),
|
||||
path('<id>', detail_view, name='detail'),
|
||||
path('<id>/log', log_view, name='log'),
|
||||
path('<id>/log', EcoAccountLogView.as_view(), name='log'),
|
||||
path('<id>/record', record_view, name='record'),
|
||||
path('<id>/report', report_view, name='report'),
|
||||
path('<id>/edit', edit_view, name='edit'),
|
||||
|
@ -6,36 +6,18 @@ Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils.decorators import method_decorator
|
||||
|
||||
from compensation.models import Compensation
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
from konova.views.log import AbstractLogView
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def log_view(request: HttpRequest, id: str):
|
||||
""" Renders a log view using modal
|
||||
class CompensationLogView(AbstractLogView):
|
||||
model = Compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
template = "modal/modal_generic.html"
|
||||
body_template = "log.html"
|
||||
|
||||
context = {
|
||||
"modal_body_template": body_template,
|
||||
"log": comp.log.all(),
|
||||
"modal_title": _("Log"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
@method_decorator(login_required)
|
||||
@method_decorator(default_group_required)
|
||||
@method_decorator(shared_access_required(Compensation, "id"))
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
@ -6,37 +6,18 @@ Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils.decorators import method_decorator
|
||||
|
||||
from compensation.models import EcoAccount
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
from konova.views.log import AbstractLogView
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def log_view(request: HttpRequest, id: str):
|
||||
""" Renders a log view using modal
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The eco acount's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(EcoAccount, id=id)
|
||||
template = "modal/modal_generic.html"
|
||||
body_template = "log.html"
|
||||
|
||||
context = {
|
||||
"modal_body_template": body_template,
|
||||
"log": comp.log.all(),
|
||||
"modal_title": _("Log"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
class EcoAccountLogView(AbstractLogView):
|
||||
model = EcoAccount
|
||||
|
||||
@method_decorator(login_required)
|
||||
@method_decorator(default_group_required)
|
||||
@method_decorator(shared_access_required(EcoAccount, "id"))
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
@ -11,7 +11,7 @@ from ema.views.action import action_new_view, action_edit_view, action_remove_vi
|
||||
from ema.views.deadline import deadline_new_view, deadline_edit_view, deadline_remove_view
|
||||
from ema.views.document import document_new_view, get_document_view, remove_document_view, edit_document_view
|
||||
from ema.views.ema import index_view, new_view, new_id_view, detail_view, edit_view, remove_view
|
||||
from ema.views.log import log_view
|
||||
from ema.views.log import EmaLogView
|
||||
from ema.views.record import record_view
|
||||
from ema.views.report import report_view
|
||||
from ema.views.resubmission import create_resubmission_view
|
||||
@ -24,7 +24,7 @@ urlpatterns = [
|
||||
path("new/", new_view, name="new"),
|
||||
path("new/id", new_id_view, name="new-id"),
|
||||
path("<id>", detail_view, name="detail"),
|
||||
path('<id>/log', log_view, name='log'),
|
||||
path('<id>/log', EmaLogView.as_view(), name='log'),
|
||||
path('<id>/edit', edit_view, name='edit'),
|
||||
path('<id>/remove', remove_view, name='remove'),
|
||||
path('<id>/record', record_view, name='record'),
|
||||
|
@ -6,36 +6,18 @@ Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils.decorators import method_decorator
|
||||
|
||||
from ema.models import Ema
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import shared_access_required, conservation_office_group_required
|
||||
from konova.views.log import AbstractLogView
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def log_view(request: HttpRequest, id: str):
|
||||
""" Renders a log view using modal
|
||||
class EmaLogView(AbstractLogView):
|
||||
model = Ema
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
template = "modal/modal_generic.html"
|
||||
body_template = "log.html"
|
||||
|
||||
context = {
|
||||
"modal_body_template": body_template,
|
||||
"log": ema.log.all(),
|
||||
"modal_title": _("Log"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
@method_decorator(login_required)
|
||||
@method_decorator(conservation_office_group_required)
|
||||
@method_decorator(shared_access_required(Ema, "id"))
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
@ -13,7 +13,7 @@ from intervention.views.compensation import remove_compensation_view
|
||||
from intervention.views.deduction import new_deduction_view, edit_deduction_view, remove_deduction_view
|
||||
from intervention.views.document import new_document_view, get_document_view, remove_document_view, edit_document_view
|
||||
from intervention.views.intervention import index_view, new_view, new_id_view, detail_view, edit_view, remove_view
|
||||
from intervention.views.log import log_view
|
||||
from intervention.views.log import InterventionLogView
|
||||
from intervention.views.record import record_view
|
||||
from intervention.views.report import report_view
|
||||
from intervention.views.resubmission import create_resubmission_view
|
||||
@ -27,7 +27,7 @@ urlpatterns = [
|
||||
path('new/', new_view, name='new'),
|
||||
path('new/id', new_id_view, name='new-id'),
|
||||
path('<id>', detail_view, name='detail'),
|
||||
path('<id>/log', log_view, name='log'),
|
||||
path('<id>/log', InterventionLogView.as_view(), name='log'),
|
||||
path('<id>/edit', edit_view, name='edit'),
|
||||
path('<id>/remove', remove_view, name='remove'),
|
||||
path('<id>/share/<token>', share_view, name='share'),
|
||||
|
@ -6,36 +6,18 @@ Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils.decorators import method_decorator
|
||||
|
||||
from intervention.models import Intervention
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
from konova.views.log import AbstractLogView
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def log_view(request: HttpRequest, id: str):
|
||||
""" Renders a log view using modal
|
||||
class InterventionLogView(AbstractLogView):
|
||||
model = Intervention
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
template = "modal/modal_generic.html"
|
||||
body_template = "log.html"
|
||||
|
||||
context = {
|
||||
"modal_body_template": body_template,
|
||||
"log": intervention.log.all(),
|
||||
"modal_title": _("Log"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
@method_decorator(login_required)
|
||||
@method_decorator(default_group_required)
|
||||
@method_decorator(shared_access_required(Intervention, "id"))
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
@ -19,7 +19,10 @@ from django.urls import path, include
|
||||
|
||||
from konova.settings import SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY, DEBUG
|
||||
from konova.sso.sso import KonovaSSOClient
|
||||
from konova.views import logout_view, home_view, get_geom_parcels, get_geom_parcels_content, map_client_proxy_view
|
||||
from konova.views.logout import logout_view
|
||||
from konova.views.geometry import get_geom_parcels, get_geom_parcels_content
|
||||
from konova.views.home import home_view
|
||||
from konova.views.map_proxy import map_client_proxy_view
|
||||
|
||||
sso_client = KonovaSSOClient(SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY)
|
||||
urlpatterns = [
|
||||
@ -45,5 +48,5 @@ if DEBUG:
|
||||
path('__debug__/', include(debug_toolbar.urls)),
|
||||
]
|
||||
|
||||
handler404 = "konova.views.get_404_view"
|
||||
handler500 = "konova.views.get_500_view"
|
||||
handler404 = "konova.views.error.get_404_view"
|
||||
handler500 = "konova.views.error.get_500_view"
|
248
konova/views.py
248
konova/views.py
@ -1,248 +0,0 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 16.11.20
|
||||
|
||||
"""
|
||||
import json
|
||||
|
||||
import requests
|
||||
from django.contrib.auth import logout
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest, HttpResponse, JsonResponse
|
||||
from django.shortcuts import redirect, render, get_object_or_404
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.models import Compensation, EcoAccount
|
||||
from intervention.models import Intervention
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import any_group_check
|
||||
from konova.models import Deadline, Geometry, Municipal
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from news.models import ServerMessage
|
||||
from konova.settings import SSO_SERVER_BASE
|
||||
|
||||
|
||||
def logout_view(request: HttpRequest):
|
||||
"""
|
||||
Logout route for ending the session manually.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The used request object
|
||||
|
||||
Returns:
|
||||
A redirect
|
||||
"""
|
||||
logout(request)
|
||||
return redirect(SSO_SERVER_BASE)
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def home_view(request: HttpRequest):
|
||||
"""
|
||||
Renders the landing page
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The used request object
|
||||
|
||||
Returns:
|
||||
A redirect
|
||||
"""
|
||||
template = "konova/home.html"
|
||||
now = timezone.now()
|
||||
user = request.user
|
||||
|
||||
# Fetch the four newest active and published ServerMessages
|
||||
msgs = ServerMessage.objects.filter(
|
||||
is_active=True,
|
||||
publish_on__lte=now,
|
||||
unpublish_on__gte=now,
|
||||
).order_by(
|
||||
"-publish_on"
|
||||
)[:3]
|
||||
|
||||
# First fetch all valid objects (undeleted, only newest versions)
|
||||
interventions = Intervention.objects.filter(
|
||||
deleted=None,
|
||||
)
|
||||
# Then fetch only user related ones
|
||||
user_interventions = interventions.filter(
|
||||
users__in=[user]
|
||||
)
|
||||
|
||||
# Repeat for other objects
|
||||
comps = Compensation.objects.filter(
|
||||
deleted=None,
|
||||
)
|
||||
user_comps = comps.filter(
|
||||
intervention__users__in=[user]
|
||||
)
|
||||
eco_accs = EcoAccount.objects.filter(
|
||||
deleted=None,
|
||||
)
|
||||
user_ecco_accs = eco_accs.filter(
|
||||
users__in=[user]
|
||||
)
|
||||
|
||||
additional_context = {
|
||||
"msgs": msgs,
|
||||
"total_intervention_count": interventions.count(),
|
||||
"user_intervention_count": user_interventions.count(),
|
||||
"total_compensation_count": comps.count(),
|
||||
"user_compensation_count": user_comps.count(),
|
||||
"total_eco_count": eco_accs.count(),
|
||||
"user_eco_count": user_ecco_accs.count(),
|
||||
TAB_TITLE_IDENTIFIER: _("Home"),
|
||||
}
|
||||
context = BaseContext(request, additional_context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
def get_geom_parcels(request: HttpRequest, id: str):
|
||||
""" Getter for HTMX
|
||||
|
||||
Returns all parcels of the requested geometry rendered into a simple HTML table
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The geometry's id
|
||||
|
||||
Returns:
|
||||
A rendered piece of HTML
|
||||
"""
|
||||
# HTTP code 286 states that the HTMX should stop polling for updates
|
||||
# https://htmx.org/docs/#polling
|
||||
status_code = 286
|
||||
template = "konova/includes/parcels/parcel_table_frame.html"
|
||||
geom = get_object_or_404(Geometry, id=id)
|
||||
parcels = geom.get_underlying_parcels()
|
||||
geos_geom = geom.geom
|
||||
|
||||
parcels_are_currently_calculated = geos_geom is not None and geos_geom.area > 0 and len(parcels) == 0
|
||||
parcels_available = len(parcels) > 0
|
||||
no_geometry_given = geos_geom is None
|
||||
|
||||
if parcels_are_currently_calculated:
|
||||
# Parcels are being calculated right now. Change the status code, so polling stays active for fetching
|
||||
# resutls after the calculation
|
||||
status_code = 200
|
||||
|
||||
if parcels_available or no_geometry_given:
|
||||
parcels = parcels.order_by("-municipal", "flr", "flrstck_zhlr", "flrstck_nnr")
|
||||
municipals = parcels.order_by("municipal").distinct("municipal").values("municipal__id")
|
||||
municipals = Municipal.objects.filter(id__in=municipals)
|
||||
|
||||
rpp = 100
|
||||
num_all_parcels = parcels.count()
|
||||
parcels = parcels[:rpp]
|
||||
next_page = 1
|
||||
if len(parcels) < rpp:
|
||||
next_page = None
|
||||
|
||||
context = {
|
||||
"num_parcels": num_all_parcels,
|
||||
"parcels": parcels,
|
||||
"municipals": municipals,
|
||||
"geom_id": str(id),
|
||||
"next_page": next_page,
|
||||
}
|
||||
html = render_to_string(template, context, request)
|
||||
return HttpResponse(html, status=status_code)
|
||||
else:
|
||||
return HttpResponse(None, status=404)
|
||||
|
||||
|
||||
def get_geom_parcels_content(request: HttpRequest, id: str, page: int):
|
||||
""" Getter for infinite scroll of HTMX
|
||||
|
||||
Returns parcels of a specific page/slice of the found parcel set.
|
||||
Implementation of infinite scroll htmx example: https://htmx.org/examples/infinite-scroll/
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The geometry's id
|
||||
page (int): The requested page number
|
||||
|
||||
Returns:
|
||||
A rendered piece of HTML
|
||||
"""
|
||||
if page < 0:
|
||||
raise AssertionError("Parcel page can not be negative")
|
||||
|
||||
# HTTP code 286 states that the HTMX should stop polling for updates
|
||||
# https://htmx.org/docs/#polling
|
||||
status_code = 286
|
||||
template = "konova/includes/parcels/parcel_table_content.html"
|
||||
geom = get_object_or_404(Geometry, id=id)
|
||||
parcels = geom.get_underlying_parcels()
|
||||
|
||||
parcels = parcels.order_by("-municipal", "flr", "flrstck_zhlr", "flrstck_nnr")
|
||||
rpp = 100
|
||||
from_p = rpp * (page-1)
|
||||
to_p = rpp * (page)
|
||||
next_page = page + 1
|
||||
parcels = parcels[from_p:to_p]
|
||||
if len(parcels) < rpp:
|
||||
next_page = None
|
||||
|
||||
context = {
|
||||
"parcels": parcels,
|
||||
"geom_id": str(id),
|
||||
"next_page": next_page,
|
||||
}
|
||||
html = render_to_string(template, context, request)
|
||||
return HttpResponse(html, status=status_code)
|
||||
|
||||
|
||||
def get_404_view(request: HttpRequest, exception=None):
|
||||
""" Returns a 404 handling view
|
||||
|
||||
Args:
|
||||
request ():
|
||||
exception ():
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
context = BaseContext.context
|
||||
return render(request, "404.html", context, status=404)
|
||||
|
||||
|
||||
def get_500_view(request: HttpRequest):
|
||||
""" Returns a 404 handling view
|
||||
|
||||
Args:
|
||||
request ():
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
context = BaseContext.context
|
||||
return render(request, "500.html", context, status=500)
|
||||
|
||||
|
||||
@login_required
|
||||
def map_client_proxy_view(request: HttpRequest):
|
||||
""" Provides proxy functionality for NETGIS map client.
|
||||
|
||||
Used for fetching content of a provided url
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
url = request.META.get("QUERY_STRING")
|
||||
response = requests.get(url)
|
||||
body = json.loads(response.content)
|
||||
if response.status_code != 200:
|
||||
return JsonResponse({
|
||||
"status_code": response.status_code,
|
||||
"content": body,
|
||||
})
|
||||
return JsonResponse(body)
|
7
konova/views/__init__.py
Normal file
7
konova/views/__init__.py
Normal file
@ -0,0 +1,7 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
38
konova/views/error.py
Normal file
38
konova/views/error.py
Normal file
@ -0,0 +1,38 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 16.11.20
|
||||
|
||||
"""
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import render
|
||||
|
||||
from konova.contexts import BaseContext
|
||||
|
||||
|
||||
def get_404_view(request: HttpRequest, exception=None):
|
||||
""" Returns a 404 handling view
|
||||
|
||||
Args:
|
||||
request ():
|
||||
exception ():
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
context = BaseContext.context
|
||||
return render(request, "404.html", context, status=404)
|
||||
|
||||
|
||||
def get_500_view(request: HttpRequest):
|
||||
""" Returns a 404 handling view
|
||||
|
||||
Args:
|
||||
request ():
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
context = BaseContext.context
|
||||
return render(request, "500.html", context, status=500)
|
108
konova/views/geometry.py
Normal file
108
konova/views/geometry.py
Normal file
@ -0,0 +1,108 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.http import HttpResponse, HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.template.loader import render_to_string
|
||||
|
||||
from konova.models import Geometry, Municipal
|
||||
|
||||
|
||||
def get_geom_parcels(request: HttpRequest, id: str):
|
||||
""" Getter for HTMX
|
||||
|
||||
Returns all parcels of the requested geometry rendered into a simple HTML table
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The geometry's id
|
||||
|
||||
Returns:
|
||||
A rendered piece of HTML
|
||||
"""
|
||||
# HTTP code 286 states that the HTMX should stop polling for updates
|
||||
# https://htmx.org/docs/#polling
|
||||
status_code = 286
|
||||
template = "konova/includes/parcels/parcel_table_frame.html"
|
||||
geom = get_object_or_404(Geometry, id=id)
|
||||
parcels = geom.get_underlying_parcels()
|
||||
geos_geom = geom.geom
|
||||
|
||||
parcels_are_currently_calculated = geos_geom is not None and geos_geom.area > 0 and len(parcels) == 0
|
||||
parcels_available = len(parcels) > 0
|
||||
no_geometry_given = geos_geom is None
|
||||
|
||||
if parcels_are_currently_calculated:
|
||||
# Parcels are being calculated right now. Change the status code, so polling stays active for fetching
|
||||
# resutls after the calculation
|
||||
status_code = 200
|
||||
|
||||
if parcels_available or no_geometry_given:
|
||||
parcels = parcels.order_by("-municipal", "flr", "flrstck_zhlr", "flrstck_nnr")
|
||||
municipals = parcels.order_by("municipal").distinct("municipal").values("municipal__id")
|
||||
municipals = Municipal.objects.filter(id__in=municipals)
|
||||
|
||||
rpp = 100
|
||||
num_all_parcels = parcels.count()
|
||||
parcels = parcels[:rpp]
|
||||
next_page = 1
|
||||
if len(parcels) < rpp:
|
||||
next_page = None
|
||||
|
||||
context = {
|
||||
"num_parcels": num_all_parcels,
|
||||
"parcels": parcels,
|
||||
"municipals": municipals,
|
||||
"geom_id": str(id),
|
||||
"next_page": next_page,
|
||||
}
|
||||
html = render_to_string(template, context, request)
|
||||
return HttpResponse(html, status=status_code)
|
||||
else:
|
||||
return HttpResponse(None, status=404)
|
||||
|
||||
|
||||
def get_geom_parcels_content(request: HttpRequest, id: str, page: int):
|
||||
""" Getter for infinite scroll of HTMX
|
||||
|
||||
Returns parcels of a specific page/slice of the found parcel set.
|
||||
Implementation of infinite scroll htmx example: https://htmx.org/examples/infinite-scroll/
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The geometry's id
|
||||
page (int): The requested page number
|
||||
|
||||
Returns:
|
||||
A rendered piece of HTML
|
||||
"""
|
||||
if page < 0:
|
||||
raise AssertionError("Parcel page can not be negative")
|
||||
|
||||
# HTTP code 286 states that the HTMX should stop polling for updates
|
||||
# https://htmx.org/docs/#polling
|
||||
status_code = 286
|
||||
template = "konova/includes/parcels/parcel_table_content.html"
|
||||
geom = get_object_or_404(Geometry, id=id)
|
||||
parcels = geom.get_underlying_parcels()
|
||||
|
||||
parcels = parcels.order_by("-municipal", "flr", "flrstck_zhlr", "flrstck_nnr")
|
||||
rpp = 100
|
||||
from_p = rpp * (page-1)
|
||||
to_p = rpp * (page)
|
||||
next_page = page + 1
|
||||
parcels = parcels[from_p:to_p]
|
||||
if len(parcels) < rpp:
|
||||
next_page = None
|
||||
|
||||
context = {
|
||||
"parcels": parcels,
|
||||
"geom_id": str(id),
|
||||
"next_page": next_page,
|
||||
}
|
||||
html = render_to_string(template, context, request)
|
||||
return HttpResponse(html, status=status_code)
|
82
konova/views/home.py
Normal file
82
konova/views/home.py
Normal file
@ -0,0 +1,82 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import render
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.models import EcoAccount, Compensation
|
||||
from intervention.models import Intervention
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import any_group_check
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from news.models import ServerMessage
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def home_view(request: HttpRequest):
|
||||
"""
|
||||
Renders the landing page
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The used request object
|
||||
|
||||
Returns:
|
||||
A redirect
|
||||
"""
|
||||
template = "konova/home.html"
|
||||
now = timezone.now()
|
||||
user = request.user
|
||||
|
||||
# Fetch the four newest active and published ServerMessages
|
||||
msgs = ServerMessage.objects.filter(
|
||||
is_active=True,
|
||||
publish_on__lte=now,
|
||||
unpublish_on__gte=now,
|
||||
).order_by(
|
||||
"-publish_on"
|
||||
)[:3]
|
||||
|
||||
# First fetch all valid objects (undeleted, only newest versions)
|
||||
interventions = Intervention.objects.filter(
|
||||
deleted=None,
|
||||
)
|
||||
# Then fetch only user related ones
|
||||
user_interventions = interventions.filter(
|
||||
users__in=[user]
|
||||
)
|
||||
|
||||
# Repeat for other objects
|
||||
comps = Compensation.objects.filter(
|
||||
deleted=None,
|
||||
)
|
||||
user_comps = comps.filter(
|
||||
intervention__users__in=[user]
|
||||
)
|
||||
eco_accs = EcoAccount.objects.filter(
|
||||
deleted=None,
|
||||
)
|
||||
user_ecco_accs = eco_accs.filter(
|
||||
users__in=[user]
|
||||
)
|
||||
|
||||
additional_context = {
|
||||
"msgs": msgs,
|
||||
"total_intervention_count": interventions.count(),
|
||||
"user_intervention_count": user_interventions.count(),
|
||||
"total_compensation_count": comps.count(),
|
||||
"user_compensation_count": user_comps.count(),
|
||||
"total_eco_count": eco_accs.count(),
|
||||
"user_eco_count": user_ecco_accs.count(),
|
||||
TAB_TITLE_IDENTIFIER: _("Home"),
|
||||
}
|
||||
context = BaseContext(request, additional_context).context
|
||||
return render(request, template, context)
|
||||
|
41
konova/views/log.py
Normal file
41
konova/views/log.py
Normal file
@ -0,0 +1,41 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.views import View
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from konova.contexts import BaseContext
|
||||
|
||||
|
||||
class AbstractLogView(View):
|
||||
model = None
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def get(self, request, id: str):
|
||||
""" Renders a log view using modal
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(self.model, id=id)
|
||||
template = "modal/modal_generic.html"
|
||||
body_template = "log.html"
|
||||
|
||||
context = {
|
||||
"modal_body_template": body_template,
|
||||
"log": intervention.log.all(),
|
||||
"modal_title": _("Log"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
26
konova/views/logout.py
Normal file
26
konova/views/logout.py
Normal file
@ -0,0 +1,26 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth import logout
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import redirect
|
||||
|
||||
from konova.sub_settings.sso_settings import SSO_SERVER_BASE
|
||||
|
||||
|
||||
def logout_view(request: HttpRequest):
|
||||
"""
|
||||
Logout route for ending the session manually.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The used request object
|
||||
|
||||
Returns:
|
||||
A redirect
|
||||
"""
|
||||
logout(request)
|
||||
return redirect(SSO_SERVER_BASE)
|
35
konova/views/map_proxy.py
Normal file
35
konova/views/map_proxy.py
Normal file
@ -0,0 +1,35 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
import json
|
||||
|
||||
import requests
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import JsonResponse, HttpRequest
|
||||
|
||||
|
||||
@login_required
|
||||
def map_client_proxy_view(request: HttpRequest):
|
||||
""" Provides proxy functionality for NETGIS map client.
|
||||
|
||||
Used for fetching content of a provided url
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
url = request.META.get("QUERY_STRING")
|
||||
response = requests.get(url)
|
||||
body = json.loads(response.content)
|
||||
if response.status_code != 200:
|
||||
return JsonResponse({
|
||||
"status_code": response.status_code,
|
||||
"content": body,
|
||||
})
|
||||
return JsonResponse(body)
|
Loading…
Reference in New Issue
Block a user