master #441
@ -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
|
||||||
|
@ -8,27 +8,46 @@ 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 = "Checks the database' sanity and removes unused entries"
|
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"
|
||||||
|
)
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
try:
|
try:
|
||||||
self.update_all_parcels()
|
self.recalculate_parcels(options)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
self._break_line()
|
self._break_line()
|
||||||
exit(-1)
|
exit(-1)
|
||||||
|
|
||||||
def update_all_parcels(self):
|
def recalculate_parcels(self, options: dict):
|
||||||
num_parcels_before = Parcel.objects.count()
|
force_all = options.get("force_all", False)
|
||||||
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.all().exclude(
|
geometries = geometry_objects.exclude(
|
||||||
geom=None
|
geom=None
|
||||||
).annotate(area=Area("geom")).order_by(
|
).annotate(area=Area("geom")).order_by(
|
||||||
'area'
|
'area'
|
||||||
@ -43,12 +62,5 @@ 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()
|
Loading…
Reference in New Issue
Block a user