diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2026-06-27 17:22:28 +0200 |
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2026-06-27 17:26:29 +0200 |
| commit | e457d1177f160d4713b859d69d56973d2e14bbc4 (patch) | |
| tree | 1ceacefe7da81f7e1466a2dda354210d7f472f1c | |
| parent | 90978c56b38033368bb9f12f6c8d97aa9217a1c7 (diff) | |
| download | Ishtar-e457d1177f160d4713b859d69d56973d2e14bbc4.tar.bz2 Ishtar-e457d1177f160d4713b859d69d56973d2e14bbc4.zip | |
✨ statement condition - import: post import actions set_default_from_find and set_apply_validation
| -rw-r--r-- | archaeological_finds/models_treatments.py | 94 |
1 files changed, 77 insertions, 17 deletions
diff --git a/archaeological_finds/models_treatments.py b/archaeological_finds/models_treatments.py index e9cb727ea..3dd9b4764 100644 --- a/archaeological_finds/models_treatments.py +++ b/archaeological_finds/models_treatments.py @@ -61,6 +61,7 @@ from ishtar_common.models import ( SearchVectorConfig, ValueGetter, ) +from ishtar_common.data_importer import post_importer_action from ishtar_common.models_common import ( CompleteIdentifierItem, IdentifierItem, @@ -1742,18 +1743,18 @@ class StatementCondition( last = models.BooleanField(_("Last"), default=True) find = models.ForeignKey(Find, verbose_name=_("Find"), on_delete=models.CASCADE, related_name="statement_conditions") + statement_condition_type = models.ForeignKey( + StatementConditionType, verbose_name=_("Type"), on_delete=models.PROTECT) treatment = models.ForeignKey( Treatment, verbose_name=_("Treatment"), on_delete=models.SET_NULL, null=True, blank=True, related_name="statement_conditions") - campaign_number = models.TextField(_("Campaign/observation number"), default="", - blank=True) - report_number = models.TextField(_("Report number"), default="", blank=True) - verification_officers = models.TextField(_("Verification officers"), default="", blank=True) in_charge = models.ForeignKey( Person, verbose_name=_("In charge"), null=True, blank=True, on_delete=models.SET_NULL) - statement_condition_type = models.ForeignKey( - StatementConditionType, verbose_name=_("Type"), on_delete=models.PROTECT) + verification_officers = models.TextField(_("Verification officers"), default="", blank=True) + campaign_number = models.TextField(_("Campaign/observation number"), default="", + blank=True) + report_number = models.TextField(_("Report number"), default="", blank=True) follow_up_actions = models.ManyToManyField( FollowUpActionType, verbose_name=_("Follow-up actions"), blank=True ) @@ -1898,6 +1899,44 @@ class StatementCondition( def _get_base_image_path(self): return f"{self.SLUG}/{self.date.year}" + @post_importer_action + def set_default_from_find(self, context, v): + """ + Set statement condition attributes from find + """ + if not v: + return + initial = self.get_initial_from_find(self.find, prefix="") + for key, value in initial.items(): + if not value: + continue + if key in self.OVERLOADED_FIELDS: + # simple attr + current_value = getattr(self, key) + if current_value: # attr set + continue + setattr(self, key, value) + continue + # m2m + if getattr(self, key).exists(): # attr set + continue + getattr(self, key).add(*value) + return + + set_default_from_find.post_save = True + + @post_importer_action + def set_apply_validation(self, context, v): + """ + Apply validation to find. + """ + if not v: + return + self.apply_validation() + return self + + set_apply_validation.post_save = True + @property def is_last(self): return not self.__class__.objects.filter( @@ -1953,6 +1992,18 @@ class StatementCondition( ) return initial + def _check_initial(self): + """ + Verify that there is only one initial statement condition + for this find. If there is many, set only the older to initial. + """ + q = self.__class__.objects.filter(find_id=self.find_id, + initial=True) + if q.count() < 2: + return + older = q.order_by("date", "pk").all()[0] + q.exclude(pk=older.pk).update(initial=False) + def _check_apply_validation(self): if not self.pk: # no previous state so apply immediatly @@ -1979,6 +2030,7 @@ class StatementCondition( obj.last = False obj.in_charge = None obj.treatment = None + obj.date = obj.find.created for k in ("campaign_number", "report_number", "observations", "verification_officers"): setattr(obj, k, "") @@ -1993,6 +2045,17 @@ class StatementCondition( continue getattr(obj, attr).add(*value) + @staticmethod + def _get_m2m(item: "MainItem", attr: str) -> list: + """ + Get m2m ids from one item. + """ + return list( + sorted( + list(getattr(item, attr).values_list("id", flat=True)) + ) + ) + def apply_validation(self): """ Copy statement condition fields to the associated find @@ -2005,16 +2068,8 @@ class StatementCondition( for attr in self.OVERLOADED_FIELDS: setattr(self.find, attr, getattr(self, attr)) for attr in self.OVERLOADED_M2M_FIELDS: - m2m = list( - sorted( - list(getattr(self.find, attr).values_list("id", flat=True)) - ) - ) - new_m2m = list( - sorted( - list(getattr(self, attr).values_list("id", flat=True)) - ) - ) + m2m = self._get_m2m(self.find, attr) + new_m2m = self._get_m2m(self, attr) if m2m == new_m2m: continue getattr(self.find, attr).clear() @@ -2063,9 +2118,14 @@ class StatementCondition( def save(self, *args, **kwargs): apply_validation = False - if self.applied in ("V", "T"): + if self.pk and self.applied == "V" and \ + self.__class__.objects.filter( + pk=self.pk).values_list("applied", flat=True)[0] == "D": + raise + # only apply when "applied" is from D to V apply_validation = self._check_apply_validation() super().save(*args, **kwargs) + self._check_initial() if apply_validation: self.apply_validation() |
