summaryrefslogtreecommitdiff
path: root/archaeological_context_records/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'archaeological_context_records/models.py')
-rw-r--r--archaeological_context_records/models.py58
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(