[PATCH 3 of 3 saem_ref] Change create_default_naa() into a "get or create" function

Denis Laxalde denis.laxalde at logilab.fr
Wed Feb 6 10:06:35 CET 2019


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1549442955 -3600
#      Wed Feb 06 09:49:15 2019 +0100
# Node ID b90744f3d016e5c1b69288a2a682a861e88313f0
# Parent  d920b7551f920cef164167d22d1809db1b6c0b00
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r b90744f3d016
Change create_default_naa() into a "get or create" function

For some reason, I now fail to db-create an instance because the default
ARK NAA appears to be created twice. Trying to fix this here by making
function create_default_naa() (renamed as ark_naa) either get or create
the ARK NAA.

Not sure why the issue I see does not show up in tests, but the change
probably does not hurt.

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
@@ -24,6 +24,7 @@ from functools import partial
 from six import text_type
 
 from logilab.common.registry import objectify_predicate
+from cubicweb import NoResultError
 
 from cubicweb_compound import CompositeGraph
 
@@ -82,8 +83,14 @@ def _nohook_store_factory(cnx, config):
     return NoHookRQLObjectStore(cnx, metagen=_metagen(cnx, config))
 
 
-def create_default_naa(cnx, who, what):
+def ark_naa(cnx, who, what):
+    """Get or create the ArkNameAssigningAuthority entity matchin "who" and
+    "what" values.
+    """
     if not isinstance(who, text_type):
         who = text_type(who, 'utf-8')
-    return cnx.create_entity('ArkNameAssigningAuthority',
-                             who=who, what=what)
+    try:
+        return cnx.find('ArkNameAssigningAuthority', who=who, what=what).one()
+    except NoResultError:
+        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
@@ -18,7 +18,7 @@
 
 from cubicweb import _
 from cubicweb_saem_ref import (
-    create_default_naa,
+    ark_naa,
     workflows,
 )
 
@@ -47,5 +47,6 @@ 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)
+# Ensure the default ARK NAA is present.
+ark_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
@@ -23,7 +23,6 @@ from logilab.common.date import ustrftim
 from logilab.common.decorators import monkeypatch
 
 from cubicweb import (
-    NoResultError,
     cwvreg,
     _,
 )
@@ -38,7 +37,7 @@ import cubicweb_seda as seda
 import cubicweb_seda.dataimport as seda_dataimport
 
 from . import (
-    create_default_naa,
+    ark_naa,
     permanent_url,
     _massive_store_factory,
     _nohook_store_factory,
@@ -148,12 +147,9 @@ 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', who=who, what=what).one()
-    except NoResultError:
-        ark_naa = create_default_naa(cnx, who, what)
+    naa = ark_naa(cnx, who, what)
     return cnx.create_entity('ConceptScheme', title=title, description=description,
-                             ark_naa=ark_naa)
+                             ark_naa=naa)
 
 
 @monkeypatch(seda_dataimport)



More information about the saem-devel mailing list