[PATCH saem_ref v2] Add options to configure the default ARK NAA entity (CONSEJIRA-614)

Denis Laxalde denis.laxalde at logilab.fr
Wed Oct 31 11:36:18 CET 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1540904991 -3600
#      Tue Oct 30 14:09:51 2018 +0100
# Node ID 096406495866b499f10c7c5fdc35769d0f873614
# Parent  91519f60b7a23bc513b4ec6c477b97273e96607d
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 096406495866
# EXP-Topic CONSEJIRA-614
Add options to configure the default ARK NAA entity (CONSEJIRA-614)

We need an initial ArkNameAssigningAuthority entity during db-init since
we import concept schemes used as SEDA vocabularies and these need an
ARK NAA in order their ARK to be generated. Previously, we used
ADGIRONDE ARK NAA number as an hard-coded value in init_seda_scheme()
but this is problematic for people wanting to use another authority.

We thus introduce two configuration values to be set at instance
creation and used during SEDA vocabularies import.

Since I failed to set a value for these options during test setup that
could be available early enough for "postcreate" script to see it, this
script contains a "if config.mode == 'TEST'" that by-pass config reading
and use values previously set in testutils.naa(); in testutils.naa() we
no longer create an ArkNameAssigningAuthority entity but now always
retrieve the same entity using the "what" value setup in postcreate.

In automatic web tests, we need a special value generator to avoid
getting into uniqueness conflict when an ArkNameAssigningAuthority with
what=0 would be generated. We use a 5 digits integer as this is what the
standards suggests.

diff --git a/cubicweb_saem_ref/__init__.py b/cubicweb_saem_ref/__init__.py
--- a/cubicweb_saem_ref/__init__.py
+++ b/cubicweb_saem_ref/__init__.py
@@ -80,3 +80,10 @@ def _massive_store_factory(cnx, config, 
 def _nohook_store_factory(cnx, config):
     from cubicweb.dataimport.stores import NoHookRQLObjectStore
     return NoHookRQLObjectStore(cnx, metagen=_metagen(cnx, config))
+
+
+def create_default_naa(cnx, who, what):
+    if not isinstance(who, text_type):
+        who = text_type(who)
+    return cnx.create_entity('ArkNameAssigningAuthority',
+                             who=who, what=what)
diff --git a/cubicweb_saem_ref/migration/postcreate.py b/cubicweb_saem_ref/migration/postcreate.py
--- a/cubicweb_saem_ref/migration/postcreate.py
+++ b/cubicweb_saem_ref/migration/postcreate.py
@@ -17,7 +17,10 @@
 """cubicweb-saem-ref postcreate script"""
 
 from cubicweb import _
-from cubicweb_saem_ref import workflows
+from cubicweb_saem_ref import (
+    create_default_naa,
+    workflows,
+)
 
 
 set_property('ui.site-title', u'Référentiel SAEM')
@@ -39,3 +42,10 @@ with cnx.deny_all_hooks_but():
     rql('SET X in_state S WHERE X is ConceptScheme, S eid %(s)s',
         {'s': schema_wf.state_by_name('published').eid})
 commit()
+
+if config.mode == 'test':
+    who, what = 'TEST', 0
+else:
+    who, what = config['default-ark-naa-who'], config['default-ark-naa-what']
+create_default_naa(cnx, who, what)
+commit()
diff --git a/cubicweb_saem_ref/site_cubicweb.py b/cubicweb_saem_ref/site_cubicweb.py
--- a/cubicweb_saem_ref/site_cubicweb.py
+++ b/cubicweb_saem_ref/site_cubicweb.py
@@ -18,6 +18,7 @@
 
 import pytz
 
+from logilab.common.configuration import REQUIRED
 from logilab.common.date import ustrftime
 from logilab.common.decorators import monkeypatch
 
@@ -36,13 +37,36 @@ from cubicweb_skos.ccplugin import Impor
 import cubicweb_seda as seda
 import cubicweb_seda.dataimport as seda_dataimport
 
-from . import permanent_url, _massive_store_factory, _nohook_store_factory
+from . import (
+    create_default_naa,
+    permanent_url,
+    _massive_store_factory,
+    _nohook_store_factory,
+)
 
 # this import is needed to take account of pg_trgm monkeypatches
 # while executing cubicweb-ctl commands (db-rebuild-fti)
 from . import pg_trgm  # noqa pylint: disable=unused-import
 
 
+options = (
+    ('default-ark-naa-who',
+     {'type': 'string',
+      'default': REQUIRED,
+      'help': ('Name of default ARK Name Assigning Authority (who)'
+               ' (only used at database initialization)'),
+      'group': 'saem_ref',
+      'level': 0}),
+    ('default-ark-naa-what',
+     {'type': 'int',
+      'default': REQUIRED,
+      'help': ('Number of default ARK Name Assigning Authority (what)'
+               ' (only used at database initialization)'),
+      'group': 'saem_ref',
+      'level': 0}),
+)
+
+
 # override Entity.rest_path to use ark for entities which have one
 
 _orig_rest_path = Entity.rest_path
@@ -122,11 +146,12 @@ ImportSkosData.cw_store_factories['nohoo
 @monkeypatch(seda_dataimport)
 def init_seda_scheme(cnx, title, _count=[]):
     description = u'edition 2009' if title.startswith('SEDA :') else None
+    who = cnx.vreg.config['default-ark-naa-who']
+    what = cnx.vreg.config['default-ark-naa-what']
     try:
-        ark_naa = cnx.find('ArkNameAssigningAuthority', what=25651).one()
+        ark_naa = cnx.find('ArkNameAssigningAuthority', who=who, what=what).one()
     except NoResultError:
-        ark_naa = cnx.create_entity(
-            'ArkNameAssigningAuthority', who=u'ADGIRONDE', what=25651)
+        ark_naa = create_default_naa(cnx, who, what)
     return cnx.create_entity('ConceptScheme', title=title, description=description,
                              ark_naa=ark_naa)
 
diff --git a/test/test_saem_ref.py b/test/test_saem_ref.py
--- a/test/test_saem_ref.py
+++ b/test/test_saem_ref.py
@@ -102,6 +102,9 @@ class MyValueGenerator(ValueGenerator):
             ''.join(random.sample('qwertyuvckj87932141', 10)),
         ]).decode('ascii')
 
+    def generate_ArkNameAssigningAuthority_what(self, entity, index):
+        return int(''.join(random.sample('0123456789', 5)))
+
 
 class AutomaticWebTest(testlib.AutomaticWebTest):
     configcls = PostgresApptestConfiguration
diff --git a/test/testutils.py b/test/testutils.py
--- a/test/testutils.py
+++ b/test/testutils.py
@@ -110,11 +110,7 @@ def seda_transfer(cnx, **kwargs):
 
 
 def naa(cnx):
-    try:
-        return cnx.find('ArkNameAssigningAuthority').one()
-    except NoResultError:
-        with cnx.security_enabled(False, False):
-            return cnx.create_entity('ArkNameAssigningAuthority', who=u'TEST', what=0)
+    return cnx.find('ArkNameAssigningAuthority', what=0).one()
 
 
 def authority_with_naa(cnx, name=u'Default authority', **kwargs):


More information about the saem-devel mailing list