diff options
| -rw-r--r-- | archaeological_context_records/models.py | 24 | ||||
| -rw-r--r-- | archaeological_context_records/tests.py | 49 | ||||
| -rw-r--r-- | archaeological_operations/tests.py | 5 | ||||
| -rw-r--r-- | ishtar_common/models.py | 2 | 
4 files changed, 74 insertions, 6 deletions
| diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index 454e83e8a..5dfaa427a 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -1217,7 +1217,23 @@ class RecordRelations(GeneralRecordRelations, models.Model):          return self.left_record.uuid, self.right_record.uuid, self.relation_type.txt_idx -post_delete.connect(post_delete_record_relation, sender=RecordRelations) +def post_delete_cr_record_relation(sender, instance, **kwargs): +    if getattr(sender, "_no_post_treatments", False): +        return +    post_delete_record_relation(sender, instance, **kwargs) +    ContextRecordTree.update(instance.left_record_id) +    ContextRecordTree.update(instance.right_record_id) + + +def post_save_cr_record_relation(sender, instance, **kwargs): +    if getattr(sender, "_no_post_treatments", False): +        return +    ContextRecordTree.update(instance.left_record_id) +    ContextRecordTree.update(instance.right_record_id) + + +post_delete.connect(post_delete_cr_record_relation, sender=RecordRelations) +post_save.connect(post_save_cr_record_relation, sender=RecordRelations)  class RecordRelationView(models.Model): @@ -1363,9 +1379,9 @@ class ContextRecordTree(RelationsViews):          cr_id               integer NOT NULL,          cr_parent_id        integer NOT NULL,          CONSTRAINT fk1_{table} FOREIGN KEY(cr_id) -            REFERENCES {fk_table}(id), +            REFERENCES {fk_table}(id) ON DELETE CASCADE,          CONSTRAINT fk2_{table} FOREIGN KEY(cr_parent_id) -            REFERENCES {fk_table}(id) +            REFERENCES {fk_table}(id) ON DELETE CASCADE      );      CREATE INDEX {table}_id ON {table} (cr_id);      CREATE INDEX {table}_parent_id ON {table} (cr_parent_id); @@ -1377,11 +1393,13 @@ class ContextRecordTree(RelationsViews):          "archaeological_context_records.ContextRecord",          verbose_name=_("Context record"),          related_name="context_record_tree_parent", +        on_delete=models.CASCADE      )      cr_parent = models.ForeignKey(          "archaeological_context_records.ContextRecord",          verbose_name=_("Context record parent"),          related_name="context_record_tree_child", +        on_delete=models.CASCADE      )      class Meta: diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py index 6b3e9f648..0d1c84c7f 100644 --- a/archaeological_context_records/tests.py +++ b/archaeological_context_records/tests.py @@ -973,6 +973,7 @@ class RecordRelationsTest(ContextRecordInit, TestCase):              (1, 3), (2, 3), (3, 4), (3, 5), (4, 6), (4, 7), (4, 8),              (5, 9), (5, 10)          ) +        models.RecordRelations._no_post_treatments = True          for child_idx, parent_idx in relations:              models.RecordRelations.objects.create(                  left_record=crs[child_idx - 1], @@ -996,10 +997,11 @@ class RecordRelationsTest(ContextRecordInit, TestCase):              cr_parent_id=crs[2].pk, cr_id=crs[0].pk)          self.assertGreaterEqual(q.count(), 1) +        ## use tables          self.assertIsNone(models.ContextRecordTree.check_engine())  # no change          profile.parent_relations_engine = "T"          profile.save() -        profile = get_current_profile(force=True) +        get_current_profile(force=True)          self.assertTrue(models.ContextRecordTree.check_engine())  # change to table          q = models.ContextRecordTree.objects.filter(cr=crs[0], cr_parent=crs[1])          self.assertEqual(q.count(), 0)  # empty table @@ -1075,6 +1077,45 @@ class RecordRelationsTest(ContextRecordInit, TestCase):              cr_parent=crs[10 - 1], cr=crs[14 - 1]).count()          self.assertEqual(nb, 0) +        # auto update +        models.RecordRelations._no_post_treatments = False +        models.RecordRelations.objects.create( +            left_record=crs[3 - 1], +            right_record=crs[4 - 1], +            relation_type=rel_type_1 +        ) +        nb = models.ContextRecordTree.objects.filter( +            cr_parent=crs[6 - 1], cr=crs[3 - 1]).count() +        self.assertEqual(nb, 1) +        models.RecordRelations.objects.create( +            left_record=crs[3 - 1], +            right_record=crs[5 - 1], +            relation_type=rel_type_1 +        ) +        nb = models.ContextRecordTree.objects.filter( +            cr_parent=crs[10 - 1], cr=crs[14 - 1]).count() +        self.assertEqual(nb, 1) + +        # delete +        nb = models.ContextRecordTree.objects.filter( +            cr_parent=crs[13 - 1], cr=crs[1 - 1]).count() +        self.assertEqual(nb, 1) +        crs[3 - 1].delete() +        nb = models.ContextRecordTree.objects.filter( +            cr_parent=crs[13 - 1], cr=crs[1 - 1]).count() +        self.assertEqual(nb, 0) + +        # delete on views +        profile = get_current_profile() +        profile.parent_relations_engine = "V" +        profile.save() +        get_current_profile(force=True) +        models.ContextRecordTree.objects.filter(pk__isnull=False).delete() +        crs[4 - 1].delete() +        q = models.ContextRecordTree.objects.filter( +            cr_parent_id=crs[1 - 1].pk, cr_id=crs[6 - 1].pk) +        self.assertGreaterEqual(q.count(), 0) +      def _test_tree_(self, test_trees, context_record):          crs = self.context_records          for tree in test_trees: @@ -1094,6 +1135,12 @@ class RecordRelationsTest(ContextRecordInit, TestCase):          models.ContextRecordTree.update(crs[cr_idx].id)          self._test_tree_(test_trees, cr_idx + 1) +    def tearDown(self): +        models.ContextRecordTree.objects.filter(pk__isnull=False).delete() +        profile = get_current_profile() +        profile.parent_relations_engine = "V" +        profile.save() +  class ContextRecordWizardCreationTest(WizardTest, ContextRecordInit, TestCase):      fixtures = OPERATION_TOWNS_FIXTURES diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 192d51214..09920cf13 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -1718,7 +1718,10 @@ class OperationInitTest(object):      def tearDown(self):          # cleanup for further test          if hasattr(self, "user"): -            self.user.delete() +            try: +                self.user.delete() +            except: +                pass              self.user = None          # all try/except is necessary for bad migrations on main...          # should be removed at the next big version diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 19c432053..54d809fdd 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -792,7 +792,7 @@ class GeneralRecordRelations(object):      def save(self, *args, **kwargs):          super(GeneralRecordRelations, self).save(*args, **kwargs) -        # after saving create the symetric or the inverse relation +        # after saving create the symmetric or the inverse relation          sym_rel_type = self.relation_type          if not self.relation_type.symmetrical: | 
