summaryrefslogtreecommitdiff
path: root/archaeological_operations
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2022-02-17 13:03:40 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2022-12-12 12:21:00 +0100
commit1205622c9acfef772a73c15352a4fa812ca7e358 (patch)
tree00b6f9ca825f89ba76900c3095e9191410c5451f /archaeological_operations
parentb93a677cc30d92a0efa0ca8be52614ed1eb67329 (diff)
downloadIshtar-1205622c9acfef772a73c15352a4fa812ca7e358.tar.bz2
Ishtar-1205622c9acfef772a73c15352a4fa812ca7e358.zip
Geodata redesign: operation migrations (1/2) - operation geo post save - town area creation
Diffstat (limited to 'archaeological_operations')
-rw-r--r--archaeological_operations/models.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py
index 9d0018f28..8fa72b423 100644
--- a/archaeological_operations/models.py
+++ b/archaeological_operations/models.py
@@ -36,6 +36,7 @@ from django.urls import reverse
from ishtar_common.utils import ugettext_lazy as _, pgettext_lazy
from ishtar_common.models import (
+ Area,
BaseHistorizedItem,
Dashboard,
DashboardFormItem,
@@ -2066,6 +2067,82 @@ class Operation(
res["mode"] = " ; ".join([str(m) for m in mode(finds)])
return res
+ def post_save_geo(self, save=True):
+ # manage geodata towns
+ if getattr(self, "_post_saved_geo", True):
+ # prevent infinite loop - should not happen, but...
+ return
+ self._post_saved_geo = True
+ q_towns = self.towns.filter(main_geodata__multi_polygon__isnull=False)
+ q_towns_nb = q_towns.count()
+ q_geodata_town = self.geodata.filter(
+ source_content_type__model="town",
+ source_content_type__app_label="ishtar_common")
+ q_geodata_area = self.geodata.filter(
+ source_content_type__model="area",
+ source_content_type__app_label="ishtar_common")
+ changed = False
+ if q_towns_nb != 1:
+ # no simple town - clean
+ for geo in q_geodata_town.all():
+ self.geodata.remove(geo)
+ if self.main_geodata == geo:
+ self.main_geodata = None
+ changed = True
+ if q_towns_nb < 2:
+ # no area - clean
+ for geo in q_geodata_area.all():
+ self.geodata.remove(geo)
+ if self.main_geodata == geo:
+ self.main_geodata = None
+ changed = True
+
+ current_geo_town = None
+ if q_towns_nb == 1:
+ current_geo_town = q_towns.all()[0]
+ if not q_geodata_town.filter(pk=current_geo_town.pk).count():
+ for geo in q_geodata_town.all():
+ self.geodata.remove(geo)
+ if self.main_geodata == geo:
+ self.main_geodata = None
+ self.geodata.add(current_geo_town)
+ changed = True
+
+ current_geo_area = None
+ if q_towns_nb > 1:
+ current_geo_area = Area.get_or_create_by_towns(q_towns, get_geo=True)
+ if current_geo_area and not q_geodata_area.filter(pk=current_geo_area.pk).count():
+ for geo in q_geodata_area.all():
+ self.geodata.remove(geo)
+ if self.main_geodata == geo:
+ self.main_geodata = None
+ self.geodata.add(current_geo_area)
+ changed = True
+
+ if current_geo_town:
+ q_extra_geo_town = q_geodata_town.exclude(pk=current_geo_town.pk)
+ if q_extra_geo_town.count():
+ # should not occur but bad migrations, bad imports...
+ for geo in q_extra_geo_town.all():
+ self.geodata.remove(geo)
+ if self.main_geodata == geo:
+ self.main_geodata = None
+ changed = True
+ if current_geo_area:
+ q_extra_geo_area = q_geodata_area.exclude(pk=current_geo_area.pk)
+ if q_extra_geo_area.count():
+ # should not occur but bad migrations, bad imports...
+ for geo in q_extra_geo_area.all():
+ self.geodata.remove(geo)
+ if self.main_geodata == geo:
+ self.main_geodata = None
+ changed = True
+
+ if changed and save:
+ self.skip_history_when_saving = True
+ self._no_move = True
+ self.save()
+
def save(self, *args, **kwargs):
# put a default year if start_date is defined
if self.start_date and not self.year: