Class based views

* refactors method based views for parcel fetching, home and logout to class based
This commit is contained in:
mpeltriaux 2023-08-15 11:29:38 +02:00
parent 60b6968436
commit 345b266422
5 changed files with 156 additions and 145 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
# Project exclude paths # Project exclude paths
/venv/ /venv/
/.idea/ /.idea/
/.coverage
/htmlcov/

View File

@ -19,17 +19,17 @@ from django.urls import path, include
from konova.settings import SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY, DEBUG from konova.settings import SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY, DEBUG
from konova.sso.sso import KonovaSSOClient from konova.sso.sso import KonovaSSOClient
from konova.views.logout import logout_view from konova.views.logout import LogoutView
from konova.views.geometry import get_geom_parcels, get_geom_parcels_content from konova.views.geometry import GeomParcelsView, GeomParcelsContentView
from konova.views.home import home_view from konova.views.home import HomeView
from konova.views.map_proxy import ClientProxyParcelSearch, ClientProxyParcelWFS from konova.views.map_proxy import ClientProxyParcelSearch, ClientProxyParcelWFS
sso_client = KonovaSSOClient(SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY) sso_client = KonovaSSOClient(SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY)
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('login/', include(sso_client.get_urls())), path('login/', include(sso_client.get_urls())),
path('logout/', logout_view, name="logout"), path('logout/', LogoutView.as_view(), name="logout"),
path('', home_view, name="home"), path('', HomeView.as_view(), name="home"),
path('intervention/', include("intervention.urls")), path('intervention/', include("intervention.urls")),
path('compensation/', include("compensation.urls")), path('compensation/', include("compensation.urls")),
path('ema/', include("ema.urls")), path('ema/', include("ema.urls")),
@ -38,8 +38,8 @@ urlpatterns = [
path('cl/', include("codelist.urls")), path('cl/', include("codelist.urls")),
path('analysis/', include("analysis.urls")), path('analysis/', include("analysis.urls")),
path('api/', include("api.urls")), path('api/', include("api.urls")),
path('geom/<id>/parcels/', get_geom_parcels, name="geometry-parcels"), path('geom/<id>/parcels/', GeomParcelsView.as_view(), name="geometry-parcels"),
path('geom/<id>/parcels/<int:page>', get_geom_parcels_content, name="geometry-parcels-content"), path('geom/<id>/parcels/<int:page>', GeomParcelsContentView.as_view(), name="geometry-parcels-content"),
path('client/proxy', ClientProxyParcelSearch.as_view(), name="client-proxy-search"), path('client/proxy', ClientProxyParcelSearch.as_view(), name="client-proxy-search"),
path('client/proxy/wfs', ClientProxyParcelWFS.as_view(), name="client-proxy-wfs"), path('client/proxy/wfs', ClientProxyParcelWFS.as_view(), name="client-proxy-wfs"),
] ]

View File

@ -5,104 +5,110 @@ Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 19.08.22 Created on: 19.08.22
""" """
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.gis.geos import MultiPolygon from django.contrib.gis.geos import MultiPolygon
from django.http import HttpResponse, HttpRequest from django.http import HttpResponse, HttpRequest
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.views import View
from konova.models import Geometry, Municipal from konova.models import Geometry
from konova.sub_settings.lanis_settings import DEFAULT_SRID_RLP from konova.sub_settings.lanis_settings import DEFAULT_SRID_RLP
def get_geom_parcels(request: HttpRequest, id: str): class GeomParcelsView(LoginRequiredMixin, View):
""" Getter for HTMX
Returns all parcels of the requested geometry rendered into a simple HTML table def get(self, request: HttpRequest, id: str):
""" Getter for HTMX
Args: Returns all parcels of the requested geometry rendered into a simple HTML table
request (HttpRequest): The incoming request
id (str): The geometry's id
Returns: Args:
A rendered piece of HTML request (HttpRequest): The incoming request
""" id (str): The geometry's id
# 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 or MultiPolygon(srid=DEFAULT_SRID_RLP)
geometry_exists = not geos_geom.empty Returns:
parcels_are_currently_calculated = geometry_exists and geos_geom.area > 0 and len(parcels) == 0 A rendered piece of HTML
parcels_available = len(parcels) > 0 """
# 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 or MultiPolygon(srid=DEFAULT_SRID_RLP)
if parcels_are_currently_calculated: geometry_exists = not geos_geom.empty
# Parcels are being calculated right now. Change the status code, so polling stays active for fetching parcels_are_currently_calculated = geometry_exists and geos_geom.area > 0 and len(parcels) == 0
# resutls after the calculation parcels_available = len(parcels) > 0
status_code = 200
if parcels_available or not geometry_exists: if parcels_are_currently_calculated:
municipals = geom.get_underlying_municipals(parcels) # 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 not geometry_exists:
municipals = geom.get_underlying_municipals(parcels)
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)
class GeomParcelsContentView(LoginRequiredMixin, View):
def get(self, 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 rpp = 100
num_all_parcels = parcels.count() from_p = rpp * (page-1)
parcels = parcels[:rpp] to_p = rpp * (page)
next_page = 1 next_page = page + 1
parcels = parcels[from_p:to_p]
if len(parcels) < rpp: if len(parcels) < rpp:
next_page = None next_page = None
context = { context = {
"num_parcels": num_all_parcels,
"parcels": parcels, "parcels": parcels,
"municipals": municipals,
"geom_id": str(id), "geom_id": str(id),
"next_page": next_page, "next_page": next_page,
} }
html = render_to_string(template, context, request) html = render_to_string(template, context, request)
return HttpResponse(html, status=status_code) 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)

View File

@ -5,12 +5,13 @@ Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 19.08.22 Created on: 19.08.22
""" """
from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import Q from django.db.models import Q
from django.http import HttpRequest from django.http import HttpRequest
from django.shortcuts import render from django.shortcuts import render
from django.utils import timezone from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views import View
from compensation.models import EcoAccount, Compensation from compensation.models import EcoAccount, Compensation
from intervention.models import Intervention from intervention.models import Intervention
@ -20,59 +21,59 @@ from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
from news.models import ServerMessage from news.models import ServerMessage
@login_required class HomeView(LoginRequiredMixin, View):
@any_group_check
def home_view(request: HttpRequest):
"""
Renders the landing page
Args: @method_decorator(any_group_check)
request (HttpRequest): The used request object def get(self, request: HttpRequest):
"""
Renders the landing page
Returns: Args:
A redirect request (HttpRequest): The used request object
"""
template = "konova/home.html"
now = timezone.now()
user = request.user
user_teams = user.shared_teams
# Fetch the four newest active and published ServerMessages Returns:
msgs = ServerMessage.get_current_news()[:3] A redirect
"""
template = "konova/home.html"
user = request.user
user_teams = user.shared_teams
# First fetch all valid objects (undeleted, only newest versions) # Fetch the four newest active and published ServerMessages
interventions = Intervention.objects.filter( msgs = ServerMessage.get_current_news()[:3]
deleted=None,
)
# Then fetch only user related ones
user_interventions = interventions.filter(
Q(users__in=[user]) | Q(teams__in=user_teams)
).distinct()
# Repeat for other objects # First fetch all valid objects (undeleted, only newest versions)
comps = Compensation.objects.filter( interventions = Intervention.objects.filter(
deleted=None, deleted=None,
) )
user_comps = comps.filter( # Then fetch only user related ones
Q(intervention__users__in=[user]) | Q(intervention__teams__in=user_teams) user_interventions = interventions.filter(
).distinct() Q(users__in=[user]) | Q(teams__in=user_teams)
eco_accs = EcoAccount.objects.filter( ).distinct()
deleted=None,
)
user_ecco_accs = eco_accs.filter(
Q(users__in=[user]) | Q(teams__in=user_teams)
).distinct()
additional_context = { # Repeat for other objects
"msgs": msgs, comps = Compensation.objects.filter(
"total_intervention_count": interventions.count(), deleted=None,
"user_intervention_count": user_interventions.count(), )
"total_compensation_count": comps.count(), user_comps = comps.filter(
"user_compensation_count": user_comps.count(), Q(intervention__users__in=[user]) | Q(intervention__teams__in=user_teams)
"total_eco_count": eco_accs.count(), ).distinct()
"user_eco_count": user_ecco_accs.count(), eco_accs = EcoAccount.objects.filter(
TAB_TITLE_IDENTIFIER: _("Home"), deleted=None,
} )
context = BaseContext(request, additional_context).context user_ecco_accs = eco_accs.filter(
return render(request, template, context) Q(users__in=[user]) | Q(teams__in=user_teams)
).distinct()
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)

View File

@ -8,19 +8,21 @@ Created on: 19.08.22
from django.contrib.auth import logout from django.contrib.auth import logout
from django.http import HttpRequest from django.http import HttpRequest
from django.shortcuts import redirect from django.shortcuts import redirect
from django.views import View
from konova.sub_settings.sso_settings import SSO_SERVER_BASE from konova.sub_settings.sso_settings import SSO_SERVER_BASE
def logout_view(request: HttpRequest): class LogoutView(View):
""" def get(self, request: HttpRequest):
Logout route for ending the session manually. """
Logout route for ending the session manually.
Args: Args:
request (HttpRequest): The used request object request (HttpRequest): The used request object
Returns: Returns:
A redirect A redirect
""" """
logout(request) logout(request)
return redirect(SSO_SERVER_BASE) return redirect(SSO_SERVER_BASE)