diff options
Diffstat (limited to 'archaeological_warehouse/models.py')
-rw-r--r-- | archaeological_warehouse/models.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index 8494b5bdc..996925d0a 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -52,6 +52,7 @@ from ishtar_common.models_common import ( QuickAction, MainItem, Merge, + GeoVectorData, ) from ishtar_common.model_merging import merge_model_objects from ishtar_common.utils import ( @@ -411,6 +412,48 @@ class Warehouse( def __str__(self): return self.name + def post_save_geo(self, save=True): + # manage geodata towns + if getattr(self, "_post_save_geo_ok", False): + # prevent infinite loop - should not happen, but... + return + self._post_save_geo_ok = True + if not self.precise_town: + q_geotown = GeoVectorData.objects.filter(pk=None) + else: + q_geotown = GeoVectorData.objects.filter( + source_content_type__model="town", + source_content_type__app_label="ishtar_common", + source_id=self.precise_town.pk, + multi_polygon__isnull=False) + q_geodata_town = self.geodata.filter( + source_content_type__model="town", + source_content_type__app_label="ishtar_common", + ) + changed = False + if not q_geotown.count(): + # 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 + else: + current_geo_town = q_geotown.all()[0] + for geo in q_geodata_town.exclude(pk=current_geo_town.pk).all(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + changed = True + if not q_geodata_town.filter(pk=current_geo_town.pk).count(): + self.geodata.add(current_geo_town) + changed = True + + if changed and save: + self.skip_history_when_saving = True + self._no_move = True + self.save() + def get_container_type_by_place(self, place: int): """ Container type by place based on the default organisation of the @@ -1131,6 +1174,49 @@ class Container( def __str__(self): return self.cached_label or "" + def post_save_geo(self, save=True): + # manage geodata towns + if getattr(self, "_post_save_geo_ok", False): + # prevent infinite loop - should not happen, but... + return + self._post_save_geo_ok = True + + if not self.location.precise_town: + town_id = None + q_geotown = GeoVectorData.objects.filter(pk=None) + else: + town_id = self.location.precise_town.pk + q_geotown = GeoVectorData.objects.filter( + source_content_type__model="town", + source_content_type__app_label="ishtar_common", + source_id=self.location.precise_town.pk, + multi_polygon__isnull=False) + q_geodata_town = self.geodata.filter( + source_content_type__model="town", + source_content_type__app_label="ishtar_common", + ) + changed = False + if not q_geotown.count(): + for geo in q_geodata_town.all(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + changed = True + else: + for geo in q_geodata_town.exclude(source_id=town_id).all(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + changed = True + if not q_geodata_town.filter(source_id=town_id).count(): + self.geodata.add(q_geotown.all()[0]) + changed = True + + if changed and save: + self.skip_history_when_saving = True + self._no_move = True + self.save() + @property def start_division_number(self): depth = 1 |