Delete "container" relation on archive unit when unlinked from a profile
authorDenis Laxalde <>
Fri, 19 Oct 2018 13:59:56 +0200
changeset 2972 359177d6a1c8
parent 2966 2c785ecccbbe
child 2973 e2837a22e676
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 as other container-related tests live there. In migration, we drop spurious "container" relation (not sure there are some).
--- a/cubicweb_seda/	Tue Oct 09 09:31:07 2018 +0200
+++ b/cubicweb_seda/	Fri Oct 19 13:59:56 2018 +0200
@@ -90,6 +90,23 @@
             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
--- a/test/	Tue Oct 09 09:31:07 2018 +0200
+++ b/test/	Fri Oct 19 13:59:56 2018 +0200
@@ -70,6 +70,24 @@
                 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: