[web/json] an empty rset is just fine for ejsonexport
authorJulien Cristau <julien.cristau@logilab.fr>
Tue, 17 Jun 2014 17:53:31 +0200
changeset 10013 b68088082280
parent 10012 8c2c6fdd8d56
child 10014 74b793086067
[web/json] an empty rset is just fine for ejsonexport The ejsonexport view can just return an empty list for an empty rset. Closes #4005518
web/test/unittest_views_json.py
web/test/unittest_viewselector.py
web/views/json.py
--- a/web/test/unittest_views_json.py	Wed Apr 30 21:04:14 2014 +0200
+++ b/web/test/unittest_views_json.py	Tue Jun 17 17:53:31 2014 +0200
@@ -70,6 +70,11 @@
             self.assertEqual(data[0]['name'], 'guests')
             self.assertEqual(data[1]['name'], 'managers')
 
+            rset = req.execute('Any G WHERE G is CWGroup, G name "foo"')
+            data = self.view('ejsonexport', rset, req=req)
+            self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json'])
+            self.assertEqual(data, [])
+
 
 class NotAnonymousJsonViewsTC(JsonViewsTC):
     anonymize = False
--- a/web/test/unittest_viewselector.py	Wed Apr 30 21:04:14 2014 +0200
+++ b/web/test/unittest_viewselector.py	Tue Jun 17 17:53:31 2014 +0200
@@ -110,6 +110,7 @@
             self.assertListEqual(self.pviews(req, rset),
                     [('csvexport', csvexport.CSVRsetView),
                      ('ecsvexport', csvexport.CSVEntityView),
+                     ('ejsonexport', json.JsonEntityView),
                      ('jsonexport', json.JsonRsetView),
                      ])
 
--- a/web/views/json.py	Wed Apr 30 21:04:14 2014 +0200
+++ b/web/views/json.py	Tue Jun 17 17:53:31 2014 +0200
@@ -21,7 +21,7 @@
 _ = unicode
 
 from cubicweb.utils import json_dumps
-from cubicweb.predicates import any_rset
+from cubicweb.predicates import any_rset, empty_rset
 from cubicweb.view import EntityView, AnyRsetView
 from cubicweb.web.application import anonymized_request
 from cubicweb.web.views import basecontrollers
@@ -106,6 +106,7 @@
     - ``__cwetype__`` : entity type
     """
     __regid__ = 'ejsonexport'
+    __select__ = EntityView.__select__ | empty_rset()
     title = _('json-entities-export-view')
 
     def call(self):