[PATCH 02 of 11 saem_ref] [ark] Use a unique prefix for ARK identifier

Denis Laxalde denis.laxalde at logilab.fr
Mon Feb 12 15:20:38 CET 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1518168835 -3600
#      Fri Feb 09 10:33:55 2018 +0100
# Node ID 5008f846f60b21a217149cd4f7e27a3228ff387b
# Parent  b5a5344246ce56b0496c48260fd71b52a3237f9c
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 5008f846f60b
# EXP-Topic ark/prefix-suffix
[ark] Use a unique prefix for ARK identifier

In Extranet #46881387, it was decided to get rid of custom (semantic)
prefixes in ARK identifier as this does not conform to "best practices".
Instead (since a prefix is still a good practice, but should be
site-wide), we want to use the "rf" prefix.

As a consequence, IARKGenerator does not need the entity type anymore to
build ARK identifiers. Respectively, we update set_ark_and_cwuri() in
hooks.

Related to extranet #46881387.

diff --git a/cubicweb_saem_ref/entities/__init__.py b/cubicweb_saem_ref/entities/__init__.py
--- a/cubicweb_saem_ref/entities/__init__.py
+++ b/cubicweb_saem_ref/entities/__init__.py
@@ -22,6 +22,12 @@ from cubicweb.predicates import match_kw
 from cubicweb.entities import AnyEntity, fetch_config, authobjs, lib
 
 
+
+
+def make_ark(name):
+    return u'rf{name}'.format(name=name)
+
+
 class ARKGeneratorMixIn(object):
     """Entity adapter for ARK unique identifier generation"""
     __abstract__ = True
@@ -46,28 +52,18 @@ class ARKExtIdentifierGenerator(ARKGener
         for sql in dbh.sqls_increment_sequence('ext_ark_count'):
             cu = self._cw.system_sql(sql)
         count = cu.fetchone()[0]
-        return u'a{0:09d}'.format(count)
+        return make_ark(u'{0:09d}'.format(count))
 
 
 class ARKCWIdentifierGenerator(ARKGeneratorMixIn, Adapter):
     """saem_ref.IARKGenerator entity adapter generating ARK like identifier during non-production
     phase.
     """
-    __select__ = ARKGeneratorMixIn.__select__ & match_kwargs('eid', 'etype')
+    __select__ = ARKGeneratorMixIn.__select__ & match_kwargs('eid')
 
     def assign_name(self):
         eid = self.cw_extra_kwargs['eid']
-        etype = self.cw_extra_kwargs['etype']
-        prefix = {
-            'AuthorityRecord': u'r',
-            'SEDAArchiveTransfer': u'p',
-            'Concept': u'c',
-            'ConceptScheme': u'v',
-            'Organization': u'o',
-            'Agent': u'oa',
-            'OrganizationUnit': u'ou',
-        }.get(etype, u'')
-        return u'{0}{1:09d}'.format(prefix, eid)
+        return make_ark(u'{0:09d}'.format(eid))
 
 
 class ArkNAALocator(EntityAdapter):
diff --git a/cubicweb_saem_ref/hooks.py b/cubicweb_saem_ref/hooks.py
--- a/cubicweb_saem_ref/hooks.py
+++ b/cubicweb_saem_ref/hooks.py
@@ -208,11 +208,11 @@ class AssignARKHook(hook.Hook):
             naa_what = self.entity.cw_adapt_to('IArkNAALocator').naa_what()
         else:
             naa_what = None
-        set_ark_and_cwuri(self._cw, self.entity.eid, self.entity.cw_etype,
+        set_ark_and_cwuri(self._cw, self.entity.eid,
                           self.entity.cw_edited, naa_what=naa_what)
 
 
-def set_ark_and_cwuri(cw, eid, etype, entity_attrs, naa_what=None):
+def set_ark_and_cwuri(cw, eid, entity_attrs, naa_what=None):
     ark = entity_attrs.get('ark')
     if not ark:
         cwuri = entity_attrs.get('cwuri')
@@ -222,7 +222,7 @@ def set_ark_and_cwuri(cw, eid, etype, en
                 msg = _('an ARK identifier has to be generated but no Name Assigning Authority is '
                         'specified')
                 raise ValidationError(None, {None: msg})
-            generator = cw.vreg['adapters'].select('IARKGenerator', cw, eid=eid, etype=etype,
+            generator = cw.vreg['adapters'].select('IARKGenerator', cw, eid=eid,
                                                    naa_what=naa_what)
             ark = generator.generate_ark()
         entity_attrs['ark'] = ark
diff --git a/cubicweb_saem_ref/migration/0.14.3_Any.py b/cubicweb_saem_ref/migration/0.14.3_Any.py
--- a/cubicweb_saem_ref/migration/0.14.3_Any.py
+++ b/cubicweb_saem_ref/migration/0.14.3_Any.py
@@ -12,7 +12,7 @@ def fix_organization_ark(cnx):
                 # but at least it will appear in the UI
                 naa_what = u'XXX'
             attrs = {}
-            set_ark_and_cwuri(cnx, org.eid, 'Organization', attrs, naa_what=naa_what)
+            set_ark_and_cwuri(cnx, org.eid, attrs, naa_what=naa_what)
             org.cw_set(**attrs)
         cnx.commit()
 
diff --git a/cubicweb_saem_ref/sobjects/__init__.py b/cubicweb_saem_ref/sobjects/__init__.py
--- a/cubicweb_saem_ref/sobjects/__init__.py
+++ b/cubicweb_saem_ref/sobjects/__init__.py
@@ -61,7 +61,7 @@ class SAEMMetadataGenerator(MetadataGene
 
     def gen_ark(self, etype, eid, attrs):
         """ARK generation callback"""
-        set_ark_and_cwuri(self._cnx, eid, etype, attrs, naa_what=self.naa_what)
+        set_ark_and_cwuri(self._cnx, eid, attrs, naa_what=self.naa_what)
         # returning value, even if set by the function, is necessary due to the meta generator
         # implementation
         return attrs['ark']
diff --git a/test/test_ccplugin.py b/test/test_ccplugin.py
--- a/test/test_ccplugin.py
+++ b/test/test_ccplugin.py
@@ -77,7 +77,7 @@ class ImportEacDataCommandTC(CCPluginTC)
         with self.admin_access.repo_cnx() as cnx:
             record = cnx.find('AuthorityRecord').one()
             self.assertEqual(record.dc_title(), u"Centre d'information et d'orientation (CIO)")
-            self.assertTrue(record.ark.startswith('0/r0'), record.ark)
+            self.assertTrue(record.ark.startswith('0/rf0'), record.ark)
 
     def test_bad_authority(self):
         code, output = self.run_import_eac('--authority', 'unexisting')
diff --git a/test/test_seda.py b/test/test_seda.py
--- a/test/test_seda.py
+++ b/test/test_seda.py
@@ -121,7 +121,7 @@ class CloneImportTC(CubicWebTC):
         with self.admin_access.web_request(**params) as req:
             path, _ = self.expect_redirect_handle_request(
                 req, 'seda.doimport')
-            match = re.match(r'^ark:/0/p0*(\d+)$', path)
+            match = re.match(r'^ark:/0/rf0*(\d+)$', path)
             assert match, path
             eid = int(match.group(1))
             rset = req.execute('Any X WHERE X seda_archive_unit P, P eid %(p)s',
diff --git a/test/test_views.py b/test/test_views.py
--- a/test/test_views.py
+++ b/test/test_views.py
@@ -329,7 +329,7 @@ class ArkViewsTC(CubicWebTC):
             path = self.expect_redirect_handle_request(req)[0]
             eid = eid_from_ark(path)
             record = req.cnx.entity_from_eid(eid)
-            self.assertEqual(record.ark, u'0/r%09d' % eid)
+            self.assertEqual(record.ark, u'0/rf%09d' % eid)
 
     def test_ark_agent_creation(self):
         with self.admin_access.web_request() as req:
@@ -343,7 +343,7 @@ class ArkViewsTC(CubicWebTC):
             path = self.expect_redirect_handle_request(req)[0]
             eid = eid_from_ark(path)
             agent = req.cnx.entity_from_eid(eid)
-            self.assertEqual(agent.ark, u'0/oa%09d' % eid)
+            self.assertEqual(agent.ark, u'0/rf%09d' % eid)
 
     def test_ark_scheme_creation(self):
         with self.admin_access.web_request() as req:
@@ -355,7 +355,7 @@ class ArkViewsTC(CubicWebTC):
             path = self.expect_redirect_handle_request(req)[0]
             eid = eid_from_ark(path)
             scheme = req.cnx.entity_from_eid(eid)
-            self.assertEqual(scheme.ark, u'0/v%09d' % eid)
+            self.assertEqual(scheme.ark, u'0/rf%09d' % eid)
 
     def test_ark_concept_creation_form(self):
         # test addition of a concept by specifying in_scheme in form
@@ -374,7 +374,7 @@ class ArkViewsTC(CubicWebTC):
             path = self.expect_redirect_handle_request(req)[0]
             eid = eid_from_ark(path)
             concept = req.cnx.entity_from_eid(eid)
-            self.assertEqual(concept.ark, u'0/c%09d' % eid)
+            self.assertEqual(concept.ark, u'0/rf%09d' % eid)
 
     def test_ark_concept_creation_linkto(self):
         # test addition of a concept by specifying in_scheme with __linkto
@@ -393,7 +393,7 @@ class ArkViewsTC(CubicWebTC):
             path = self.expect_redirect_handle_request(req)[0]
             eid = eid_from_ark(path)
             concept = req.cnx.entity_from_eid(eid)
-            self.assertEqual(concept.ark, u'0/c%09d' % eid)
+            self.assertEqual(concept.ark, u'0/rf%09d' % eid)
 
     def test_ark_url_rewrite(self):
         with self.admin_access.web_request() as req:
@@ -473,14 +473,14 @@ class AssignArkWebServiceTC(CubicWebTC):
                 method='POST', **params) as req:
             result = self.app_handle_request(req, 'ark')
             self.assertEqual(json.loads(result),
-                             [{'ark': '0/a000000001'}])
+                             [{'ark': '0/rf000000001'}])
         params = {'organization': 'ark:/' + org_ark}
         with self.admin_access.web_request(
                 headers={'Accept': 'application/json'},
                 method='POST', **params) as req:
             result = self.app_handle_request(req, 'ark')
             self.assertEqual(json.loads(result),
-                             [{'ark': '0/a000000002'}])
+                             [{'ark': '0/rf000000002'}])
 
 
 class ListArchivalUnitsServiceTC(CubicWebTC):
diff --git a/test/unittest_entities.py b/test/unittest_entities.py
--- a/test/unittest_entities.py
+++ b/test/unittest_entities.py
@@ -21,25 +21,16 @@ class ArkGeneratorTC(CubicWebTC):
 
     def test_eid(self):
         with self.admin_access.repo_cnx() as cnx:
-            for etype, expected in (
-                ("AuthorityRecord", u"r000000042"),
-                ("SEDAArchiveTransfer", u"p000000042"),
-                ("Concept", u"c000000042"),
-                ("ConceptScheme", u"v000000042"),
-                ("Organization", u"o000000042"),
-                ("OrganizationUnit", u"ou000000042"),
-                ("Agent", u"oa000000042"),
-                ("CWUser", u"000000042"),
-            ):
-                generator = self.vreg['adapters'].select('IARKGenerator', cnx, eid=42, etype=etype,
-                                                         naa_what='saemref-test')
-                self.assertEqual(generator.generate_ark(), 'saemref-test/{0}'.format(expected))
+            generator = self.vreg['adapters'].select(
+                'IARKGenerator', cnx, eid=42, naa_what='saemref-test')
+            self.assertEqual(generator.generate_ark(),
+                             'saemref-test/rf000000042')
 
     def test_ext_identifier(self):
         with self.admin_access.repo_cnx() as cnx:
             generator = self.vreg['adapters'].select('IARKGenerator', cnx,
                                                      naa_what='saemref-test')
-            self.assertEqual(generator.generate_ark(), 'saemref-test/a000000001')
+            self.assertEqual(generator.generate_ark(), 'saemref-test/rf000000001')
 
 
 if __name__ == '__main__':
diff --git a/test/unittest_hooks.py b/test/unittest_hooks.py
--- a/test/unittest_hooks.py
+++ b/test/unittest_hooks.py
@@ -205,22 +205,22 @@ class SAEMRefHooksTC(CubicWebTC):
         with self.admin_access.repo_cnx() as cnx:
             agent = testutils.authority_record(cnx, u'bob')
             cnx.commit()
-            self.assertEqual(agent.ark, '0/r%09d' % agent.eid)
-            self.assertEqual(agent.cwuri, 'ark:/0/r%09d' % agent.eid)
+            self.assertEqual(agent.ark, '0/rf%09d' % agent.eid)
+            self.assertEqual(agent.cwuri, 'ark:/0/rf%09d' % agent.eid)
             agent = testutils.authority_record(cnx, u'john', ark=u'authority/123456')
             cnx.commit()
             self.assertEqual(agent.ark, 'authority/123456')
             self.assertEqual(agent.cwuri, 'ark:/authority/123456')
             agent = testutils.authority_record(cnx, u'alf', cwuri=u'http://someuri/someagent')
             cnx.commit()
-            self.assertEqual(agent.ark, '0/r%09d' % agent.eid)
+            self.assertEqual(agent.ark, '0/rf%09d' % agent.eid)
             self.assertEqual(agent.cwuri, 'http://someuri/someagent')
 
     def test_ark_generation_seda_profile(self):
         with self.admin_access.repo_cnx() as cnx:
             profile = testutils.setup_profile(cnx)
-            self.assertEqual(profile.ark, '0/p{0:09d}'.format(profile.eid))
-            self.assertEqual(profile.cwuri, 'ark:/0/p{0:09d}'.format(profile.eid))
+            self.assertEqual(profile.ark, '0/rf{0:09d}'.format(profile.eid))
+            self.assertEqual(profile.cwuri, 'ark:/0/rf{0:09d}'.format(profile.eid))
 
     def test_ark_generation_seda_profile_ark_given(self):
         with self.admin_access.repo_cnx() as cnx:
@@ -232,7 +232,7 @@ class SAEMRefHooksTC(CubicWebTC):
         with self.admin_access.repo_cnx() as cnx:
             profile = testutils.setup_profile(
                 cnx, cwuri=u'http://example.org/profile/125')
-            self.assertEqual(profile.ark, '0/p{0:09d}'.format(profile.eid))
+            self.assertEqual(profile.ark, '0/rf{0:09d}'.format(profile.eid))
             self.assertEqual(profile.cwuri, 'http://example.org/profile/125')
 
     def test_ark_generation_concept(self):
@@ -240,19 +240,19 @@ class SAEMRefHooksTC(CubicWebTC):
             scheme = cnx.create_entity('ConceptScheme', ark_naa=testutils.naa(cnx))
             concept = scheme.add_concept(u'some object')
             cnx.commit()
-            self.assertEqual(scheme.ark, '0/v%09d' % scheme.eid)
+            self.assertEqual(scheme.ark, '0/rf%09d' % scheme.eid)
             self.assertEqual(scheme.cwuri,
-                             'ark:/0/v%09d' % scheme.eid)
-            self.assertEqual(concept.ark, '0/c%09d' % concept.eid)
+                             'ark:/0/rf%09d' % scheme.eid)
+            self.assertEqual(concept.ark, '0/rf%09d' % concept.eid)
             self.assertEqual(concept.cwuri,
-                             'ark:/0/c%09d' % concept.eid)
+                             'ark:/0/rf%09d' % concept.eid)
             scheme = cnx.create_entity('ConceptScheme', cwuri=u'http://someuri/somescheme',
                                        ark_naa=testutils.naa(cnx))
             concept = scheme.add_concept(u'some object', cwuri=u'http://someuri/someconcept')
             cnx.commit()
-            self.assertEqual(scheme.ark, '0/v%09d' % scheme.eid)
+            self.assertEqual(scheme.ark, '0/rf%09d' % scheme.eid)
             self.assertEqual(scheme.cwuri, 'http://someuri/somescheme')
-            self.assertEqual(concept.ark, '0/c%09d' % concept.eid)
+            self.assertEqual(concept.ark, '0/rf%09d' % concept.eid)
             self.assertEqual(concept.cwuri, 'http://someuri/someconcept')
             scheme = cnx.create_entity('ConceptScheme', cwuri=u'http://dcf/res/ark:/67717/Matiere',
                                        ark_naa=testutils.naa(cnx))
diff --git a/test/unittest_sobjects_skos.py b/test/unittest_sobjects_skos.py
--- a/test/unittest_sobjects_skos.py
+++ b/test/unittest_sobjects_skos.py
@@ -75,9 +75,9 @@ class SKOSImportTC(CubicWebTC):
             dfsource.pull_data(cnx, force=True, raise_on_error=True)
         with self.admin_access.client_cnx() as cnx:
             scheme = cnx.find('ConceptScheme').one()
-            self.assertTrue(scheme.ark.startswith('0/v'))
+            self.assertTrue(scheme.ark.startswith('0/rf'))
             self.assertEqual(len(scheme.top_concepts), 2)
-            self.assertTrue(scheme.top_concepts[0].ark.startswith('0/c'))
+            self.assertTrue(scheme.top_concepts[0].ark.startswith('0/rf'))
 
 
 class LCSVImportTC(CubicWebTC):
@@ -92,7 +92,7 @@ class LCSVImportTC(CubicWebTC):
             concept1 = cnx.find(
                 'Concept', definition="Définition de l'organisation politique de l'organisme").one()
             self.assertEqual(concept1.cwuri,
-                             'ark:/0/c%09d' % concept1.eid)
+                             'ark:/0/rf%09d' % concept1.eid)
 
 
 if __name__ == '__main__':


More information about the saem-devel mailing list