mpeltriaux
b24e461e06
* fixes bug where None-geometry entry (instead of empty geometry) would not be expected on parcel fetching
111 lines
3.7 KiB
Python
111 lines
3.7 KiB
Python
"""
|
|
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.gis.geos import MultiPolygon
|
|
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
|
|
from konova.sub_settings.lanis_settings import DEFAULT_SRID_RLP
|
|
|
|
|
|
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 or MultiPolygon(srid=DEFAULT_SRID_RLP)
|
|
|
|
geometry_exists = not geos_geom.empty
|
|
parcels_are_currently_calculated = geometry_exists and geos_geom.area > 0 and len(parcels) == 0
|
|
parcels_available = len(parcels) > 0
|
|
|
|
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 not geometry_exists:
|
|
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)
|