cubicweb/web/views/ajaxcontroller.py
changeset 12188 fea018b2e056
parent 12187 3a1f742b84cd
child 12189 ef46695adb68
equal deleted inserted replaced
12187:3a1f742b84cd 12188:fea018b2e056
    65 
    65 
    66 from warnings import warn
    66 from warnings import warn
    67 from functools import partial
    67 from functools import partial
    68 
    68 
    69 from six import PY2, text_type
    69 from six import PY2, text_type
       
    70 from six.moves import http_client
    70 
    71 
    71 from logilab.common.date import strptime
    72 from logilab.common.date import strptime
    72 from logilab.common.registry import yes
    73 from logilab.common.registry import yes
    73 from logilab.common.deprecation import deprecated
    74 from logilab.common.deprecation import deprecated
    74 
    75 
   114     def publish(self, rset=None):
   115     def publish(self, rset=None):
   115         self._cw.ajax_request = True
   116         self._cw.ajax_request = True
   116         try:
   117         try:
   117             fname = self._cw.form['fname']
   118             fname = self._cw.form['fname']
   118         except KeyError:
   119         except KeyError:
   119             raise RemoteCallFailed('no method specified')
   120             raise RemoteCallFailed('no method specified',
       
   121                                    status=http_client.BAD_REQUEST)
   120         # 1/ check first for old-style (JSonController) ajax func for bw compat
   122         # 1/ check first for old-style (JSonController) ajax func for bw compat
   121         try:
   123         try:
   122             func = getattr(basecontrollers.JSonController, 'js_%s' % fname)
   124             func = getattr(basecontrollers.JSonController, 'js_%s' % fname)
   123             if PY2:
   125             if PY2:
   124                 func = func.__func__
   126                 func = func.__func__
   126         except AttributeError:
   128         except AttributeError:
   127             # 2/ check for new-style (AjaxController) ajax func
   129             # 2/ check for new-style (AjaxController) ajax func
   128             try:
   130             try:
   129                 func = self._cw.vreg['ajax-func'].select(fname, self._cw)
   131                 func = self._cw.vreg['ajax-func'].select(fname, self._cw)
   130             except ObjectNotFound:
   132             except ObjectNotFound:
   131                 raise RemoteCallFailed('no %s method' % fname)
   133                 raise RemoteCallFailed('no %s method' % fname,
       
   134                                        status=http_client.BAD_REQUEST)
   132         else:
   135         else:
   133             warn('[3.15] remote function %s found on JSonController, '
   136             warn('[3.15] remote function %s found on JSonController, '
   134                  'use AjaxFunction / @ajaxfunc instead' % fname,
   137                  'use AjaxFunction / @ajaxfunc instead' % fname,
   135                  DeprecationWarning, stacklevel=2)
   138                  DeprecationWarning, stacklevel=2)
   136         debug_mode = self._cw.vreg.config.debugmode
   139         debug_mode = self._cw.vreg.config.debugmode
   142             args = [json.loads(arg) for arg in args]
   145             args = [json.loads(arg) for arg in args]
   143         except ValueError as exc:
   146         except ValueError as exc:
   144             if debug_mode:
   147             if debug_mode:
   145                 self.exception('error while decoding json arguments for '
   148                 self.exception('error while decoding json arguments for '
   146                                'js_%s: %s (err: %s)', fname, args, exc)
   149                                'js_%s: %s (err: %s)', fname, args, exc)
   147             raise RemoteCallFailed(exc_message(exc, self._cw.encoding))
   150             raise RemoteCallFailed(exc_message(exc, self._cw.encoding),
       
   151                                    status=http_client.BAD_REQUEST)
   148         try:
   152         try:
   149             result = func(*args)
   153             result = func(*args)
   150         except (RemoteCallFailed, DirectResponse):
   154         except (RemoteCallFailed, DirectResponse):
   151             raise
   155             raise
   152         except Exception as exc:
   156         except Exception as exc: