diff options
author | Étienne Loks <etienne.loks@proxience.com> | 2015-07-10 21:20:43 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@proxience.com> | 2015-07-10 21:20:43 +0200 |
commit | 6f50ea8965abd59e91e18390dd6305635a9fc3d7 (patch) | |
tree | 450fd988cf84b43a1f32e3ab559ffb29ab231ae2 /archaeological_operations | |
parent | 7a10be6aa9afb55beb461f5ea2eef1750750187a (diff) | |
parent | a500dd90aca319d9306cf08c7c5ad73d4e27a261 (diff) | |
download | Ishtar-6f50ea8965abd59e91e18390dd6305635a9fc3d7.tar.bz2 Ishtar-6f50ea8965abd59e91e18390dd6305635a9fc3d7.zip |
Merge branch 'stable'
Diffstat (limited to 'archaeological_operations')
-rw-r--r-- | archaeological_operations/models.py | 68 | ||||
-rw-r--r-- | archaeological_operations/wizards.py | 11 |
2 files changed, 57 insertions, 22 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index f13ee4ca9..3011d8076 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -458,17 +458,7 @@ def operation_post_save(sender, **kwargs): # manage parcel association if FILES_AVAILABLE and operation.associated_file: for parcel in operation.parcels.all(): - keys = {'town':parcel.town, 'section':parcel.section, - 'parcel_number':parcel.parcel_number} - if not operation.associated_file.parcels.filter(**keys).count(): - keys['address'] = parcel.address - keys['year'] = parcel.year - keys['associated_file'] = operation.associated_file - new_p = Parcel.objects.create(**keys) - for owning in parcel.owners.all(): - ParcelOwner.objects.create(owner=owning.owner, - parcel=new_p, start_date=owning.start_date, - end_date=owning.end_date) + parcel.copy_to_file() post_save.connect(operation_post_save, sender=Operation) class OperationByDepartment(models.Model): @@ -790,12 +780,59 @@ class Parcel(LightHistorizedItem): if item] return settings.JOINT.join(items) + def copy_to_file(self): + """ + Copy from operation to file when associating file to operation + """ + if not self.operation or not self.operation.associated_file: + # not concerned + return + keys = {'town':self.town, 'section':self.section, + 'parcel_number':self.parcel_number} + if self.operation.associated_file.parcels.filter(**keys).count(): + # everything is OK + return + keys['address'] = self.address + keys['year'] = self.year + keys['associated_file'] = self.operation.associated_file + new_p = Parcel.objects.create(**keys) + # also copy owning + 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 copy_to_operation(self): + """ + Parcel cannot have operation and associated_file but on + new parcel association a copy have to be done before cleaning + """ + if not (self.operation and self.associated_file): + # everything is OK + return + keys = {'town':self.town, 'section':self.section, + 'parcel_number':self.parcel_number, + 'operation':self.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) + self.operation = None + self.save() + def parcel_post_save(sender, **kwargs): if not kwargs['instance']: return parcel = kwargs['instance'] if not parcel.external_id and (parcel.section or parcel.parcel_number): - parcel.external_id = (parcel.section or "") + (parcel.parcel_number or "") + parcel.external_id = unicode(parcel.section or "") + \ + unicode(parcel.parcel_number or "") parcel.save() return if parcel.operation and parcel.operation.pk and \ @@ -808,11 +845,8 @@ def parcel_post_save(sender, **kwargs): if not FILES_AVAILABLE: return if parcel.operation and parcel.associated_file: - return - if parcel.operation and parcel.operation.associated_file: - parcel.associated_file = parcel.operation.associated_file - parcel.save() - return + # parcels are copied between files and operations + parcel.copy_to_operation() post_save.connect(parcel_post_save, sender=Parcel) class ParcelOwner(LightHistorizedItem): diff --git a/archaeological_operations/wizards.py b/archaeological_operations/wizards.py index c7c375a49..01e717b18 100644 --- a/archaeological_operations/wizards.py +++ b/archaeological_operations/wizards.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2012-2014 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2012-2015 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -114,10 +114,11 @@ class OperationWizard(Wizard): parcels = [] current_parcels = [] operation = self.get_current_object() - for parcel in operation.parcels.all(): - current_parcels.append((parcel.town, parcel.section, - parcel.parcel_number)) - parcels.append((parcel.pk, parcel.short_label)) + if operation: + for parcel in operation.parcels.all(): + current_parcels.append((parcel.town, parcel.section, + parcel.parcel_number)) + parcels.append((parcel.pk, parcel.short_label)) try: for parcel in file.parcels.all(): if (parcel.town, parcel.section, parcel.parcel_number) \ |