[hooks] Ensure data object references have cardinality=1 in the context of typed references
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 06 Jan 2017 14:08:07 +0100
changeset 2258 9ee61b23011c
parent 2257 2d824356706d
child 2259 c53a0d875814
[hooks] Ensure data object references have cardinality=1 in the context of typed references In this case, the cardinality is hidden in the web ui, we hence get the schema default 0..1 cardinality which is not expected.
hooks.py
test/test_hooks.py
--- a/hooks.py	Fri Jan 06 14:20:19 2017 +0100
+++ b/hooks.py	Fri Jan 06 14:08:07 2017 +0100
@@ -242,6 +242,28 @@
                         {'t': transfer, 'rt': ref_rtype, 'et': ref_etype})
 
 
+class SetDefaultDataObjectRefCardinalityHook(hook.Hook):
+    """Hook triggering an operation to set cardinality to 1 on creation of a data object 'typed'
+    reference.
+    """
+    __regid__ = 'seda.doref.default'
+    __select__ = hook.Hook.__select__ & is_instance('SEDADataObjectReference')
+    events = ('after_add_entity',)
+
+    def __call__(self):
+        SetDefaultDataObjectRefCardinalityOp(self._cw, do_ref_eid=self.entity.eid)
+
+
+class SetDefaultDataObjectRefCardinalityOp(hook.Operation):
+    """Set cardinality to 1 on creation of a data object 'typed' reference."""
+
+    def precommit_event(self):
+        do_ref = self.cnx.entity_from_eid(self.do_ref_eid)
+        parent = do_ref.seda_data_object_reference[0]
+        if parent.cw_etype != 'SEDASeqAltArchiveUnitArchiveUnitRefIdManagement':
+            do_ref.cw_set(user_cardinality=u'1')
+
+
 class CheckProfileSEDACompatiblityOp(hook.DataOperationMixIn, hook.LateOperation):
     """Data operation that will check compatibility of a SEDA profile upon modification.
 
--- a/test/test_hooks.py	Fri Jan 06 14:20:19 2017 +0100
+++ b/test/test_hooks.py	Fri Jan 06 14:08:07 2017 +0100
@@ -194,6 +194,24 @@
             self.assertTrue(transfer.reverse_seda_access_rule_code_list_version_from)
             self.assertTrue(transfer.reverse_seda_appraisal_rule_code_list_version_from)
 
+    def test_default_card_on_typed_data_object_ref(self):
+        """When creating a SEDADataObjectReference in the context of a reference, its cardinality
+        should always be 1 (by default any card is allowed since in may be used in the context of
+        'main' reference).
+        """
+        with self.admin_access.client_cnx() as cnx:
+            transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
+            unit, unit_alt, unit_alt_seq = create_archive_unit(transfer)
+            content = cnx.create_entity('SEDAContent', seda_content=unit_alt_seq)
+            cnx.create_entity('SEDATitle', seda_title=content)
+            version_of = cnx.create_entity('SEDAIsVersionOf', seda_is_version_of=content)
+            alt2 = cnx.create_entity('SEDAAltIsVersionOfArchiveUnitRefId',
+                                     reverse_seda_alt_is_version_of_archive_unit_ref_id=version_of)
+            do_ref = cnx.create_entity('SEDADataObjectReference', seda_data_object_reference=alt2)
+            cnx.commit()
+
+            self.assertEqual(do_ref.user_cardinality, '1')
+
 
 class CheckProfileTC(CubicWebTC):