[PATCH seda] Delete "container" relation on archive unit when unlinked from a profile

Denis Laxalde denis.laxalde at logilab.fr
Fri Oct 19 15:35:54 CEST 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1539950396 -7200
#      Fri Oct 19 13:59:56 2018 +0200
# Node ID 2823afce6ee56e105c0118adb6cdd9fc533ff929
# Parent  45d11e450efdd601d1f9b86393a2c6cdb916f6f4
# Available At http://hg.logilab.org/review/cubes/seda
#              hg pull http://hg.logilab.org/review/cubes/seda -r 2823afce6ee5
# EXP-Topic container-relation-deletion
Delete "container" relation on archive unit when unlinked from a profile

- Container machinery got introduced in 143ae7a4a964, at that time the
  "container" relation was mandatory (on subject) for all entity types.
- Integrity of this relation relied on the assumption that the relation
  was mandatory. (I.e. all entities mush have a link to their container
  and the only way to remove this link is to remove the container entity
  itself).
- In a88deb387b2b, this assumption got broken as "container" relation
  was made optional for SEDAArchiveUnit as a subject. From there, when
  an archive unit got unlinked from a profile (through deletion of the
  "seda_archive_unit" relation), a "container" relation remained set.
- This is problem since permission (especially on "delete" action)
  relies on the presence or absence of this relation. For instance,
  one would get an error when trying to delete an archive unit they just
  unlinked from a profile because they have no rights to delete the
  profile.

So we fix this by dropping the "container" relation when a
"seda_archive_unit" relation is deleted through a new hook.

Additional test goes in test_entities.py as other container-related
tests live there.

In migration, we drop spurious "container" relation (not sure there are
some).

diff --git a/cubicweb_seda/hooks.py b/cubicweb_seda/hooks.py
--- a/cubicweb_seda/hooks.py
+++ b/cubicweb_seda/hooks.py
@@ -90,6 +90,23 @@ class SetContainerHook(hook.Hook):
             SetContainerOp.get_instance(self._cw).add_data((self.eidto, self.eidfrom))
 
 
+class DeleteArchiveUnitContainerHook(hook.Hook):
+    """Delete the "container" relation on SEDAArchiveUnit upon deletion of
+    "seda_archive_unit" relation.
+
+    Normally, deletion of "container" relation occurs as a consequence of the
+    container entity deletion. But for SEDAArchiveUnit, the relation is
+    optional so deleting the container entity does not delete the relation.
+    """
+    __regid__ = 'seda.archive-unit.delete-container'
+    __select__ = hook.Hook.__select__ & hook.match_rtype('seda_archive_unit')
+    events = ('before_delete_relation',)
+
+    def __call__(self):
+        self._cw.execute('DELETE X container Y WHERE X eid %(x)s',
+                         {'x': self.eidfrom})
+
+
 class CheckRefNonRuleIdCodeListHook(hook.Hook):
     """Watch for addition of concept through seda_ref_non_rule_id_to relation, to ensure it belongs
     to the scheme specified on the transfer
diff --git a/cubicweb_seda/migration/0.14.1_Any.py b/cubicweb_seda/migration/0.14.1_Any.py
--- a/cubicweb_seda/migration/0.14.1_Any.py
+++ b/cubicweb_seda/migration/0.14.1_Any.py
@@ -19,3 +19,6 @@ unexpected_eids = [str(x) for x, in rql(
 sql('DELETE FROM container_relation WHERE eid_from IN ({});'.format(
     ','.join(unexpected_eids)))
 commit()
+
+rql('DELETE X container Y WHERE X is SEDAArchiveUnit, NOT EXISTS(X seda_archive_unit Y)')
+commit()
diff --git a/test/test_entities.py b/test/test_entities.py
--- a/test/test_entities.py
+++ b/test/test_entities.py
@@ -70,6 +70,24 @@ class ContainerTC(CubicWebTC):
                 entity.cw_clear_all_caches()
                 self.assertEqual(entity.cw_adapt_to('IContained').container.eid, transfer.eid)
 
+    def test_container_relation_archive_unit(self):
+        """Ensure that container relation is deleted upon deletion on
+        "seda_archive_unit" relation.
+        """
+        with self.admin_access.cnx() as cnx:
+            transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test')
+            unit, _, _ = create_archive_unit(transfer, cnx=cnx,
+                                             user_cardinality=u'1',
+                                             user_annotation=u'plop')
+            cnx.commit()
+            unit.cw_clear_all_caches()
+            self.assertEqual(unit.container, (transfer, ))
+            self.assertIn(unit, transfer.reverse_seda_archive_unit)
+            transfer.cw_set(reverse_seda_archive_unit=None)
+            cnx.commit()
+            unit.cw_clear_all_caches()
+            self.assertEqual(unit.container, ())
+
     def test_archive_unit_container_clone(self):
         """Functional test for SEDA component clone."""
         with self.admin_access.cnx() as cnx:



More information about the saem-devel mailing list