[datafeed, cw.xml] xml now carry entity's source information, interpreted at the other end so that for instance when an entity from elo is seen when importing cwo, it's properly marked as coming from elo source if one exists
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 17 Jun 2011 18:50:13 +0200
changeset 7534 d58a9d96aad8
parent 7533 43835fbdf97d
child 7536 29961a416faa
[datafeed, cw.xml] xml now carry entity's source information, interpreted at the other end so that for instance when an entity from elo is seen when importing cwo, it's properly marked as coming from elo source if one exists
server/sources/datafeed.py
sobjects/parsers.py
sobjects/test/unittest_parsers.py
web/views/xmlrss.py
--- a/server/sources/datafeed.py	Fri Jun 17 18:49:55 2011 +0200
+++ b/server/sources/datafeed.py	Fri Jun 17 18:50:13 2011 +0200
@@ -259,9 +259,14 @@
         """return an entity for the given uri. May return None if it should be
         skipped
         """
+        # if cwsource is specified and repository has a source with the same
+        # name, call extid2eid on that source so entity will be properly seen as
+        # coming from this source
+        source = self._cw.repo.sources_by_uri.get(
+            sourceparams.pop('cwsource', None), self.source)
         sourceparams['parser'] = self
-        eid = self.source.extid2eid(str(uri), etype, self._cw,
-                                    sourceparams=sourceparams)
+        eid = source.extid2eid(str(uri), etype, self._cw,
+                               sourceparams=sourceparams)
         if eid < 0:
             # entity has been moved away from its original source
             #
--- a/sobjects/parsers.py	Fri Jun 17 18:49:55 2011 +0200
+++ b/sobjects/parsers.py	Fri Jun 17 18:50:13 2011 +0200
@@ -80,12 +80,14 @@
         try:
             item = {'cwtype': unicode(node.tag),
                     'cwuri': node.attrib['cwuri'],
+                    'cwsource': node.attrib.get('cwsource'),
                     'eid': typed_eid(node.attrib['eid']),
                     }
         except KeyError:
             # cw < 3.11 compat mode XXX
             item = {'cwtype': unicode(node.tag),
                     'cwuri': node.find('cwuri').text,
+                    'cwsource': None,
                     'eid': typed_eid(node.find('eid').text),
                     }
         rels = {}
@@ -202,7 +204,7 @@
 
     def process_item(self, item, rels):
         entity = self.extid2entity(str(item.pop('cwuri')),  item.pop('cwtype'),
-                                   item=item)
+                                   cwsource=item.pop('cwsource'), item=item)
         if entity is None:
             return None
         if entity.eid in self._processed_entities:
--- a/sobjects/test/unittest_parsers.py	Fri Jun 17 18:49:55 2011 +0200
+++ b/sobjects/test/unittest_parsers.py	Fri Jun 17 18:50:13 2011 +0200
@@ -101,12 +101,28 @@
 ''',
     }
 
+
+OTHERXML = ''.join(u'''
+<rset size="1">
+ <CWUser eid="5" cwuri="http://pouet.org/5" cwsource="myfeed">
+  <login>sthenault</login>
+  <upassword>toto</upassword>
+  <last_login_time>2011-01-25 14:14:06</last_login_time>
+  <creation_date>2010-01-22 10:27:59</creation_date>
+  <modification_date>2011-01-25 14:14:06</modification_date>
+ </CWUser>
+</rset>
+'''.splitlines()
+)
 class CWEntityXMLParserTC(CubicWebTC):
-    def setup_database(self):
-        req = self.request()
-        source = req.create_entity('CWSource', name=u'myfeed', type=u'datafeed',
+    test_db_id = 'xmlparser'
+    @classmethod
+    def pre_setup_database(cls, session, config):
+        source = session.create_entity('CWSource', name=u'myfeed', type=u'datafeed',
                                    parser=u'cw.entityxml', url=BASEXML)
-        self.commit()
+        session.create_entity('CWSource', name=u'myotherfeed', type=u'datafeed',
+                              parser=u'cw.entityxml', url=OTHERXML)
+        session.commit()
         source.init_mapping([(('CWUser', 'use_email', '*'),
                               u'role=subject\naction=copy'),
                              (('CWUser', 'in_group', '*'),
@@ -114,7 +130,7 @@
                              (('*', 'tags', 'CWUser'),
                               u'role=object\naction=link-or-create\nlinkattr=name'),
                             ])
-        req.create_entity('Tag', name=u'hop')
+        session.create_entity('Tag', name=u'hop')
 
     def test_complete_url(self):
         dfsource = self.repo.sources_by_uri['myfeed']
@@ -215,6 +231,16 @@
         rset = self.sexecute('Any X WHERE X use_email E, X login "sthenault"')
         self.assertEqual(len(rset), 0)
 
+    def test_external_entity(self):
+        dfsource = self.repo.sources_by_uri['myotherfeed']
+        session = self.repo.internal_session()
+        stats = dfsource.pull_data(session, force=True, raise_on_error=True)
+        user = self.execute('CWUser X WHERE X login "sthenault"').get_entity(0, 0)
+        self.assertEqual(user.creation_date, datetime(2010, 01, 22, 10, 27, 59))
+        self.assertEqual(user.modification_date, datetime(2011, 01, 25, 14, 14, 06))
+        self.assertEqual(user.cwuri, 'http://pouet.org/5')
+        self.assertEqual(user.cw_source[0].name, 'myfeed')
+
 if __name__ == '__main__':
     from logilab.common.testlib import unittest_main
     unittest_main()
--- a/web/views/xmlrss.py	Fri Jun 17 18:49:55 2011 +0200
+++ b/web/views/xmlrss.py	Fri Jun 17 18:50:13 2011 +0200
@@ -76,8 +76,10 @@
     def cell_call(self, row, col):
         """ element as an item for an xml feed """
         entity = self.cw_rset.complete_entity(row, col)
-        self.w(u'<%s eid="%s" cwuri="%s">\n'
-               % (entity.e_schema, entity.eid, xml_escape(entity.cwuri)))
+        source = entity.cw_metainformation()['source']['uri']
+        self.w(u'<%s eid="%s" cwuri="%s" cwsource="%s">\n'
+               % (entity.__regid__, entity.eid, xml_escape(entity.cwuri),
+                  xml_escape(source)))
         for rschema, attrschema in entity.e_schema.attribute_definitions():
             attr = rschema.type
             if attr in ('eid', 'cwuri'):