diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-02-24 23:30:23 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-12-12 12:21:00 +0100 |
commit | 4286395e83bc27f853f0919c988a716dc2b6a345 (patch) | |
tree | 26f0daf8d47183130713be5dfc52b8e788e21dc4 /archaeological_context_records | |
parent | a14d0067a933ff3898773119fb85146545530dc3 (diff) | |
download | Ishtar-4286395e83bc27f853f0919c988a716dc2b6a345.tar.bz2 Ishtar-4286395e83bc27f853f0919c988a716dc2b6a345.zip |
Geodata redesign: context record, base find migration
Diffstat (limited to 'archaeological_context_records')
-rw-r--r-- | archaeological_context_records/models.py | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index 29e8793dc..e9bd70aa8 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -397,12 +397,61 @@ class CRBulkView(object): """ +class GeographicSubTownItem(GeoItem): + class Meta: + abstract = True + + def _get_geo_town(self): + raise NotImplementedError() + + 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 + + q_geodata_current_town = self.geodata.filter( + source_content_type__model="town", + source_content_type__app_label="ishtar_common", + ) + town = self._get_geo_town() + has_geo_town = ( + town and town.main_geodata and town.main_geodata.multi_polygon + ) + if has_geo_town: + bad_towns = q_geodata_current_town + else: + bad_towns = q_geodata_current_town.exclude(source_id=town.id) + + modified = False + for bad_town in bad_towns.all(): + self.geodata.remove(bad_town) + if self.main_geodata == bad_town: + self.main_geodata = None + modified = True + + if not has_geo_town: + if modified and save: + self.skip_history_when_saving = True + self._no_move = True + self.save() + return + + if not q_geodata_current_town.filter(source_id=town.id).count(): + self.geodata.add(town.main_geodata) + if save: + self.skip_history_when_saving = True + self._no_move = True + self.save() + + class ContextRecord( BulkUpdatedItem, DocumentItem, BaseHistorizedItem, CompleteIdentifierItem, - GeoItem, + GeographicSubTownItem, OwnPerms, ValueGetter, MainItem, @@ -510,7 +559,7 @@ class ContextRecord( ), "operation__common_name": SearchAltName( pgettext_lazy("key for text search", "operation-name"), - "operation__common_name__iexact" + "operation__common_name__iexact", ), "operation__code_patriarche": SearchAltName( pgettext_lazy("key for text search", "patriarche"), @@ -810,6 +859,11 @@ class ContextRecord( if self.surface: return self.surface / 10000.0 + def _get_geo_town(self): + if self.parcel: + return self.parcel.town + return self.town + def public_representation(self): dct = super(ContextRecord, self).public_representation() dct.update( |