server/sources/datafeed.py
changeset 8188 1867e252e487
parent 8187 981f6e487788
child 8230 00435a332502
--- a/server/sources/datafeed.py	Thu Feb 02 14:30:07 2012 +0100
+++ b/server/sources/datafeed.py	Tue Jan 31 21:43:24 2012 +0100
@@ -103,6 +103,7 @@
                          if url.strip()]
         else:
             self.urls = []
+
     def update_config(self, source_entity, typedconfig):
         """update configuration from source entity. `typedconfig` is config
         properly typed with defaults set
@@ -290,7 +291,7 @@
                       'updated': set()}
 
     def normalize_url(self, url):
-        from cubicweb.sobjects.parsers import URL_MAPPING
+        from cubicweb.sobjects import URL_MAPPING # available after registration
         for mappedurl in URL_MAPPING:
             if url.startswith(mappedurl):
                 return url.replace(mappedurl, URL_MAPPING[mappedurl], 1)
@@ -374,6 +375,19 @@
         """
         return True
 
+    def update_if_necessary(self, entity, attrs):
+        self.notify_updated(entity)
+        entity.complete(tuple(attrs))
+        # check modification date and compare attribute values to only update
+        # what's actually needed
+        mdate = attrs.get('modification_date')
+        if not mdate or mdate > entity.modification_date:
+            attrs = dict( (k, v) for k, v in attrs.iteritems()
+                          if v != getattr(entity, k))
+            if attrs:
+                entity.set_attributes(**attrs)
+
+
 class DataFeedXMLParser(DataFeedParser):
 
     def process(self, url, raise_on_error=False, partialcommit=True):