web/views/basecontrollers.py
branch3.5
changeset 3232 eccb7380dc3b
parent 3144 a5deac822a13
child 3237 ee2253f9fbe6
equal deleted inserted replaced
3231:3ee43e2f8560 3232:eccb7380dc3b
     9 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     9 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
    10 """
    10 """
    11 __docformat__ = "restructuredtext en"
    11 __docformat__ = "restructuredtext en"
    12 
    12 
    13 from smtplib import SMTP
    13 from smtplib import SMTP
       
    14 import datetime
    14 
    15 
    15 import simplejson
    16 import simplejson
    16 
    17 
    17 from logilab.common.decorators import cached
    18 from logilab.common.decorators import cached
    18 
    19 
   179         if foreid == eid:
   180         if foreid == eid:
   180             foreid = var
   181             foreid = var
   181             break
   182             break
   182     return (foreid, ex.errors)
   183     return (foreid, ex.errors)
   183 
   184 
       
   185 
   184 def _validate_form(req, vreg):
   186 def _validate_form(req, vreg):
   185     # XXX should use the `RemoteCallFailed` mechanism
   187     # XXX should use the `RemoteCallFailed` mechanism
   186     try:
   188     try:
   187         ctrl = vreg['controllers'].select('edit', req=req)
   189         ctrl = vreg['controllers'].select('edit', req=req)
   188     except NoSelectableObject:
   190     except NoSelectableObject:
   189         return (False, {None: req._('not authorized')})
   191         return (False, {None: req._('not authorized')}, None)
   190     try:
   192     try:
   191         ctrl.publish(None)
   193         ctrl.publish(None)
   192     except ValidationError, ex:
   194     except ValidationError, ex:
   193         return (False, _validation_error(req, ex))
   195         return (False, _validation_error(req, ex), ctrl._edited_entity)
   194     except Redirect, ex:
   196     except Redirect, ex:
       
   197         if ctrl._edited_entity:
       
   198             ctrl._edited_entity.complete()
   195         try:
   199         try:
   196             req.cnx.commit() # ValidationError may be raise on commit
   200             req.cnx.commit() # ValidationError may be raise on commit
   197         except ValidationError, ex:
   201         except ValidationError, ex:
   198             return (False, _validation_error(req, ex))
   202             return (False, _validation_error(req, ex), ctrl._edited_entity)
   199         else:
   203         else:
   200             return (True, ex.location)
   204             return (True, ex.location, ctrl._edited_entity)
   201     except Exception, ex:
   205     except Exception, ex:
   202         req.cnx.rollback()
   206         req.cnx.rollback()
   203         req.exception('unexpected error while validating form')
   207         req.exception('unexpected error while validating form')
   204         return (False, req._(str(ex).decode('utf-8')))
   208         return (False, req._(str(ex).decode('utf-8')), ctrl._edited_entity)
   205     return (False, '???')
   209     return (False, '???', None)
   206 
   210 
   207 
   211 
   208 class FormValidatorController(Controller):
   212 class FormValidatorController(Controller):
   209     id = 'validateform'
   213     id = 'validateform'
   210 
   214 
   211     def response(self, domid, status, args):
   215     def response(self, domid, status, args, entity):
       
   216         callback = str(self.req.form.get('__onsuccess', 'null'))
       
   217         errback = str(self.req.form.get('__onfailure', 'null'))
   212         self.req.set_content_type('text/html')
   218         self.req.set_content_type('text/html')
   213         jsargs = simplejson.dumps( (status, args) )
   219         jsargs = simplejson.dumps((status, args, entity), cls=CubicWebJsonEncoder)
   214         return """<script type="text/javascript">
   220         return """<script type="text/javascript">
   215  window.parent.handleFormValidationResponse('%s', null, null, %s);
   221  wp = window.parent;
   216 </script>""" %  (domid, jsargs)
   222  window.parent.handleFormValidationResponse('%s', %s, %s, %s);
       
   223 </script>""" %  (domid, callback, errback, jsargs)
   217 
   224 
   218     def publish(self, rset=None):
   225     def publish(self, rset=None):
   219         self.req.json_request = True
   226         self.req.json_request = True
   220         # XXX unclear why we have a separated controller here vs
   227         # XXX unclear why we have a separated controller here vs
   221         # js_validate_form on the json controller
   228         # js_validate_form on the json controller
   222         status, args = _validate_form(self.req, self.vreg)
   229         status, args, entity = _validate_form(self.req, self.vreg)
   223         domid = self.req.form.get('__domid', 'entityForm').encode(
   230         domid = self.req.form.get('__domid', 'entityForm').encode(
   224             self.req.encoding)
   231             self.req.encoding)
   225         return self.response(domid, status, args)
   232         return self.response(domid, status, args, entity)
   226 
   233 
   227 
   234 
   228 class JSonController(Controller):
   235 class JSonController(Controller):
   229     id = 'json'
   236     id = 'json'
   230 
   237