[PATCH V3] [dataimport] Fix case when extid is text in use_extid_as_cwuri()

julien tayon julien.tayon at logilab.fr
Tue May 21 17:56:04 CEST 2019


# HG changeset patch
# User julien tayon <julien.tayon at logilab.fr>
# Date 1558428608 -7200
#      Tue May 21 10:50:08 2019 +0200
# Node ID 84e672dfca30e9ec13b4f467178f0f5ffb96f5e9
# Parent  8241058763524c3306cddf7f2b52b2bca8211fa0
[dataimport] Fix case when extid is text in use_extid_as_cwuri()

Extid can be bytes or text.

This avoid AttributeError: 'str' object has no attribute 'decode'

diff --git a/cubicweb/dataimport/importer.py b/cubicweb/dataimport/importer.py
--- a/cubicweb/dataimport/importer.py
+++ b/cubicweb/dataimport/importer.py
@@ -71,7 +71,10 @@ def use_extid_as_cwuri(extid2eid):
     def use_extid_as_cwuri_filter(extentities):
         for extentity in extentities:
             if extentity.extid not in extid2eid:
-                extentity.values.setdefault('cwuri', set([extentity.extid.decode('utf-8')]))
+                cwuri = extentity.extid
+                if isinstance(cwuri, bytes):
+                    cwuri = cwuri.decode('utf-8')
+                extentity.values.setdefault('cwuri', set([cwuri]))
             yield extentity
     return use_extid_as_cwuri_filter
 
diff --git a/cubicweb/dataimport/test/unittest_importer.py b/cubicweb/dataimport/test/unittest_importer.py
--- a/cubicweb/dataimport/test/unittest_importer.py
+++ b/cubicweb/dataimport/test/unittest_importer.py
@@ -196,6 +196,10 @@ class UseExtidAsCwuriTC(TestCase):
         personne.values.pop('cwuri')
         list(set_cwuri((personne,)))
         self.assertNotIn('cwuri', personne.values)
+        personne = ExtEntity('Personne', 'ééé', {})
+        mapping = {}
+        set_cwuri = use_extid_as_cwuri(mapping)
+        list(set_cwuri((personne,)))
 
 
 class DropExtraValuesTC(CubicWebTC):


More information about the cubicweb-devel mailing list