Compare commits

..

No commits in common. "0ef960c73a2501f27179c62dd632bf33592e9caa" and "db53cc168ae812952437cf79097e1c40bc7d87e0" 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
from datetime import datetime
from io import BytesIO
from django.core.mail import EmailMessage
from django.utils import timezone
from django.utils.datetime_safe import datetime
from analysis.utils.excel.excel import TempExcelFile
from analysis.utils.report import TimespanReport

View File

@ -8,46 +8,27 @@ Created on: 04.01.22
import datetime
from django.contrib.gis.db.models.functions import Area
from django.utils.timezone import now
from konova.management.commands.setup import BaseKonovaCommand
from konova.models import Geometry, Parcel, District
class Command(BaseKonovaCommand):
help = "Recalculates parcels for entries with geometry but missing parcel information"
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"
)
help = "Checks the database' sanity and removes unused entries"
def handle(self, *args, **options):
try:
self.recalculate_parcels(options)
self.update_all_parcels()
except KeyboardInterrupt:
self._break_line()
exit(-1)
def recalculate_parcels(self, options: dict):
force_all = options.get("force_all", False)
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
)
def update_all_parcels(self):
num_parcels_before = Parcel.objects.count()
num_districts_before = District.objects.count()
self._write_warning("=== Update parcels and districts ===")
# Order geometries by size to process smaller once at first
geometries = geometry_objects.exclude(
geometries = Geometry.objects.all().exclude(
geom=None
).annotate(area=Area("geom")).order_by(
'area'
@ -62,5 +43,12 @@ class Command(BaseKonovaCommand):
i += 1
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._break_line()