more generic way to detect json requests (not yet perfect though) stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 03 Jul 2009 17:57:19 +0200
branchstable
changeset 2255 c346af0727ca
parent 2254 f632b06058c4
child 2256 0b915d6f4264
more generic way to detect json requests (not yet perfect though)
web/request.py
web/views/basecontrollers.py
web/views/editcontroller.py
--- a/web/request.py	Fri Jul 03 16:50:13 2009 +0200
+++ b/web/request.py	Fri Jul 03 17:57:19 2009 +0200
@@ -61,6 +61,7 @@
 
 class CubicWebRequestBase(DBAPIRequest):
     """abstract HTTP request, should be extended according to the HTTP backend"""
+    json_request = False # to be set to True by json controllers
 
     def __init__(self, vreg, https, form=None):
         super(CubicWebRequestBase, self).__init__(vreg)
--- a/web/views/basecontrollers.py	Fri Jul 03 16:50:13 2009 +0200
+++ b/web/views/basecontrollers.py	Fri Jul 03 17:57:19 2009 +0200
@@ -196,7 +196,7 @@
     except NoSelectableObject:
         return (False, {None: req._('not authorized')})
     try:
-        ctrl.publish(None, fromjson=True)
+        ctrl.publish(None)
     except ValidationError, ex:
         req.cnx.rollback()
         return (False, _validation_error(req, ex))
@@ -219,6 +219,7 @@
     id = 'validateform'
 
     def publish(self, rset=None):
+        self.req.json_request = True
         # XXX unclear why we have a separated controller here vs
         # js_validate_form on the json controller
         status, args = _validate_form(self.req, self.vreg)
@@ -243,6 +244,7 @@
         note: it's the responsability of js_* methods to set the correct
         response content type
         """
+        self.req.json_request = True
         self.req.pageid = self.req.form.get('pageid')
         try:
             fname = self.req.form['fname']
--- a/web/views/editcontroller.py	Fri Jul 03 16:50:13 2009 +0200
+++ b/web/views/editcontroller.py	Fri Jul 03 17:57:19 2009 +0200
@@ -25,9 +25,8 @@
 class EditController(ViewController):
     id = 'edit'
 
-    def publish(self, rset=None, fromjson=False):
+    def publish(self, rset=None):
         """edit / create / copy / delete entity / relations"""
-        self.fromjson = fromjson
         for key in self.req.form:
             # There should be 0 or 1 action
             if key.startswith('__action_'):
@@ -115,7 +114,7 @@
             except ValidationError, ex:
                 # ex.entity may be an int or an entity instance
                 self._to_create[formparams['eid']] = ex.entity
-                if self.fromjson:
+                if self.req.json_request: # XXX (syt) why?
                     ex.entity = formparams['eid']
                 raise
             self._to_create[formparams['eid']] = eid