10 """ |
10 """ |
11 __docformat__ = "restructuredtext en" |
11 __docformat__ = "restructuredtext en" |
12 |
12 |
13 from smtplib import SMTP |
13 from smtplib import SMTP |
14 |
14 |
15 import simplejson |
15 try: |
|
16 import json |
|
17 except ImportError: |
|
18 import simplejson as json |
16 |
19 |
17 from logilab.common.decorators import cached |
20 from logilab.common.decorators import cached |
18 from logilab.common.date import strptime |
21 from logilab.common.date import strptime |
19 |
22 |
20 from cubicweb import (NoSelectableObject, ValidationError, ObjectNotFound, |
23 from cubicweb import (NoSelectableObject, ValidationError, ObjectNotFound, |
32 HAS_SEARCH_RESTRICTION = True |
35 HAS_SEARCH_RESTRICTION = True |
33 except ImportError: # gae |
36 except ImportError: # gae |
34 HAS_SEARCH_RESTRICTION = False |
37 HAS_SEARCH_RESTRICTION = False |
35 |
38 |
36 def jsonize(func): |
39 def jsonize(func): |
37 """decorator to sets correct content_type and calls `simplejson.dumps` on |
40 """decorator to sets correct content_type and calls `json.dumps` on |
38 results |
41 results |
39 """ |
42 """ |
40 def wrapper(self, *args, **kwargs): |
43 def wrapper(self, *args, **kwargs): |
41 self._cw.set_content_type('application/json') |
44 self._cw.set_content_type('application/json') |
42 return json_dumps(func(self, *args, **kwargs)) |
45 return json_dumps(func(self, *args, **kwargs)) |
234 def response(self, domid, status, args, entity): |
237 def response(self, domid, status, args, entity): |
235 callback = str(self._cw.form.get('__onsuccess', 'null')) |
238 callback = str(self._cw.form.get('__onsuccess', 'null')) |
236 errback = str(self._cw.form.get('__onfailure', 'null')) |
239 errback = str(self._cw.form.get('__onfailure', 'null')) |
237 cbargs = str(self._cw.form.get('__cbargs', 'null')) |
240 cbargs = str(self._cw.form.get('__cbargs', 'null')) |
238 self._cw.set_content_type('text/html') |
241 self._cw.set_content_type('text/html') |
239 jsargs = simplejson.dumps((status, args, entity), cls=CubicWebJsonEncoder) |
242 jsargs = json.dumps((status, args, entity), cls=CubicWebJsonEncoder) |
240 return """<script type="text/javascript"> |
243 return """<script type="text/javascript"> |
241 wp = window.parent; |
244 wp = window.parent; |
242 window.parent.handleFormValidationResponse('%s', %s, %s, %s, %s); |
245 window.parent.handleFormValidationResponse('%s', %s, %s, %s, %s); |
243 </script>""" % (domid, callback, errback, jsargs, cbargs) |
246 </script>""" % (domid, callback, errback, jsargs, cbargs) |
244 |
247 |
275 # no <arg> attribute means the callback takes no argument |
278 # no <arg> attribute means the callback takes no argument |
276 args = self._cw.form.get('arg', ()) |
279 args = self._cw.form.get('arg', ()) |
277 if not isinstance(args, (list, tuple)): |
280 if not isinstance(args, (list, tuple)): |
278 args = (args,) |
281 args = (args,) |
279 try: |
282 try: |
280 args = [simplejson.loads(arg) for arg in args] |
283 args = [json.loads(arg) for arg in args] |
281 except ValueError, exc: |
284 except ValueError, exc: |
282 self.exception('error while decoding json arguments for js_%s: %s', fname, args, exc) |
285 self.exception('error while decoding json arguments for js_%s: %s', fname, args, exc) |
283 raise RemoteCallFailed(repr(exc)) |
286 raise RemoteCallFailed(repr(exc)) |
284 try: |
287 try: |
285 result = func(*args) |
288 result = func(*args) |
439 args = dict((x, self._cw.form[x]) |
442 args = dict((x, self._cw.form[x]) |
440 for x in frozenset(('rtype', 'role', 'reload', 'landing_zone'))) |
443 for x in frozenset(('rtype', 'role', 'reload', 'landing_zone'))) |
441 entity = self._cw.entity_from_eid(int(self._cw.form['eid'])) |
444 entity = self._cw.entity_from_eid(int(self._cw.form['eid'])) |
442 # note: default is reserved in js land |
445 # note: default is reserved in js land |
443 args['default'] = self._cw.form['default_value'] |
446 args['default'] = self._cw.form['default_value'] |
444 args['reload'] = simplejson.loads(args['reload']) |
447 args['reload'] = json.loads(args['reload']) |
445 rset = req.eid_rset(int(self._cw.form['eid'])) |
448 rset = req.eid_rset(int(self._cw.form['eid'])) |
446 view = req.vreg['views'].select('doreledit', req, rset=rset, rtype=args['rtype']) |
449 view = req.vreg['views'].select('doreledit', req, rset=rset, rtype=args['rtype']) |
447 stream = view.set_stream() |
450 stream = view.set_stream() |
448 view.render(**args) |
451 view.render(**args) |
449 # XXX why not _call_view ? |
452 # XXX why not _call_view ? |