diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-11-16 19:10:01 +0100 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-12-12 12:23:19 +0100 | 
| commit | c0c0f67e4f6150de5c3cee7b13ef257a367486b9 (patch) | |
| tree | 101ceae0727da1829d5761dd65ac5d89d091a479 | |
| parent | 2e15f5fd3525120d22336faf10e960050aaefb67 (diff) | |
| download | Ishtar-c0c0f67e4f6150de5c3cee7b13ef257a367486b9.tar.bz2 Ishtar-c0c0f67e4f6150de5c3cee7b13ef257a367486b9.zip  | |
Operation from file creation - fix automatic town association and parcel recopy
| -rw-r--r-- | archaeological_operations/models.py | 31 | ||||
| -rw-r--r-- | archaeological_operations/wizards.py | 4 | 
2 files changed, 34 insertions, 1 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index 57f538383..293b1e933 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -2260,6 +2260,12 @@ def operation_post_save(sender, **kwargs):      post_save_geo(sender=sender, **kwargs)      operation = kwargs["instance"] + +    # on creation associate parcel from a file if the file exist +    if kwargs.get("created", False) and operation.associated_file: +        for parcel in operation.associated_file.parcels.all(): +            parcel.copy_from_file_to_operation(operation) +      operation.skip_history_when_saving = True      if operation.fnap_financing and operation.cost:          fnap_cost = int(float(operation.cost) / 100 * operation.fnap_financing) @@ -3194,6 +3200,31 @@ class Parcel(LightHistorizedItem):          self.operation = None          self.save() +    def copy_from_file_to_operation(self, operation): +        """ +        Used to copy parcel from file on operation creation +        """ +        if not self.associated_file: +            return +        keys = { +            "town": self.town, +            "section": self.section, +            "parcel_number": self.parcel_number, +            "operation": operation, +            "associated_file": None, +            "defaults": {"address": self.address, "year": self.year}, +        } +        new_p, created = Parcel.objects.get_or_create(**keys) +        # copy owning only if created +        if created: +            for owning in self.owners.all(): +                ParcelOwner.objects.create( +                    owner=owning.owner, +                    parcel=new_p, +                    start_date=owning.start_date, +                    end_date=owning.end_date, +                ) +      def clean_orphan(self):          """          Remove when the parcel is linked to nothing diff --git a/archaeological_operations/wizards.py b/archaeological_operations/wizards.py index 44b3988eb..cafb7598b 100644 --- a/archaeological_operations/wizards.py +++ b/archaeological_operations/wizards.py @@ -138,7 +138,7 @@ class OperationWizard(Wizard):          Show a specific warning if no archaeological file is provided          """          datas = super(OperationWizard, self).get_formated_datas(forms) -        # if the general town form is used the advertissement is relevant +        # if the general town form is used the advertisement is relevant          has_no_af = [              form.prefix for form in forms if form.prefix == "townsgeneral-operation"          ] and True @@ -186,6 +186,8 @@ class OperationWizard(Wizard):              (("total_surface",), "surface"),          )          initial.update(self.__copy_fields(file, keys)) +        if "town" not in initial: +            initial["town"] = [idx for idx, __ in self.get_towns()]          if file.is_preventive():              return initial          keys = (  | 
