diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-02-22 15:45:40 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-02-22 15:45:40 +0100 |
commit | 57e4cbc17b54e10757fb752b821cb862067840b7 (patch) | |
tree | fd3cd5fd78447db9db5663c3c30c4d4d1379da54 /archaeological_operations | |
parent | 79942f7242dd38f62dcee79c990070f84718b574 (diff) | |
download | Ishtar-57e4cbc17b54e10757fb752b821cb862067840b7.tar.bz2 Ishtar-57e4cbc17b54e10757fb752b821cb862067840b7.zip |
Fix cache operation update - Parcel associated with context records are not removed!
Diffstat (limited to 'archaeological_operations')
-rw-r--r-- | archaeological_operations/models.py | 21 | ||||
-rw-r--r-- | archaeological_operations/tests.py | 18 |
2 files changed, 32 insertions, 7 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index 7c9efaef7..225d46a53 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,\ @@ -769,7 +770,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): @@ -1356,16 +1357,22 @@ class Parcel(LightHistorizedItem): def parcel_post_save(sender, **kwargs): if not kwargs['instance']: return + from archaeological_context_records.models import ContextRecord 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: diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 364cc4c8e..9cdeb67e6 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -607,6 +607,9 @@ class OperationInitTest(object): def create_parcel(self, data={}): default = {'town': self.get_default_town(), 'section': 'A', 'parcel_number': '1'} + if not hasattr(self, 'operations'): + self.create_operation() + default['operation'] = self.operations[0] default.update(data) if not getattr(self, 'parcels', None): self.parcels = [] @@ -741,6 +744,21 @@ class OperationTest(TestCase, OperationInitTest): ope2 = create_operation(self.user, values={'year': 0}) self.assertEqual(ope2.operation_code, 2) + def test_cache_update(self): + self.create_towns() + operation = self.operations[0] + town, ope_id = operation.cached_label.split(' | ') + self.assertIn(town, (u'Intercommunal', u"Multi-town")) + self.assertEqual(ope_id, 'OP2010-1') + operation = models.Operation.objects.get(pk=operation.pk) + operation.year = 2011 + operation.save() + operation.towns.add(self.towns[0]) + operation = models.Operation.objects.get(pk=operation.pk) + town, ope_id = operation.cached_label.split(' | ') + self.assertEqual(ope_id, 'OP2011-1') + self.assertEqual(town, self.towns[0].name) + class OperationSearchTest(TestCase, OperationInitTest): fixtures = [settings.ROOT_PATH + |