# HG changeset patch # User Sylvain Thénault # Date 1308329413 -7200 # Node ID d58a9d96aad895a59d9b7409701d4c8ecb49799f # Parent 43835fbdf97dda3dc2e65b1c52063e83d8d3a4dc [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 diff -r 43835fbdf97d -r d58a9d96aad8 server/sources/datafeed.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 # diff -r 43835fbdf97d -r d58a9d96aad8 sobjects/parsers.py --- 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: diff -r 43835fbdf97d -r d58a9d96aad8 sobjects/test/unittest_parsers.py --- 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''' + + + sthenault + toto + 2011-01-25 14:14:06 + 2010-01-22 10:27:59 + 2011-01-25 14:14:06 + + +'''.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() diff -r 43835fbdf97d -r d58a9d96aad8 web/views/xmlrss.py --- 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'):