From ecb67809da494f10b884f73494a67e7efddd9cc9 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Tue, 4 Jan 2022 16:25:17 +0100 Subject: [PATCH] #49 Extends sanitize db command * extends sanitize db command to remove unrelated parcels and district from the database * fixes bug where single parcel wfs match would lead to unexpected behaviour * adds admin interface for parcels and districts * adds updating of parcels in case of SimpleGeomForm saving --- konova/admin.py | 23 ++++++++++++++- konova/forms.py | 1 + konova/management/commands/sanitize_db.py | 34 ++++++++++++++++++++++- konova/utils/wfs/spatial.py | 8 +++++- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/konova/admin.py b/konova/admin.py index 213e0412..23e29a65 100644 --- a/konova/admin.py +++ b/konova/admin.py @@ -7,7 +7,7 @@ Created on: 22.07.21 """ from django.contrib import admin -from konova.models import Geometry, Deadline, GeometryConflict +from konova.models import Geometry, Deadline, GeometryConflict, Parcel, District class GeometryAdmin(admin.ModelAdmin): @@ -17,6 +17,25 @@ class GeometryAdmin(admin.ModelAdmin): ] +class ParcelAdmin(admin.ModelAdmin): + list_display = [ + "id", + "flr", + "flrstck_nnr", + "flrstck_zhlr", + "updated_on", + ] + + +class DistrictAdmin(admin.ModelAdmin): + list_display = [ + "id", + "gmrkng", + "gmnd", + "krs", + ] + + class GeometryConflictAdmin(admin.ModelAdmin): list_display = [ "conflicting_geometry", @@ -52,5 +71,7 @@ class BaseObjectAdmin(admin.ModelAdmin): admin.site.register(Geometry, GeometryAdmin) +admin.site.register(Parcel, ParcelAdmin) +admin.site.register(District, DistrictAdmin) admin.site.register(GeometryConflict, GeometryConflictAdmin) admin.site.register(Deadline, DeadlineAdmin) diff --git a/konova/forms.py b/konova/forms.py index 43c83498..e9d9c8d7 100644 --- a/konova/forms.py +++ b/konova/forms.py @@ -287,6 +287,7 @@ class SimpleGeomForm(BaseForm): geometry = self.instance.geometry geometry.geom = self.cleaned_data.get("geom", MultiPolygon(srid=DEFAULT_SRID)) geometry.modified = action + geometry.update_parcels() geometry.save() except LookupError: # No geometry or linked instance holding a geometry exist --> create a new one! diff --git a/konova/management/commands/sanitize_db.py b/konova/management/commands/sanitize_db.py index c5526517..0eecdc03 100644 --- a/konova/management/commands/sanitize_db.py +++ b/konova/management/commands/sanitize_db.py @@ -9,7 +9,7 @@ from compensation.models import CompensationState, Compensation, EcoAccount, Com from ema.models import Ema from intervention.models import Intervention from konova.management.commands.setup import BaseKonovaCommand -from konova.models import Deadline, Geometry +from konova.models import Deadline, Geometry, Parcel, District from user.models import UserActionLogEntry @@ -23,6 +23,7 @@ class Command(BaseKonovaCommand): self.sanitize_actions() self.sanitize_deadlines() self.sanitize_geometries() + self.sanitize_parcels_and_districts() except KeyboardInterrupt: self._break_line() exit(-1) @@ -266,3 +267,34 @@ class Command(BaseKonovaCommand): self._write_success("No unused states found.") self._break_line() + def sanitize_parcels_and_districts(self): + """ Removes unattached parcels and districts + + Returns: + + """ + self._write_warning("=== Sanitize parcels and districts ===") + unrelated_parcels = Parcel.objects.filter( + geometries=None, + ) + num_unrelated_parcels = unrelated_parcels.count() + if num_unrelated_parcels > 0: + self._write_error(f"Found {num_unrelated_parcels} unrelated parcel entries. Delete now...") + unrelated_parcels.delete() + self._write_success("Unrelated parcels deleted.") + else: + self._write_success("No unrelated parcels found.") + + unrelated_districts = District.objects.filter( + parcels=None, + ) + num_unrelated_districts = unrelated_districts.count() + if num_unrelated_districts > 0: + self._write_error(f"Found {num_unrelated_districts} unrelated district entries. Delete now...") + unrelated_districts.delete() + self._write_success("Unrelated districts deleted.") + else: + self._write_success("No unrelated districts found.") + + self._break_line() + diff --git a/konova/utils/wfs/spatial.py b/konova/utils/wfs/spatial.py index 62f71f65..ed64f3cf 100644 --- a/konova/utils/wfs/spatial.py +++ b/konova/utils/wfs/spatial.py @@ -158,10 +158,16 @@ class ParcelWFSFetcher(AbstractWFSFetcher): "wfs:FeatureCollection", {}, ) - features += collection.get( + + members = collection.get( "wfs:member", [], ) + if len(members) > 1: + features += members + else: + features += [members] + if collection.get("@next", None) is not None: start_index += self.count else: