diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-07-24 19:34:27 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-07-24 19:37:02 +0200 |
commit | 73eedc117776ad42e2241232ad1dcc547cb967b2 (patch) | |
tree | fb1c2133c08e1bdc3d1a087618c59776546ee266 /archaeological_context_records | |
parent | 8fa0f928e68e10d4ef055bf9afd47e0e91ce7b55 (diff) | |
download | Ishtar-73eedc117776ad42e2241232ad1dcc547cb967b2.tar.bz2 Ishtar-73eedc117776ad42e2241232ad1dcc547cb967b2.zip |
Datings: post-fix of duplicates associated to the same object (usually on imports)
Diffstat (limited to 'archaeological_context_records')
-rw-r--r-- | archaeological_context_records/models.py | 20 | ||||
-rw-r--r-- | archaeological_context_records/tests.py | 35 |
2 files changed, 51 insertions, 4 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index 940330d86..a5f02133e 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -74,6 +74,20 @@ class Dating(models.Model): return unicode(self.period) return u"%s (%s-%s)" % (self.period, start_date, end_date) + @classmethod + def fix_dating_association(cls, obj): + """ + Fix redundant m2m dating association (usually after imports) + """ + current_datings = [] + for dating in obj.datings.order_by('pk').all(): + key = (dating.period.pk, dating.start_date, dating.end_date, + dating.dating_type, dating.quality, dating.precise_dating) + if key not in current_datings: + current_datings.append(key) + continue + dating.delete() + class Unit(GeneralType): order = models.IntegerField(_(u"Order")) @@ -493,6 +507,12 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, self.save() return returned + def fix(self): + """ + Fix redundant m2m dating association (usually after imports) + """ + Dating.fix_dating_association(self) + post_save.connect(cached_label_changed, sender=ContextRecord) diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py index b613b837e..b64754ad6 100644 --- a/archaeological_context_records/tests.py +++ b/archaeological_context_records/tests.py @@ -352,6 +352,21 @@ class ContextRecordTest(ContextRecordInit, TestCase): self.assertEqual(response.status_code, 200) self.assertIn('class="sheet"', response.content) + def test_redundant_dating_clean(self): + obj = self.context_records[0] + values = {'period': models.Period.objects.all()[0]} + values_2 = {'period': models.Period.objects.all()[0], + 'quality': models.DatingQuality.objects.all()[0]} + + obj.datings.add(models.Dating.objects.create(**values)) + obj.datings.add(models.Dating.objects.create(**values)) + obj.datings.add(models.Dating.objects.create(**values_2)) + obj.datings.add(models.Dating.objects.create(**values_2)) + self.assertEqual(obj.datings.count(), 4) + obj.fix() + self.assertEqual(obj.datings.count(), 2) + + class ContextRecordSearchTest(ContextRecordInit, TestCase): fixtures = ImportContextRecordTest.fixtures @@ -558,22 +573,23 @@ class ContextRecordWizardCreationTest(WizardTest, ContextRecordInit, TestCase): 'general': { 'label': "First" }, - 'relations': [] + 'relations': [], }, ignored=('datings', 'interpretation', ) ), FormData( - "Create a context record with a relation", + "Create a context record with a relation and datings", form_datas={ 'selec': {}, 'general': { 'label': "Second" }, - 'relations': [] + 'relations': [], + 'datings': [] }, - ignored=('datings', 'interpretation',) + ignored=('interpretation',) ), ] @@ -598,6 +614,14 @@ class ContextRecordWizardCreationTest(WizardTest, ContextRecordInit, TestCase): label="Test").pk} ) + period = models.Period.objects.all()[0].pk + self.form_datas[1].append( + 'datings', {'period': period} + ) + self.form_datas[1].append( + 'datings', {'period': period} + ) + self.cr_nb = models.ContextRecord.objects.count() super(ContextRecordWizardCreationTest, self).pre_wizard() @@ -606,3 +630,6 @@ class ContextRecordWizardCreationTest(WizardTest, ContextRecordInit, TestCase): self.cr_nb + 2) self.assertEqual(self.related_cr.left_relations.count(), 1) + # identical datings, only one should be finaly save + cr = models.ContextRecord.objects.order_by('-pk')[0] + self.assertEqual(cr.datings.count(), 1) |