[PATCH 3 of 3 seda] [hooks] Synchronize user_cardinality of binary data object and its reference

Sylvain Thenault sylvain.thenault at logilab.fr
Wed Mar 29 11:11:11 CEST 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1490774051 -7200
#      Wed Mar 29 09:54:11 2017 +0200
# Node ID 76e6d3bd61454cf5dc56841270e8a04d38e2f2b9
# Parent  09efe180fc2c5c7522f6eadec30461641daed480
[hooks] Synchronize user_cardinality of binary data object and its reference

when in a simplified profile, because the ui only display bdo's cardinality.

Closes #17066567

diff --git a/cubicweb_seda/hooks.py b/cubicweb_seda/hooks.py
--- a/cubicweb_seda/hooks.py
+++ b/cubicweb_seda/hooks.py
@@ -231,10 +231,51 @@ class SetDefaultDataObjectRefCardinality
         parent = do_ref.seda_data_object_reference[0]
         if parent.cw_etype != 'SEDASeqAltArchiveUnitArchiveUnitRefIdManagement':
             do_ref.cw_set(user_cardinality=u'1')
 
 
+class SimplifiedProfileSyncDORefCardOnCreateHook(hook.Hook):
+    """Hook triggering an operation to keep in sync user_cardinality of data object reference and
+    its associated data object for simplified profiles, on creation of the relation between them.
+    """
+    __regid__ = 'seda.doref.default'
+    __select__ = hook.Hook.__select__ & hook.match_rtype('seda_data_object_reference_id')
+    events = ('after_add_relation',)
+
+    def __call__(self):
+        ref = self._cw.entity_from_eid(self.eidfrom)
+        bdo = self._cw.entity_from_eid(self.eidto)
+        if ref.cw_etype == 'SEDADataObjectReference' and bdo.cw_etype == 'SEDABinaryDataObject':
+            SimplifiedProfileSyncDORefCardOp(self._cw, do_ref=ref, do=bdo)
+
+
+class SimplifiedProfileSyncDORefCardOnUpdateHook(hook.Hook):
+    """Hook triggering an operation to keep in sync user_cardinality of data object reference and
+    its associated data object for simplified profiles, on update of the data object's cardinality.
+    """
+    __regid__ = 'seda.doref.default'
+    __select__ = hook.Hook.__select__ & is_instance('SEDABinaryDataObject')
+    events = ('after_update_entity',)
+
+    def __call__(self):
+        if 'user_cardinality' in self.entity.cw_edited:
+            references = self.entity.reverse_seda_data_object_reference_id
+            if len(references) == 1:  # else it can't be a simplified profile
+                SimplifiedProfileSyncDORefCardOp(self._cw, do_ref=references[0], do=self.entity)
+
+
+class SimplifiedProfileSyncDORefCardOp(hook.Operation):
+    """Keep in sync user_cardinality of data object reference and its associated data object for
+    simplified profiles.
+    """
+
+    def precommit_event(self):
+        container = self.do.seda_binary_data_object[0] if self.do.seda_binary_data_object else None
+        if container and container.simplified_profile:
+            self.do_ref.cw_set(user_cardinality=self.do.user_cardinality)
+
+
 class CheckProfileSEDACompatiblityOp(hook.DataOperationMixIn, hook.LateOperation):
     """Data operation that will check compatibility of a SEDA profile upon modification.
 
     This is a late operation since it has to be executed once the 'container' relation is set.
     """
diff --git a/test/test_hooks.py b/test/test_hooks.py
--- a/test/test_hooks.py
+++ b/test/test_hooks.py
@@ -163,10 +163,32 @@ class SetDefaultHooksTC(CubicWebTC):
             do_ref = cnx.create_entity('SEDADataObjectReference', seda_data_object_reference=alt2)
             cnx.commit()
 
             self.assertEqual(do_ref.user_cardinality, '1')
 
+    def test_simplified_profile_do_ref_sync(self):
+        """When creating a SEDABinaryDataObject in a simplified profile from the ui, one only
+        specifies user_cardinality for it, not for it's associated reference whose cardinality
+        should have the same value.
+        """
+        with self.admin_access.client_cnx() as cnx:
+            transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile',
+                                         simplified_profile=True)
+            unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(transfer)
+            bdo = testutils.create_data_object(unit_alt_seq, user_cardinality=u'1',
+                                               seda_binary_data_object=transfer)
+            cnx.commit()
+            ref = cnx.find('SEDADataObjectReference').one()
+
+            ref.cw_clear_all_caches()
+            self.assertEqual(ref.user_cardinality, '1')
+
+            bdo.cw_set(user_cardinality=u'1..n')
+            cnx.commit()
+            ref.cw_clear_all_caches()
+            self.assertEqual(ref.user_cardinality, '1..n')
+
 
 class CheckProfileTC(CubicWebTC):
 
     def test_base(self):
         with self.admin_access.repo_cnx() as cnx:


More information about the saem-devel mailing list