[dataimport] Handle Bytes data in ExtEntity and convert them to Binary
authorDenis Laxalde <denis.laxalde@logilab.fr>
Wed, 22 Jun 2016 10:22:37 +0200
changeset 11342 62a7100d774b
parent 11341 bd3cd3691ade
child 11343 892e4c12f03f
[dataimport] Handle Bytes data in ExtEntity and convert them to Binary Closes #13887276.
cubicweb/dataimport/importer.py
cubicweb/dataimport/test/data/schema.py
cubicweb/dataimport/test/unittest_importer.py
--- a/cubicweb/dataimport/importer.py	Tue Jun 21 18:11:40 2016 +0200
+++ b/cubicweb/dataimport/importer.py	Wed Jun 22 10:22:37 2016 +0200
@@ -32,6 +32,8 @@
 
 from logilab.mtconverter import xml_escape
 
+from cubicweb import Binary
+
 
 def cwuri2eid(cnx, etypes, source_eid=None):
     """Return a dictionary mapping cwuri to eid for entities of the given entity types and / or
@@ -137,7 +139,8 @@
       (eg. ``'Person'``, ``'Animal'``, ...),
 
     * ``values``, a dictionary whose keys are attribute or relation names from the schema (eg.
-      ``'first_name'``, ``'friend'``), and whose values are *sets*
+      ``'first_name'``, ``'friend'``), and whose values are *sets*. For
+      attributes of type Bytes, byte strings should be inserted in `values`.
 
     For instance:
 
@@ -213,6 +216,8 @@
                     if (rschema.final and eschema.has_metadata(rtype, 'format')
                             and not rtype + '_format' in entity_dict):
                         entity_dict[rtype + '_format'] = u'text/plain'
+                    if rschema.final and rschema.objects()[0].type == 'Bytes':
+                        entity_dict[rtype] = Binary(entity_dict[rtype])
                 else:
                     del entity_dict[key]
             else:
--- a/cubicweb/dataimport/test/data/schema.py	Tue Jun 21 18:11:40 2016 +0200
+++ b/cubicweb/dataimport/test/data/schema.py	Wed Jun 22 10:22:37 2016 +0200
@@ -16,7 +16,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
 
-from yams.buildobjs import EntityType, String, SubjectRelation
+from yams.buildobjs import Bytes, EntityType, String, SubjectRelation
 
 from cubicweb.schema import RQLConstraint
 
@@ -27,3 +27,4 @@
     enfant = SubjectRelation('Personne', inlined=True, cardinality='?*')
     connait = SubjectRelation('Personne', symmetric=True,
                               constraints=[RQLConstraint('NOT S identity O')])
+    photo = Bytes()
--- a/cubicweb/dataimport/test/unittest_importer.py	Tue Jun 21 18:11:40 2016 +0200
+++ b/cubicweb/dataimport/test/unittest_importer.py	Wed Jun 22 10:22:37 2016 +0200
@@ -64,6 +64,16 @@
             self.assertEqual(entity.nom, u'de la lune')
             self.assertEqual(entity.prenom, u'Jean')
 
+    def test_bytes_attribute(self):
+        with self.admin_access.repo_cnx() as cnx:
+            importer = self.importer(cnx)
+            personne = ExtEntity('Personne', 1, {'photo': set([b'poilu']),
+                                                 'nom': set([u'alf'])})
+            importer.import_entities([personne])
+            cnx.commit()
+            entity = cnx.find('Personne').one()
+            self.assertEqual(entity.photo.getvalue(), b'poilu')
+
     def test_import_missing_required_attribute(self):
         """Check import of ext entity with missing required attribute"""
         with self.admin_access.repo_cnx() as cnx: