[PATCH 2 of 2 saem_ref] [seda] Unlink deprecated profiles from OrganizationUnit using them

Denis Laxalde denis.laxalde at logilab.fr
Wed Mar 21 14:25:30 CET 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1521637825 -3600
#      Wed Mar 21 14:10:25 2018 +0100
# Node ID 263a38bd1c656d2877d6a0dbca54e0dcc01ff5cf
# Parent  769b49630c5d7e2ecbeb00631d624a3c9de34368
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 263a38bd1c65
# EXP-Topic deprecated-profiles-in-ou-tab
[seda] Unlink deprecated profiles from OrganizationUnit using them

We only want "published" profiles to be linked to an OrganizationUnit.
There's already an RQL constraint on "use_profile" relation for that,
but upon change of the SEDA archive transfer workflow state, this
constraint is not fulfilled anymore. So add a hook that drops the
relation when state changes to "deprecated".

(CONSEJIRA-427)

diff --git a/cubicweb_saem_ref/hooks.py b/cubicweb_saem_ref/hooks.py
--- a/cubicweb_saem_ref/hooks.py
+++ b/cubicweb_saem_ref/hooks.py
@@ -465,6 +465,22 @@ class DepreciateProfileOnReplacePublishe
             wf.fire_transition_if_possible("deprecate")
 
 
+class UnlinkDeprecatedProfileFromOrganizationUnit(hook.Hook):
+    """Remove 'use_profile' relation of a SEDAArchiveTransfer when it gets
+    deprecated.
+    """
+    __select__ = (
+        hook.Hook.__select__
+        & on_fire_transition('SEDAArchiveTransfer', 'deprecate')
+    )
+    __regid__ = 'seda.unlink_deprecated_profile_from_organization_unit'
+    events = ('after_add_entity',)
+
+    def __call__(self):
+        self._cw.execute('DELETE O use_profile X WHERE X eid %(x)s',
+                         {'x': self.entity.for_entity.eid})
+
+
 # Relations deposit agent - archival agent  ####################################
 
 class DontDeleteUnitIfArchival(hook.Hook):
diff --git a/test/unittest_hooks.py b/test/unittest_hooks.py
--- a/test/unittest_hooks.py
+++ b/test/unittest_hooks.py
@@ -633,6 +633,21 @@ class SEDAArchiveTransferHooksTC(CubicWe
             profile.cw_clear_all_caches()
             self.assertEqual(workflow.state, 'deprecated')
 
+    def test_profile_deprecated_unlinked_from_organization_unit(self):
+        with self.admin_access.cnx() as cnx:
+            profile = testutils.setup_profile(cnx)
+            organization_unit = testutils.organization_unit(
+                cnx, u'org', archival_roles=[u'deposit'])
+            cnx.commit()
+            workflow = profile.cw_adapt_to('IWorkflowable')
+            workflow.fire_transition('publish')
+            cnx.commit()
+            organization_unit.cw_set(use_profile=profile)
+            cnx.commit()
+            workflow.fire_transition('deprecate')
+            cnx.commit()
+            self.assertFalse(organization_unit.use_profile)
+
 
 if __name__ == '__main__':
     unittest.main()



More information about the saem-devel mailing list