Retrieve default vocabularies for access/appraisal rule of archive unit component
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 20 Oct 2016 00:11:30 +0200
changeset 1943 e958e8bd54c6
parent 1926 cbcc11b64d08
child 1944 90595e088196
Retrieve default vocabularies for access/appraisal rule of archive unit component If this is validated, we will have handle this on import in a profile (check vocabulary consistency).
test/test_views.py
views/mgmt_rules.py
--- a/test/test_views.py	Thu Oct 20 00:10:02 2016 +0200
+++ b/test/test_views.py	Thu Oct 20 00:11:30 2016 +0200
@@ -23,14 +23,13 @@
 from cubicweb.web import INTERNAL_FIELD_VALUE
 
 from cubes.seda.xsd2yams import RULE_TYPES
-from cubes.seda.views import export, clone
+from cubes.seda.views import export, clone, mgmt_rules
 
-from testutils import create_transfer_to_bdo, create_archive_unit, create_data_object
+import testutils
 
 
 class ManagementRulesTC(CubicWebTC):
     def test_rule_ref_vocabulary(self):
-        from cubes.seda.views.mgmt_rules import _rule_ref_vocabulary
         with self.admin_access.client_cnx() as cnx:
             create = cnx.create_entity
 
@@ -38,7 +37,7 @@
             access_concept = access_scheme.add_concept(label=u'anyone')
             cnx.commit()
 
-            bdo = create_transfer_to_bdo(cnx)
+            bdo = testutils.create_transfer_to_bdo(cnx)
             transfer = bdo.container[0]
 
             rule_base = create('SEDAAccessRule', seda_access_rule=transfer)
@@ -47,10 +46,10 @@
             cnx.create_entity('SEDAPreventInheritance', seda_prevent_inheritance=rule_alt)
             cnx.commit()
 
-            self.assertEqual(_rule_ref_vocabulary(transfer, rule_base.cw_etype),
+            self.assertEqual(mgmt_rules._rule_ref_vocabulary(transfer, rule_base.cw_etype),
                              [('you must specify a scheme for seda_access_rule_code_list_version_'
                                'from_object to select a value', INTERNAL_FIELD_VALUE)])
-            self.assertEqual(_rule_ref_vocabulary(transfer, rule_alt.cw_etype),
+            self.assertEqual(mgmt_rules._rule_ref_vocabulary(transfer, rule_alt.cw_etype),
                              [('you must specify a scheme for seda_access_rule_code_list_version_'
                                'from_object to select a value', INTERNAL_FIELD_VALUE)])
 
@@ -59,12 +58,27 @@
                    seda_access_rule_code_list_version_to=access_scheme)
             cnx.commit()
 
-            self.assertEqual(_rule_ref_vocabulary(transfer, rule_base.cw_etype),
+            self.assertEqual(mgmt_rules._rule_ref_vocabulary(transfer, rule_base.cw_etype),
+                             [('<no value specified>', '__cubicweb_internal_field__'),
+                              (access_concept.label(), text_type(access_concept.eid))])
+            self.assertEqual(mgmt_rules._rule_ref_vocabulary(transfer, rule_alt.cw_etype),
                              [('<no value specified>', '__cubicweb_internal_field__'),
                               (access_concept.label(), text_type(access_concept.eid))])
-            self.assertEqual(_rule_ref_vocabulary(transfer, rule_alt.cw_etype),
-                             [('<no value specified>', '__cubicweb_internal_field__'),
-                              (access_concept.label(), text_type(access_concept.eid))])
+
+    def test_archive_unit_component_rule_ref_vocabulary(self):
+        with self.admin_access.client_cnx() as cnx:
+            unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(None, cnx=cnx)
+            for rule_type in ('access', 'appraisal'):
+                etype = 'SEDASeq{}RuleRule'.format(rule_type.capitalize())
+                scheme = testutils.scheme_for_type(cnx, 'seda_rule', etype)
+                concept = scheme.add_concept(label=u'whatever')
+                cnx.commit()
+
+                unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(None, cnx=cnx)
+
+                self.assertEqual(mgmt_rules._rule_ref_vocabulary(unit_alt_seq, etype),
+                                 [('<no value specified>', '__cubicweb_internal_field__'),
+                                  ('whatever', text_type(concept.eid))])
 
 
 class PermissionsTC(CubicWebTC):
@@ -212,7 +226,7 @@
     def test_diagnose_tab(self):
         with self.admin_access.web_request() as req:
             transfer = req.create_entity('SEDAArchiveTransfer', title=u'diagnosis testing')
-            unit, unit_alt, unit_alt_seq = create_archive_unit(transfer)
+            unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(transfer)
             req.cnx.commit()
             # ensure the diagnosis tab display correctly
             self.view('seda_at_diagnose_tab', req=req, rset=transfer.as_rset())
@@ -224,7 +238,7 @@
         with self.admin_access.web_request() as req:
             transfer = req.create_entity('SEDAArchiveTransfer', title=u'diagnosis testing',
                                          simplified_profile=True)
-            unit, unit_alt, unit_alt_seq = create_archive_unit(transfer)
+            unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(transfer)
             req.cnx.commit()
 
             exporter = self.vreg['views'].select('seda.export', req, entity=transfer)
@@ -258,7 +272,7 @@
         with self.admin_access.repo_cnx() as cnx:
             transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'diagnosis testing',
                                          simplified_profile=True)
-            archive_unit = create_archive_unit(transfer)[0]
+            archive_unit = testutils.create_archive_unit(transfer)[0]
 
             cnx.commit()
             self.transfer_eid = transfer.eid
@@ -368,7 +382,7 @@
 
         with self.admin_access.web_request() as req:
             transfer = req.create_entity('SEDAArchiveTransfer', title=u'test')
-            unit = create_archive_unit(transfer)[0]
+            unit = testutils.create_archive_unit(transfer)[0]
             req.cnx.commit()
             actions = self.pactionsdict(req, transfer.as_rset())
             self.assertIn(clone.ImportSEDAArchiveUnitAction, actions['moreactions'])
@@ -394,10 +408,10 @@
             #     user_annotation=u'plop',
             #     seda_document_type_code=doctypecode).eid
             transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test')
-            unit, unit_alt, unit_alt_seq = create_archive_unit(None, cnx=cnx,
-                                                               user_cardinality=u'1',
-                                                               user_annotation=u'plop')
-            bdo = create_data_object(None, cnx=cnx, filename=u'file.txt')
+            unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(None, cnx=cnx,
+                                                                         user_cardinality=u'1',
+                                                                         user_annotation=u'plop')
+            bdo = testutils.create_data_object(None, cnx=cnx, filename=u'file.txt')
             cnx.create_entity('SEDADataObjectReference', user_cardinality=u'0..n',
                               seda_data_object_reference=unit_alt_seq,
                               seda_data_object_reference_id=bdo)
@@ -441,10 +455,9 @@
 
     def test_import_multiple_entities(self):
         with self.admin_access.cnx() as cnx:
-            unit, unit_alt, unit_alt_seq = create_archive_unit(None, cnx=cnx,
-                                                               id=u'new',
-                                                               user_cardinality=u'0..1',
-                                                               user_annotation=u'plouf')
+            unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(
+                None, cnx=cnx, id=u'new',
+                user_cardinality=u'0..1', user_annotation=u'plouf')
             cnx.commit()
         to_clone = [self.unit_eid, unit.eid]
         params = dict(eid=text_type(self.transfer_eid),
@@ -478,7 +491,7 @@
     def test_archiveunit_reparent_to_transfer(self):
         with self.admin_access.cnx() as cnx:
             transfer = cnx.entity_from_eid(self.transfer_eid)
-            archunit, _, _ = create_archive_unit(transfer)
+            archunit, _, _ = testutils.create_archive_unit(transfer)
             transfer2 = cnx.create_entity('SEDAArchiveTransfer',
                                           title=u'Test2',
                                           simplified_profile=True)
@@ -493,8 +506,8 @@
     def test_archiveunit_reparent_to_archiveunit(self):
         with self.admin_access.cnx() as cnx:
             transfer = cnx.entity_from_eid(self.transfer_eid)
-            archunit, _, _ = create_archive_unit(transfer)
-            archunit2, _, _ = create_archive_unit(transfer, id=u'pouet')
+            archunit, _, _ = testutils.create_archive_unit(transfer)
+            archunit2, _, _ = testutils.create_archive_unit(transfer, id=u'pouet')
             cnx.commit()
             archunit.cw_clear_all_caches()
             archunit.cw_adapt_to('IJQTree').reparent(archunit2.eid)
@@ -507,9 +520,9 @@
     def test_binarydataobject_reparent(self):
         with self.admin_access.cnx() as cnx:
             transfer = cnx.entity_from_eid(self.transfer_eid)
-            archunit, _, alt_seq = create_archive_unit(transfer)
-            archunit2, _, alt_seq2 = create_archive_unit(transfer, id=u'pouet')
-            bdo = create_data_object(transfer)
+            archunit, _, alt_seq = testutils.create_archive_unit(transfer)
+            archunit2, _, alt_seq2 = testutils.create_archive_unit(transfer, id=u'pouet')
+            bdo = testutils.create_data_object(transfer)
             ref = cnx.create_entity('SEDADataObjectReference',
                                     seda_data_object_reference=alt_seq,
                                     seda_data_object_reference_id=bdo)
--- a/views/mgmt_rules.py	Thu Oct 20 00:10:02 2016 +0200
+++ b/views/mgmt_rules.py	Thu Oct 20 00:11:30 2016 +0200
@@ -52,10 +52,18 @@
 def _rule_ref_vocabulary(container, parent_etype):
     req = container._cw
     rule_type = rule_type_from_etype(parent_etype)
-    rql = ('Any C WHERE C in_scheme CS, AT eid %(at)s, '
-           'CACLV seda_{0}_rule_code_list_version_from AT, '
-           'CACLV seda_{0}_rule_code_list_version_to CS'.format(rule_type))
-    rset = req.execute(rql, {'at': container.eid})
+    if container.cw_etype == 'SEDAArchiveTransfer':
+        rql = ('Any C WHERE C in_scheme CS, AT eid %(at)s, '
+               'CACLV seda_{0}_rule_code_list_version_from AT, '
+               'CACLV seda_{0}_rule_code_list_version_to CS'.format(rule_type))
+        rset = req.execute(rql, {'at': container.eid})
+    else:
+        # component archive unit, suppose we can use default vocabularies
+        assert rule_type in ('access', 'appraisal')
+        rql = ('Any C WHERE C in_scheme CS, CS scheme_relation_type RT, RT name %(rt)s,'
+               'CS scheme_entity_type ET, ET name %(et)s')
+        etype = 'SEDASeq{0}RuleRule'.format(rule_type.capitalize())
+        rset = req.execute(rql, {'rt': 'seda_rule', 'et': etype})
     if rset:
         return ([(req._('<no value specified>'), INTERNAL_FIELD_VALUE)]
                 + sorted([(concept.label(), text_type(concept.eid))