Set default code list for appraisal and access rules at profile creation time
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 30 Sep 2016 14:14:40 +0200
changeset 1606 e097834d5292
parent 1597 d39735336352
child 1607 d242e37f1bf8
Set default code list for appraisal and access rules at profile creation time Now that we've proper vocabularies for them.
hooks.py
test/test_hooks.py
test/test_profile_generation.py
test/testutils.py
--- a/hooks.py	Wed Sep 28 16:21:09 2016 +0200
+++ b/hooks.py	Fri Sep 30 14:14:40 2016 +0200
@@ -194,17 +194,18 @@
         ('seda_file_format_code_list_version', 'seda_format_id_to'),
         # ('seda_message_digest_algorithm_code_list_version'),
     ]
+    # XXX factorize with data structure in dataimport
     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_mime_type_code_list_version', 'seda_mime_type_to', None),
+        ('seda_encoding_code_list_version', 'seda_encoding_to', None),
+        ('seda_data_object_version_code_list_version', 'seda_data_object_version_to', None),
+        ('seda_relationship_code_list_version', 'seda_type_relationship', None),
+        ('seda_access_rule_code_list_version', 'seda_rule', 'SEDASeqAccessRuleRule'),
+        ('seda_appraisal_rule_code_list_version', 'seda_rule', 'SEDASeqAppraisalRuleRule'),
         # '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',
     ]
 
@@ -215,13 +216,15 @@
             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:
+        for rtype, ref_rtype, ref_etype 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})
+            rql = ('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')
+            if ref_etype is not None:
+                rql += ', CS scheme_entity_type ET, ET name %(et)s'
+            cnx.execute(rql.format(etype=etype, rtype=rtype),
+                        {'t': transfer, 'rt': ref_rtype, 'et': ref_etype})
 
 
 def registration_callback(vreg):
--- a/test/test_hooks.py	Wed Sep 28 16:21:09 2016 +0200
+++ b/test/test_hooks.py	Fri Sep 30 14:14:40 2016 +0200
@@ -15,10 +15,12 @@
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 """cubicweb-seda unit tests for hooks"""
 
+from itertools import chain, repeat
+
 from cubicweb import ValidationError
 from cubicweb.devtools.testlib import CubicWebTC
 
-from testutils import create_transfer_to_bdo, create_archive_unit, scheme_for_rtype
+from testutils import create_transfer_to_bdo, create_archive_unit, scheme_for_type
 
 
 class ValidationHooksTC(CubicWebTC):
@@ -176,8 +178,12 @@
 
     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)
+            for rtype, etype in chain(zip(('seda_format_id_to', 'seda_mime_type_to',
+                                           'seda_encoding_to'),
+                                          repeat(None)),
+                                      [('seda_rule', 'SEDASeqAppraisalRuleRule'),
+                                       ('seda_rule', 'SEDASeqAccessRuleRule')]):
+                scheme_for_type(cnx, rtype, etype)
             cnx.commit()
             transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
             cnx.commit()
@@ -185,6 +191,8 @@
             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)
+            self.assertTrue(transfer.reverse_seda_access_rule_code_list_version_from)
+            self.assertTrue(transfer.reverse_seda_appraisal_rule_code_list_version_from)
 
 
 if __name__ == '__main__':
--- a/test/test_profile_generation.py	Wed Sep 28 16:21:09 2016 +0200
+++ b/test/test_profile_generation.py	Fri Sep 30 14:14:40 2016 +0200
@@ -380,7 +380,7 @@
                                                'minOccurs': '0'})
             self.assertXSDAttributes(
                 kwt,
-                [{'name': 'listVersionID', 'fixed': 'seda_keyword_type_to vocabulary'}])
+                [{'name': 'listVersionID', 'fixed': 'seda_keyword_type_to/None vocabulary'}])
             kwr = self.get_element(profile, 'KeywordReference')
             self.assertElementDefinition(kwr, {'name': 'KeywordReference',
                                                'type': 'xsd:token',
--- a/test/testutils.py	Wed Sep 28 16:21:09 2016 +0200
+++ b/test/testutils.py	Fri Sep 30 14:14:40 2016 +0200
@@ -49,15 +49,22 @@
     return bdo
 
 
-def map_cs_to_rtype(scheme, rtype):
+def map_cs_to_type(scheme, rtype, etype=None):
     cnx = scheme._cw
     cnx.execute('SET CS scheme_relation_type RT WHERE CS eid %(cs)s, RT name %(rt)s',
                 {'cs': scheme.eid, 'rt': rtype})
+    if etype is not None:
+        cnx.execute('SET CS scheme_entity_type ET WHERE CS eid %(cs)s, ET name %(et)s',
+                    {'cs': scheme.eid, 'et': etype})
+
+
+def scheme_for_type(cnx, rtype, etype, *concept_labels):
+    scheme = cnx.create_entity('ConceptScheme', title=u'{0}/{1} vocabulary'.format(rtype, etype))
+    map_cs_to_type(scheme, rtype, etype)
+    for label in concept_labels:
+        scheme.add_concept(label)
+    return scheme
 
 
 def scheme_for_rtype(cnx, rtype, *concept_labels):
-    scheme = cnx.create_entity('ConceptScheme', title=u'{0} vocabulary'.format(rtype))
-    map_cs_to_rtype(scheme, rtype)
-    for label in concept_labels:
-        scheme.add_concept(label)
-    return scheme
+    return scheme_for_type(cnx, rtype, None, *concept_labels)