diff options
Diffstat (limited to 'archaeological_operations/models.py')
| -rw-r--r-- | archaeological_operations/models.py | 187 | 
1 files changed, 102 insertions, 85 deletions
| diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index 40ee27fba..263aa5f55 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -159,11 +159,97 @@ post_save.connect(post_save_cache, sender=RecordQualityType)  post_delete.connect(post_save_cache, sender=RecordQualityType) +class GeographicTownItem(GeoItem): +    class Meta: +        abstract = True + +    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_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_town_geo = None +        if q_towns_nb == 1: +            current_town_geo = q_towns.all()[0] +            if not q_geodata_town.filter(pk=current_town_geo.pk).count(): +                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 +                self.geodata.add(current_town_geo.main_geodata) +                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_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 +        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() + +  class ArchaeologicalSite(      DocumentItem,      BaseHistorizedItem,      CompleteIdentifierItem, -    GeoItem, +    GeographicTownItem,      OwnPerms,      ValueGetter,      MainItem, @@ -786,7 +872,7 @@ class Operation(      DocumentItem,      BaseHistorizedItem,      CompleteIdentifierItem, -    GeoItem, +    GeographicTownItem,      OwnPerms,      ValueGetter,      MainItem, @@ -1132,11 +1218,17 @@ class Operation(      ]      SERIALIZE_PROPERTIES = MainItem.SERIALIZE_PROPERTIES + ["short_label"]      SERIALIZE_DATES = ["start_date", "excavation_end_date"] -    SERIALIZE_CALL = {"closing": "serialize_closing", -                      "archaeological_sites_list": "archaeological_sites_list", -                      "documents_list": "documents_list"} -    SERIALIZE_EXCLUDE = ["search_vector", "documents", "operations", -                         "archaeological_sites"] +    SERIALIZE_CALL = { +        "closing": "serialize_closing", +        "archaeological_sites_list": "archaeological_sites_list", +        "documents_list": "documents_list", +    } +    SERIALIZE_EXCLUDE = [ +        "search_vector", +        "documents", +        "operations", +        "archaeological_sites", +    ]      SERIALIZE_STRING = ["scientist", "in_charge", "cira_rapporteur"]      # fields definition @@ -1479,8 +1571,9 @@ class Operation(          return dct      def archaeological_sites_list(self) -> list: -        return self.get_associated_main_item_list("archaeological_sites", -                                                  ArchaeologicalSite) +        return self.get_associated_main_item_list( +            "archaeological_sites", ArchaeologicalSite +        )      @classmethod      def _get_department_code(cls, value): @@ -2067,82 +2160,6 @@ 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_save_geo_ok", False): -            # prevent infinite loop - should not happen, but... -            return -        self._post_save_geo_ok = 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_town_geo = None -        if q_towns_nb == 1: -            current_town_geo = q_towns.all()[0] -            if not q_geodata_town.filter(pk=current_town_geo.pk).count(): -                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 -                self.geodata.add(current_town_geo.main_geodata) -                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(source_id=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_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 -        if current_geo_area: -            q_extra_geo_area = q_geodata_area.exclude(source_id=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: | 
