[ux] Preset some code list version
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 08 Jul 2016 12:16:01 +0200
changeset 1369 fe4825937b4b
parent 1368 ef09c56b211b
child 1370 365d91dcf2b5
[ux] Preset some code list version At transfer creation time, attempt to predefine some code lists by querying the scheme_relation_type relation. Those depending on both a relation type and an entity type are not yet handled (we've no default list for those anyway). Closes #14157192
hooks.py
test/test_hooks.py
--- a/hooks.py	Thu Jul 07 19:15:06 2016 +0200
+++ b/hooks.py	Fri Jul 08 12:16:01 2016 +0200
@@ -173,6 +173,54 @@
         EnsureChoiceNotEmptyOp.get_instance(self._cw).add_data(choice)
 
 
+class SetDefaultCodeListVersionsHook(hook.Hook):
+    """Hook triggering an operation to set sensible default values for a transfer's code list
+    versions.
+    """
+    __regid__ = 'seda.transfer.default-code-lists'
+    __select__ = hook.Hook.__select__ & is_instance('SEDAArchiveTransfer')
+    events = ('after_add_entity',)
+
+    def __call__(self):
+        SetDefaultCodeListVersionsOp(self._cw, transfer_eid=self.entity.eid)
+
+
+class SetDefaultCodeListVersionsOp(hook.DataOperationMixIn, hook.Operation):
+    """Set sensible default values for a transfer's code list versions."""
+    simple_rtypes = [
+        ('seda_file_format_code_list_version', 'seda_format_id_to'),
+        # ('seda_message_digest_algorithm_code_list_version'),
+    ]
+    complex_rtypes = [
+        ('seda_mime_type_code_list_version', 'seda_mime_type_to'),
+        ('seda_encoding_code_list_version', 'seda_encoding_to'),
+        ('seda_data_object_version_code_list_version', 'seda_data_object_version_to'),
+        ('seda_relationship_code_list_version', 'seda_type_relationship'),
+        # 'seda_compression_algorithm_code_list_version',
+        # 'seda_classification_rule_code_list_version',
+        # 'seda_reuse_rule_code_list_version',
+        # 'seda_dissemination_rule_code_list_version',
+        # 'seda_access_rule_code_list_version',
+        # 'seda_appraisal_rule_code_list_version',
+        # 'seda_storage_rule_code_list_version',
+    ]
+
+    def precommit_event(self):
+        transfer = self.transfer_eid
+        cnx = self.cnx
+        for rtype, ref_rtype in self.simple_rtypes:
+            cnx.execute('SET T {rtype} CS WHERE NOT T {rtype} CS, T eid %(t)s, '
+                        'CS scheme_relation_type RT, RT name %(rt)s'.format(rtype=rtype),
+                        {'t': transfer, 'rt': ref_rtype})
+        for rtype, ref_rtype in self.complex_rtypes:
+            etype = 'SEDA' + ''.join(word.capitalize() for word in rtype.split('_')[1:])
+            cnx.execute('INSERT {etype} X: X {rtype}_from T, X {rtype}_to CS '
+                        'WHERE NOT Y {rtype}_from T, T eid %(t)s, '
+                        'CS scheme_relation_type RT, RT name %(rt)s'.format(etype=etype,
+                                                                            rtype=rtype),
+                        {'t': transfer, 'rt': ref_rtype})
+
+
 def registration_callback(vreg):
     from cubicweb.server import ON_COMMIT_ADD_RELATIONS
     from cubes.seda import seda_profile_container_def, iter_all_rdefs
--- a/test/test_hooks.py	Thu Jul 07 19:15:06 2016 +0200
+++ b/test/test_hooks.py	Fri Jul 08 12:16:01 2016 +0200
@@ -19,6 +19,7 @@
 from cubicweb.devtools.testlib import CubicWebTC
 
 from test_schema import create_transfer_to_bdo
+from test_profile_generation import scheme_for_rtype
 
 
 class ValidationHooksTC(CubicWebTC):
@@ -188,6 +189,21 @@
             cnx.commit()
 
 
+class SetDefaultHooksTC(CubicWebTC):
+
+    def test_default_code_list_version(self):
+        with self.admin_access.client_cnx() as cnx:
+            for rtype in ('seda_format_id_to', 'seda_mime_type_to', 'seda_encoding_to'):
+                scheme_for_rtype(cnx, rtype)
+            cnx.commit()
+            transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
+            cnx.commit()
+
+            self.assertTrue(transfer.seda_file_format_code_list_version)
+            self.assertTrue(transfer.reverse_seda_mime_type_code_list_version_from)
+            self.assertTrue(transfer.reverse_seda_encoding_code_list_version_from)
+
+
 if __name__ == '__main__':
     import unittest
     unittest.main()