[datafeed] Allow to override use_cwuri_as_url in configuration of a datafeed source
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 20 Mar 2014 08:49:10 +0100
changeset 9822 4a118bfd6ab4
parent 9821 2077c8da1893
child 9823 258d2f9f7d39
[datafeed] Allow to override use_cwuri_as_url in configuration of a datafeed source This is necessary because in some case one will want to write parser which create entities not one-to-one related to entities from the distant source (for instance see the `vcrs` cube which create entities holding statistics for code review, while not importing review entities themselves). Ideally we should provide a way to the parser to indicate the source that cwuri should not be used, but that's a start... Closes #3670210
server/sources/__init__.py
server/sources/datafeed.py
server/test/unittest_datafeed.py
--- a/server/sources/__init__.py	Fri Jun 27 11:48:26 2014 +0200
+++ b/server/sources/__init__.py	Thu Mar 20 08:49:10 2014 +0100
@@ -105,7 +105,7 @@
         self.support_relations['identity'] = False
         self.eid = eid
         self.public_config = source_config.copy()
-        self.public_config.setdefault('use-cwuri-as-url', self.use_cwuri_as_url)
+        self.public_config['use-cwuri-as-url'] = self.use_cwuri_as_url
         self.remove_sensitive_information(self.public_config)
         self.uri = source_config.pop('uri')
         set_log_methods(self, getLogger('cubicweb.sources.'+self.uri))
--- a/server/sources/datafeed.py	Fri Jun 27 11:48:26 2014 +0200
+++ b/server/sources/datafeed.py	Thu Mar 20 08:49:10 2014 +0100
@@ -83,6 +83,13 @@
           'help': ('Timeout of HTTP GET requests, when synchronizing a source.'),
           'group': 'datafeed-source', 'level': 2,
           }),
+        ('use-cwuri-as-url',
+         {'type': 'yn',
+          'default': None, # explicitly unset
+          'help': ('Use cwuri (i.e. external URL) for link to the entity '
+                   'instead of its local URL.'),
+          'group': 'datafeed-source', 'level': 1,
+          }),
         )
 
     def check_config(self, source_entity):
@@ -107,6 +114,12 @@
         self.synchro_interval = timedelta(seconds=typed_config['synchronization-interval'])
         self.max_lock_lifetime = timedelta(seconds=typed_config['max-lock-lifetime'])
         self.http_timeout = typed_config['http-timeout']
+        # if typed_config['use-cwuri-as-url'] is set, we have to update
+        # use_cwuri_as_url attribute and public configuration dictionary
+        # accordingly
+        if typed_config['use-cwuri-as-url'] is not None:
+            self.use_cwuri_as_url = typed_config['use-cwuri-as-url']
+            self.public_config['use-cwuri-as-url'] = self.use_cwuri_as_url
 
     def init(self, activated, source_entity):
         super(DataFeedSource, self).init(activated, source_entity)
--- a/server/test/unittest_datafeed.py	Fri Jun 27 11:48:26 2014 +0200
+++ b/server/test/unittest_datafeed.py	Thu Mar 20 08:49:10 2014 +0100
@@ -33,6 +33,10 @@
     def test(self):
         self.assertIn('myfeed', self.repo.sources_by_uri)
         dfsource = self.repo.sources_by_uri['myfeed']
+        self.assertNotIn('use_cwuri_as_url', dfsource.__dict__)
+        self.assertEqual({'type': u'datafeed', 'uri': u'myfeed', 'use-cwuri-as-url': True},
+                         dfsource.public_config)
+        self.assertEqual(dfsource.use_cwuri_as_url, True)
         self.assertEqual(dfsource.latest_retrieval, None)
         self.assertEqual(dfsource.synchro_interval, timedelta(seconds=60))
         self.assertFalse(dfsource.fresh())
@@ -119,6 +123,20 @@
             self.assertFalse(cnx.execute('Card X WHERE X title "cubicweb.org"'))
             self.assertFalse(cnx.execute('Any X WHERE X has_text "cubicweb.org"'))
 
+
+class DataFeedConfigTC(CubicWebTC):
+
+    def test_use_cwuri_as_url_override(self):
+        with self.admin_access.client_cnx() as cnx:
+            cnx.create_entity('CWSource', name=u'myfeed', type=u'datafeed',
+                              parser=u'testparser', url=u'ignored',
+                              config=u'use-cwuri-as-url=no')
+            cnx.commit()
+        dfsource = self.repo.sources_by_uri['myfeed']
+        self.assertEqual(dfsource.use_cwuri_as_url, False)
+        self.assertEqual({'type': u'datafeed', 'uri': u'myfeed', 'use-cwuri-as-url': False},
+                         dfsource.public_config)
+
 if __name__ == '__main__':
     from logilab.common.testlib import unittest_main
     unittest_main()