[entities] Implement a specific adapter for File entity types
authorDenis Laxalde <denis.laxalde@logilab.fr>
Thu, 18 Dec 2014 09:32:05 +0100
changeset 43 681487d37369
parent 42 70deedd11ff0
child 44 359eb926e733
[entities] Implement a specific adapter for File entity types Related to #4753964.
entities.py
test/data/entities.py
--- a/entities.py	Wed Dec 17 16:29:39 2014 +0100
+++ b/entities.py	Thu Dec 18 09:32:05 2014 +0100
@@ -20,7 +20,7 @@
 import unicodedata
 from urllib2 import urlopen
 
-from cubicweb.predicates import relation_possible, adaptable
+from cubicweb.predicates import relation_possible, adaptable, is_instance
 from cubicweb.view import EntityAdapter
 
 from cubes.ckanpublish.utils import ckan_post
@@ -182,3 +182,14 @@
         """Read content using IDownloadable adapter from URL."""
         idownload = self.entity.cw_adapt_to('IDownloadable')
         return urlopen(idownload.download_url())
+
+
+class FileCKANResourceAdapter(DownloadableCKANResourceAdapter):
+    """Adapter for File entities that can be mapped to a CKAN resource."""
+    __abstract__ = True
+    __select__ = (DownloadableCKANResourceAdapter.__select__ &
+                  is_instance('File'))
+
+    def read(self):
+        """Read file content relying on File interface."""
+        return self.entity
--- a/test/data/entities.py	Wed Dec 17 16:29:39 2014 +0100
+++ b/test/data/entities.py	Thu Dec 18 09:32:05 2014 +0100
@@ -1,7 +1,7 @@
 from cubicweb.predicates import is_instance, relation_possible
 
 from cubes.ckanpublish.entities import (CKANDatasetAdapter,
-                                        DownloadableCKANResourceAdapter)
+                                        FileCKANResourceAdapter)
 
 
 class CWDatasetCKANDatasetAdapter(CKANDatasetAdapter):
@@ -12,9 +12,8 @@
             return self.entity.maintainer[0]
 
 
-class FileCKANResourceAdapter(DownloadableCKANResourceAdapter):
-    __select__ = (DownloadableCKANResourceAdapter.__select__ &
-                  is_instance('File') &
+class CWDatasetResourceCKANResourceAdapter(FileCKANResourceAdapter):
+    __select__ = (FileCKANResourceAdapter.__select__ &
                   relation_possible('resources', role='object'))
 
     @property