[controllers] allow onsuccess / onfailure callback to be passed to validateform 3.5
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Tue, 15 Sep 2009 16:02:17 +0200
branch3.5
changeset 3232 eccb7380dc3b
parent 3231 3ee43e2f8560
child 3234 e454590f1b80
[controllers] allow onsuccess / onfailure callback to be passed to validateform For instance : validateform?__onsuccess=wp.addAndSelectHall will call the addAndSelectHall function on success rather than redirecting to a specific URL.
web/data/cubicweb.edition.js
web/views/basecontrollers.py
--- a/web/data/cubicweb.edition.js	Tue Sep 15 16:00:03 2009 +0200
+++ b/web/data/cubicweb.edition.js	Tue Sep 15 16:02:17 2009 +0200
@@ -354,7 +354,7 @@
     // Success
     if (result[0]) {
 	if (onsuccess) {
-             onsuccess(result[1], formid);
+             onsuccess(result, formid);
 	} else {
 	    document.location.href = result[1];
 	}
--- a/web/views/basecontrollers.py	Tue Sep 15 16:00:03 2009 +0200
+++ b/web/views/basecontrollers.py	Tue Sep 15 16:02:17 2009 +0200
@@ -11,6 +11,7 @@
 __docformat__ = "restructuredtext en"
 
 from smtplib import SMTP
+import datetime
 
 import simplejson
 
@@ -181,48 +182,54 @@
             break
     return (foreid, ex.errors)
 
+
 def _validate_form(req, vreg):
     # XXX should use the `RemoteCallFailed` mechanism
     try:
         ctrl = vreg['controllers'].select('edit', req=req)
     except NoSelectableObject:
-        return (False, {None: req._('not authorized')})
+        return (False, {None: req._('not authorized')}, None)
     try:
         ctrl.publish(None)
     except ValidationError, ex:
-        return (False, _validation_error(req, ex))
+        return (False, _validation_error(req, ex), ctrl._edited_entity)
     except Redirect, ex:
+        if ctrl._edited_entity:
+            ctrl._edited_entity.complete()
         try:
             req.cnx.commit() # ValidationError may be raise on commit
         except ValidationError, ex:
-            return (False, _validation_error(req, ex))
+            return (False, _validation_error(req, ex), ctrl._edited_entity)
         else:
-            return (True, ex.location)
+            return (True, ex.location, ctrl._edited_entity)
     except Exception, ex:
         req.cnx.rollback()
         req.exception('unexpected error while validating form')
-        return (False, req._(str(ex).decode('utf-8')))
-    return (False, '???')
+        return (False, req._(str(ex).decode('utf-8')), ctrl._edited_entity)
+    return (False, '???', None)
 
 
 class FormValidatorController(Controller):
     id = 'validateform'
 
-    def response(self, domid, status, args):
+    def response(self, domid, status, args, entity):
+        callback = str(self.req.form.get('__onsuccess', 'null'))
+        errback = str(self.req.form.get('__onfailure', 'null'))
         self.req.set_content_type('text/html')
-        jsargs = simplejson.dumps( (status, args) )
+        jsargs = simplejson.dumps((status, args, entity), cls=CubicWebJsonEncoder)
         return """<script type="text/javascript">
- window.parent.handleFormValidationResponse('%s', null, null, %s);
-</script>""" %  (domid, jsargs)
+ wp = window.parent;
+ window.parent.handleFormValidationResponse('%s', %s, %s, %s);
+</script>""" %  (domid, callback, errback, jsargs)
 
     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)
+        status, args, entity = _validate_form(self.req, self.vreg)
         domid = self.req.form.get('__domid', 'entityForm').encode(
             self.req.encoding)
-        return self.response(domid, status, args)
+        return self.response(domid, status, args, entity)
 
 
 class JSonController(Controller):