Allow to specify export version/format as select context variable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 17 Oct 2016 14:42:01 +0200
changeset 1847 55b4f1a5d3ec
parent 1846 c15f45eaad6a
child 1848 44879714e928
Allow to specify export version/format as select context variable This will ease usage of the export view programmatically (for interaction with e.g. oaipmh).
test/test_views.py
views/export.py
--- a/test/test_views.py	Tue Oct 18 10:03:16 2016 +0200
+++ b/test/test_views.py	Mon Oct 17 14:42:01 2016 +0200
@@ -187,6 +187,15 @@
             self.assertEqual(predicate(None, req, entity=transfer),
                              0)
 
+            del req.form['version']
+            self.assertEqual(predicate(fakecls(seda_version='1.0'), req, entity=transfer,
+                                       version='2.0'),
+                             0)
+            self.assertEqual(predicate(None, req, entity=transfer, version='2.0'),
+                             1)
+            self.assertEqual(predicate(None, req, entity=transfer, version='1.0'),
+                             0)
+
 
 class ArchiveTransferDiagnoseTabTC(CubicWebTC):
 
--- a/views/export.py	Tue Oct 18 10:03:16 2016 +0200
+++ b/views/export.py	Mon Oct 17 14:42:01 2016 +0200
@@ -30,7 +30,7 @@
     """
     if entity is None:
         entity = rset.get_entity(0, 0)
-    version = getattr(cls, 'seda_version', req.form.get('version', '2.0'))
+    version = getattr(cls, 'seda_version', kwargs.get('version', req.form.get('version', '2.0')))
     return int(('SEDA ' + version) in entity.formats_compat)
 
 
@@ -122,8 +122,9 @@
 
     @property
     def seda_adapter_id(self):
-        return 'SEDA-{0}.{1}'.format(self._cw.form.get('version', '2.0'),
-                                     self._cw.form.get('format', 'rng').lower())
+        return 'SEDA-{0}.{1}'.format(
+            self.cw_extra_kwargs.get('version', self._cw.form.get('version', '2.0')),
+            self.cw_extra_kwargs.get('format', self._cw.form.get('format', 'rng')).lower())
 
     def set_request_content_type(self):
         entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
@@ -131,7 +132,7 @@
         self._cw.set_content_type(adapter.content_type, filename=adapter.file_name,
                                   encoding=adapter.encoding, disposition='attachment')
 
-    def call(self):
+    def call(self, **kwargs):
         entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
         adapter = entity.cw_adapt_to(self.seda_adapter_id)
         self.w(adapter.dump())
@@ -144,7 +145,7 @@
 
     def entity_call(self, entity):
         self.w(u'<p class="bg-error">')
-        version = self._cw.form['version']
+        version = self.cw_extra_kwargs.get('version', self._cw.form.get('version', '2.0'))
         self.w(self._cw._('Export to SEDA version {0} is not possible.').format(version))
         self.w(u'</p>')
         self.w(u'<p>')