diff --git a/konova/management/commands/update_all_parcels.py b/konova/management/commands/update_all_parcels.py new file mode 100644 index 00000000..9d96ebae --- /dev/null +++ b/konova/management/commands/update_all_parcels.py @@ -0,0 +1,41 @@ +""" +Author: Michel Peltriaux +Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany +Contact: michel.peltriaux@sgdnord.rlp.de +Created on: 04.01.22 + +""" +from konova.management.commands.setup import BaseKonovaCommand +from konova.models import Geometry, Parcel, District + + +class Command(BaseKonovaCommand): + help = "Checks the database' sanity and removes unused entries" + + def handle(self, *args, **options): + try: + self.update_all_parcels() + except KeyboardInterrupt: + self._break_line() + exit(-1) + + def update_all_parcels(self): + num_parcels_before = Parcel.objects.count() + num_districts_before = District.objects.count() + self._write_warning("=== Update parcels and districts ===") + geometries = Geometry.objects.all().exclude( + geom=None + ) + self._write_warning(f"Process parcels for {geometries.count()} geometry entries now ...") + for geometry in geometries: + geometry.update_parcels() + + num_parcels_after = Parcel.objects.count() + num_districts_after = District.objects.count() + if num_parcels_after != num_parcels_before: + self._write_error(f"Parcels have changed: {num_parcels_before} to {num_parcels_after} entries. You should run the sanitize command.") + if num_districts_after != num_districts_before: + self._write_error(f"Districts have changed: {num_districts_before} to {num_districts_after} entries. You should run the sanitize command.") + + self._write_success("Updating parcels done!") + self._break_line()