diff options
Diffstat (limited to 'archaeological_operations/models.py')
-rw-r--r-- | archaeological_operations/models.py | 77 |
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: |