[hooks] Delete binary DO when deleting an archive unit not related to a profile
authorDenis Laxalde <denis.laxalde@logilab.fr>
Fri, 14 Dec 2018 09:57:34 +0100
changeset 2981 30e67f4f9e29
parent 2980 049322b9d8ee
child 2982 509b9ca09d42
[hooks] Delete binary DO when deleting an archive unit not related to a profile We extend the logic of "seda.doref.del" hook so that it not only handles the case of "simplified profile" but also the case of archive unit not related to a profile. In the later case, we check that the container of "SEDABinaryDataObject" is really a "SEDAArchiveUnit" (not sure what else it could be) and trigger the operation deleting the binary data-object when its parent object reference (SEDADataObjectReference entity) is deleted. Accordingly, we rename hook and operation classes and update their docstring. This resolves CONSEJIRA-643 but making deletion of an archive unit containing a binary data object but not related to profile. New test goes into test_schema.py below other tests related to the hook we modified.
cubicweb_seda/hooks.py
test/test_schema.py
--- a/cubicweb_seda/hooks.py	Fri Dec 14 09:39:19 2018 +0100
+++ b/cubicweb_seda/hooks.py	Fri Dec 14 09:57:34 2018 +0100
@@ -298,9 +298,9 @@
             self.do_ref.cw_set(user_cardinality=self.do.user_cardinality)
 
 
-class SimplifiedProfileDelRefROHook(hook.Hook):
+class DeleleBDOWhenDeletingDORefHook(hook.Hook):
     """Hook triggering an operation to delete the data object associated to a
-    reference when the later is deleted from a simplified profile.
+    reference when the later is deleted from a simplified profile or an archive unit.
     """
     __regid__ = 'seda.doref.del'
     __select__ = hook.Hook.__select__ & hook.match_rtype('seda_data_object_reference_id')
@@ -314,13 +314,17 @@
                 container = bdo.seda_binary_data_object[0]
                 if not container.simplified_profile:
                     return
-            SimplifiedProfileDelRefROOp(self._cw, do_ref=ref, do=bdo)
+            else:
+                if not bdo.container or bdo.container[0].cw_etype != 'SEDAArchiveUnit':
+                    return
+            DeleleBDOAlongWithDORefOp(self._cw, do_ref=ref, do=bdo)
 
 
-class SimplifiedProfileDelRefROOp(hook.Operation):
+class DeleleBDOAlongWithDORefOp(hook.Operation):
     """Delete data object whose former reference has been deleted in the
     transaction. Expected to be run only if the profile is a simplified
-    profile.
+    profile or if the data object is related to an archive unit not itself
+    related to profile.
     """
 
     def precommit_event(self):
--- a/test/test_schema.py	Fri Dec 14 09:39:19 2018 +0100
+++ b/test/test_schema.py	Fri Dec 14 09:57:34 2018 +0100
@@ -279,6 +279,19 @@
                 len(cnx.execute('Any X WHERE X eid %(x)s', {'x': bdo.eid})),
                 0 if simplified else 1)
 
+    def test_delete_archiveunit_with_data_object(self):
+        """Make sure deletion of an archive unit with a binary data object,
+        but not related to an archive transfer is possible.
+        """
+        with self.admin_access.cnx() as cnx:
+            unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(None, cnx=cnx)
+            bdo = testutils.create_data_object(unit_alt_seq)
+            cnx.commit()
+            bdo.cw_clear_all_caches()
+            unit.cw_delete()
+            cnx.commit()
+            self.assertFalse(cnx.execute('Any X WHERE X eid %(x)s', {'x': bdo.eid}))
+
 
 class SecurityTC(CubicWebTC):