summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
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
commitb564d3d2d32782ca439f9d721d01b0fe6cff25a3 (patch)
treefd3cd5fd78447db9db5663c3c30c4d4d1379da54
parent2b059ee6ed6e90c63ab8f04d3709800d4d365227 (diff)
downloadIshtar-b564d3d2d32782ca439f9d721d01b0fe6cff25a3.tar.bz2
Ishtar-b564d3d2d32782ca439f9d721d01b0fe6cff25a3.zip
Fix cache operation update - Parcel associated with context records are not removed!
-rw-r--r--archaeological_context_records/tests.py42
-rw-r--r--archaeological_operations/models.py21
-rw-r--r--archaeological_operations/tests.py18
-rw-r--r--ishtar_common/utils.py7
4 files changed, 80 insertions, 8 deletions
diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py
index 1c900184c..b87f3ce4d 100644
--- a/archaeological_context_records/tests.py
+++ b/archaeological_context_records/tests.py
@@ -147,7 +147,6 @@ class ContextRecordInit(OperationInitTest):
def create_context_record(self, user=None, data={}, force=False):
if not getattr(self, 'context_records', None):
self.context_records = []
-
default = {'label': "Context record"}
if force or not data.get('operation'):
data['operation'] = self.get_default_operation(force=force)
@@ -200,6 +199,47 @@ class ContextRecordTest(ContextRecordInit, TestCase):
cr.external_id,
u"{}-{}".format(cr.parcel.external_id, cr.label))
+ def test_lost_parcel_dont_delete_context_record(self):
+ cr = self.create_context_record(force=True)[0]
+ parcel = models.Parcel.objects.get(pk=cr.parcel.pk)
+ parcel.operation = None
+ parcel.save()
+ self.assertEqual(
+ models.ContextRecord.objects.filter(pk=cr.pk).count(), 1)
+ # associated operation is restored
+ self.assertEqual(
+ models.Parcel.objects.get(pk=parcel.pk).operation,
+ cr.operation
+ )
+
+ def test_cache_update(self):
+ cr = self.create_context_record()[0]
+ cr_pk = cr.pk
+ # OP2010 - 1 | A | 1 | CR 1
+ ope_id, parcel_sec, parcel_nb, cr_label = cr.cached_label.split(' | ')
+ self.assertEqual(ope_id, 'OP2010-1')
+ self.assertEqual(parcel_sec, cr.parcel.section)
+ self.assertEqual(parcel_nb, cr.parcel.parcel_number)
+ self.assertEqual(cr_label, cr.label)
+
+ new_lbl = "UE 2"
+ cr.label = new_lbl
+ cr.save()
+ cr = models.ContextRecord.objects.get(pk=cr_pk)
+ ope_id, parcel_sec, parcel_nb, cr_label = cr.cached_label.split(' | ')
+ self.assertEqual(cr_label, new_lbl)
+
+ new_sec, new_nb = "B", "42"
+ parcel = cr.parcel
+ parcel.section = new_sec
+ parcel.parcel_number = new_nb
+ parcel.save()
+ cr = models.ContextRecord.objects.get(pk=cr_pk)
+ ope_id, parcel_sec, parcel_nb, cr_label = cr.cached_label.split(' | ')
+ self.assertEqual(parcel_sec, new_sec)
+ self.assertEqual(parcel_nb, new_nb)
+
+
class ContextRecordSearchTest(ContextRecordInit, TestCase):
fixtures = ImportContextRecordTest.fixtures
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 +
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py
index 83534d93a..e16d1abb6 100644
--- a/ishtar_common/utils.py
+++ b/ishtar_common/utils.py
@@ -55,6 +55,13 @@ def get_cache(cls, extra_args=[]):
return cache_key, cache.get(cache_key)
+def force_cached_label_changed(sender, **kwargs):
+ if not kwargs.get('instance'):
+ return
+ kwargs['instance']._cached_label_checked = False
+ cached_label_changed(sender, **kwargs)
+
+
def cached_label_changed(sender, **kwargs):
if not kwargs.get('instance'):
return