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 | c3b928b50cda4c087699fff50fd6e91dd75d2580 (patch) | |
| tree | fb1c2133c08e1bdc3d1a087618c59776546ee266 | |
| parent | bc959913ada31882c49bbfe510933edc9f043444 (diff) | |
| download | Ishtar-c3b928b50cda4c087699fff50fd6e91dd75d2580.tar.bz2 Ishtar-c3b928b50cda4c087699fff50fd6e91dd75d2580.zip | |
Datings: post-fix of duplicates associated to the same object (usually on imports)
| -rw-r--r-- | archaeological_context_records/models.py | 20 | ||||
| -rw-r--r-- | archaeological_context_records/tests.py | 35 | ||||
| -rw-r--r-- | archaeological_finds/models_finds.py | 6 | ||||
| -rw-r--r-- | archaeological_finds/tests.py | 13 | ||||
| -rw-r--r-- | ishtar_common/data_importer.py | 3 | ||||
| -rw-r--r-- | ishtar_common/wizards.py | 4 | 
6 files changed, 76 insertions, 5 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) diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py index 4fea6edd3..78280bede 100644 --- a/archaeological_finds/models_finds.py +++ b/archaeological_finds/models_finds.py @@ -1095,6 +1095,12 @@ class Find(ValueGetter, BaseHistorizedItem, ImageModel, OwnPerms,                  #    base_find.material_index = \                  #        idx and idx['material_index__max'] + 1 or 1 +    def fix(self): +        """ +        Fix redundant m2m dating association (usually after imports) +        """ +        Dating.fix_dating_association(self) +  post_save.connect(cached_label_changed, sender=Find) diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index 41e113245..86c7936ba 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -123,6 +123,11 @@ class FindWizardCreationTest(WizardTest, FindInit, TestCase):                          'period': None,                          'start_date': '0',                          'end_date': '200', +                    }, +                    { +                        'period': None, +                        'start_date': '0', +                        'end_date': '200',                      }                  ]              }, @@ -135,8 +140,11 @@ class FindWizardCreationTest(WizardTest, FindInit, TestCase):          self.form_datas[0].form_datas['selecrecord-find_creation']['pk'] = \              cr.pk +        period = Period.objects.all()[0].pk          self.form_datas[0].form_datas['dating-find_creation'][0]['period'] = \ -            Period.objects.all()[0].pk +            period +        self.form_datas[0].form_datas['dating-find_creation'][1]['period'] = \ +            period          self.find_number = models.Find.objects.count()          self.basefind_number = models.BaseFind.objects.count()          super(FindWizardCreationTest, self).pre_wizard() @@ -146,6 +154,9 @@ class FindWizardCreationTest(WizardTest, FindInit, TestCase):                           self.basefind_number + 1)          self.assertEqual(models.Find.objects.count(),                           self.find_number + 1) +        # identical datings, only one should be finaly save +        f = models.Find.objects.order_by("-pk").all()[0] +        self.assertEqual(f.datings.count(), 1)  class FindWizardDeletionWithWarehouseModTest(WizardTest, FindInit, TestCase): diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index e11e72449..dbc7c21cb 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -1538,6 +1538,9 @@ class Importer(object):              if m2ms:                  # force post save script                  obj.save() +            if hasattr(obj, 'fix'): +                # post save/m2m specific fix +                obj.fix()          except IntegrityError as e:              message = e.message              try: diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index 3f90f8c48..c5158dfcd 100644 --- a/ishtar_common/wizards.py +++ b/ishtar_common/wizards.py @@ -757,6 +757,10 @@ class Wizard(NamedUrlWizardView):                      item.skip_history_when_saving = True                      item.save() +        if hasattr(obj, 'fix'): +            # post save/m2m specific fix +            obj.fix() +          # make the new object a default          if self.current_obj_slug:              self.request.session[self.current_obj_slug] = unicode(obj.pk) | 
