diff options
-rw-r--r-- | archaeological_context_records/models.py | 15 | ||||
-rw-r--r-- | archaeological_operations/models.py | 83 |
2 files changed, 64 insertions, 34 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index 97a629d05..55b101acc 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -23,10 +23,9 @@ import uuid from django.apps import apps from django.conf import settings from django.contrib.gis.db import models -from django.contrib.gis.geos import Point from django.contrib.postgres.indexes import GinIndex from django.contrib.sites.models import Site -from django.db import connection +from django.db import connection, IntegrityError, transaction from django.db.models import Q from django.db.models.signals import post_delete, post_save, m2m_changed from django.urls import reverse @@ -458,11 +457,15 @@ class GeographicSubTownItem(GeoItem): return if not q_geodata_current_town.filter(source_id=town.id).count(): - self.geodata.add(town.main_geodata) + try: + # multiple save, post treatments can cause synchronous add + with transaction.atomic(): + self.geodata.add(town.main_geodata) + except IntegrityError: + pass if save: - self.skip_history_when_saving = True - self._no_move = True - self.save() + post_save_geo(self.__class__, instance=self, created=False, + update_fields=False, raw=False, using="default") else: return True diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index 78ade77f8..6cbac457e 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -188,26 +188,38 @@ class GeographicTownItem(GeoItem): 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 + try: + with transaction.atomic(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + changed = True + except IntegrityError: + pass 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 + try: + with transaction.atomic(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + changed = True + except IntegrityError: + pass current_town_geo = None if q_towns_nb == 1: current_town_geo = q_towns.all()[0] for geo in q_geodata_town.exclude(source_id=current_town_geo.pk).all(): - self.geodata.remove(geo) - if self.main_geodata == geo: - self.main_geodata = None - changed = True + try: + with transaction.atomic(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + changed = True + except IntegrityError: + pass if not q_geodata_town.filter(source_id=current_town_geo.pk).count(): self.geodata.add(current_town_geo.main_geodata) changed = True @@ -220,35 +232,50 @@ class GeographicTownItem(GeoItem): 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 + try: + with transaction.atomic(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + except IntegrityError: + pass + try: + with transaction.atomic(): + self.geodata.add(current_geo_area) + changed = True + except IntegrityError: + pass if current_town_geo: q_extra_geo_town = q_geodata_town.exclude(source_id=current_town_geo.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 + try: + with transaction.atomic(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + changed = True + except IntegrityError: + pass 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 + try: + with transaction.atomic(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + changed = True + except IntegrityError: + pass if changed and save: - self.skip_history_when_saving = True - self._no_move = True - self.save() + post_save_geo(self.__class__, instance=self, created=False, + update_fields=False, raw=False, using="default") class ArchaeologicalSite( |