Compare commits

..

No commits in common. "e6eb9abd36b54f025cc51bc6d3ade32a7a883ca4" and "45441cc44dbb11930606d77540ac1e59473d9cbc" have entirely different histories.

2 changed files with 14 additions and 26 deletions

View File

@ -6,11 +6,11 @@ Created on: 26.10.22
""" """
import zipfile import zipfile
from datetime import datetime
from io import BytesIO from io import BytesIO
from django.core.mail import EmailMessage from django.core.mail import EmailMessage
from django.utils import timezone from django.utils import timezone
from django.utils.datetime_safe import datetime
from analysis.utils.excel.excel import TempExcelFile from analysis.utils.excel.excel import TempExcelFile
from analysis.utils.report import TimespanReport from analysis.utils.report import TimespanReport

View File

@ -8,46 +8,27 @@ Created on: 04.01.22
import datetime import datetime
from django.contrib.gis.db.models.functions import Area from django.contrib.gis.db.models.functions import Area
from django.utils.timezone import now
from konova.management.commands.setup import BaseKonovaCommand from konova.management.commands.setup import BaseKonovaCommand
from konova.models import Geometry, Parcel, District from konova.models import Geometry, Parcel, District
class Command(BaseKonovaCommand): class Command(BaseKonovaCommand):
help = "Recalculates parcels for entries with geometry but missing parcel information" help = "Checks the database' sanity and removes unused entries"
def add_arguments(self, parser):
parser.add_argument(
"--force-all",
action="store_true",
default=False,
help="If Attribute set, all entries parcels will be recalculated"
)
def handle(self, *args, **options): def handle(self, *args, **options):
try: try:
self.recalculate_parcels(options) self.update_all_parcels()
except KeyboardInterrupt: except KeyboardInterrupt:
self._break_line() self._break_line()
exit(-1) exit(-1)
def recalculate_parcels(self, options: dict): def update_all_parcels(self):
force_all = options.get("force_all", False) num_parcels_before = Parcel.objects.count()
num_districts_before = District.objects.count()
if force_all:
geometry_objects = Geometry.objects.all()
else:
_today = now().date()
_date_threshold = _today - datetime.timedelta(days=1)
geometry_objects = Geometry.objects.filter(
parcel_update_start__date__lte=_date_threshold,
parcel_update_end__isnull=True
)
self._write_warning("=== Update parcels and districts ===") self._write_warning("=== Update parcels and districts ===")
# Order geometries by size to process smaller once at first # Order geometries by size to process smaller once at first
geometries = geometry_objects.exclude( geometries = Geometry.objects.all().exclude(
geom=None geom=None
).annotate(area=Area("geom")).order_by( ).annotate(area=Area("geom")).order_by(
'area' 'area'
@ -62,5 +43,12 @@ class Command(BaseKonovaCommand):
i += 1 i += 1
self._write_warning(f"--- {i}/{num_geoms} processed") self._write_warning(f"--- {i}/{num_geoms} processed")
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._write_success("Updating parcels done!")
self._break_line() self._break_line()