diff options
Diffstat (limited to 'archaeological_operations/models.py')
-rw-r--r-- | archaeological_operations/models.py | 47 |
1 files changed, 34 insertions, 13 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index 7c9efaef7..e74d02647 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -29,7 +29,8 @@ from django.db.models.signals import post_save, m2m_changed, post_delete from django.forms import ValidationError from django.utils.translation import ugettext_lazy as _, ugettext -from ishtar_common.utils import cached_label_changed, get_cache, mode +from ishtar_common.utils import cached_label_changed, \ + force_cached_label_changed, get_cache, mode from ishtar_common.models import GeneralType, BaseHistorizedItem, \ HistoricalRecords, LightHistorizedItem, OwnPerms, Department, Source,\ @@ -430,13 +431,13 @@ class Operation(ClosedItem, BaseHistorizedItem, ImageModel, OwnPerms, def get_reference(self, full=False): ref = "" if settings.COUNTRY == 'fr' and self.code_patriarche: - ref = "OA" + unicode(self.code_patriarche) + ref = settings.ISHTAR_OPE_PREFIX + unicode(self.code_patriarche) if not full: return ref if self.year and self.operation_code: if ref: ref += u" - " - ref += settings.OP_PREFIX + ref += settings.ISHTAR_DEF_OPE_PREFIX ref += u"-".join((unicode(self.year), unicode(self.operation_code))) return ref or "00" @@ -462,6 +463,12 @@ class Operation(ClosedItem, BaseHistorizedItem, ImageModel, OwnPerms, def _get_associated_cached_labels(self): return list(self.context_record.all()) + def _cached_labels_bulk_update(self): + if settings.TESTING and settings.USE_SPATIALITE_FOR_TESTS: + return + self.context_record.model.cached_label_bulk_update(operation_id=self.pk) + return True + def get_town_label(self): lbl = unicode(_('Intercommunal')) if self.towns.count() == 1: @@ -517,15 +524,15 @@ class Operation(ClosedItem, BaseHistorizedItem, ImageModel, OwnPerms, return "" lbl = unicode(self.operation_code) year = self.year or 0 - lbl = settings.OP_PREFIX + u"%d-%s%s" % (year, (3 - len(lbl)) * "0", - lbl) + lbl = settings.ISHTAR_DEF_OPE_PREFIX \ + + u"%d-%s%s" % (year, (3 - len(lbl)) * "0", lbl) return lbl @property def full_code_patriarche(self): if not self.code_patriarche: return '' - return u"OA" + unicode(self.code_patriarche) + return settings.ISHTAR_OPE_PREFIX + unicode(self.code_patriarche) def clean(self): if not self.operation_code: @@ -769,7 +776,7 @@ class Operation(ClosedItem, BaseHistorizedItem, ImageModel, OwnPerms, return super(Operation, self).save(*args, **kwargs) -m2m_changed.connect(cached_label_changed, sender=Operation.towns.through) +m2m_changed.connect(force_cached_label_changed, sender=Operation.towns.through) def operation_post_save(sender, **kwargs): @@ -1359,24 +1366,38 @@ def parcel_post_save(sender, **kwargs): parcel = kwargs['instance'] created = kwargs.get('created', None) + updated = False # remove when the parcel is linked to nothing - if not getattr(parcel, '_updated_id', None) and not created and not \ - parcel.operation and not parcel.associated_file: - parcel.delete() - return + if not getattr(parcel, '_updated_id', None) and not created \ + and not parcel.operation and not parcel.associated_file: + if parcel.context_record.count(): + # trying to restore a lost parcel + parcel.operation = parcel.context_record.all()[0].operation + updated = True + else: + parcel.delete() + return - updated = False if not parcel.external_id or parcel.auto_external_id: external_id = get_external_id('parcel_external_id', parcel) if external_id != parcel.external_id: updated = True + parcel._updated_id = True parcel.auto_external_id = True parcel.external_id = external_id if updated: - parcel._updated_id = True parcel.save() return + if parcel.context_record.count(): + if settings.TESTING and settings.USE_SPATIALITE_FOR_TESTS: + for cr in parcel.context_record.all(): + cr.skip_history_when_saving = True + cr.save() + else: + parcel.context_record.model.cached_label_bulk_update( + parcel_id=parcel.id) + if parcel.operation and parcel.operation.pk and \ parcel.town not in list(parcel.operation.towns.all()): parcel.operation.towns.add(parcel.town) |