author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 10 Jun 2016 13:21:31 +0200 | |
changeset 11295 | 00d42916f238 |
parent 10974 | 6557833657d6 |
permissions | -rw-r--r-- |
10974
6557833657d6
a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10973
diff
changeset
|
1 |
# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
3 |
# |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
4 |
# This file is part of CubicWeb. |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
5 |
# |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
9 |
# any later version. |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
10 |
# |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
14 |
# details. |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
15 |
# |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
18 |
"""json export views""" |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
19 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
20 |
__docformat__ = "restructuredtext en" |
10666
7f6b5f023884
[py3k] replace '_ = unicode' in global scope (closes #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10397
diff
changeset
|
21 |
from cubicweb import _ |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
22 |
|
10390
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
23 |
from cubicweb.uilib import rest_traceback |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
24 |
|
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
25 |
from cubicweb.utils import json_dumps |
10390
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
26 |
from cubicweb.predicates import ExpectedValuePredicate, any_rset, empty_rset |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
27 |
from cubicweb.view import EntityView, AnyRsetView |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
28 |
from cubicweb.web.application import anonymized_request |
10390
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
29 |
from cubicweb.web.views import basecontrollers, management |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
30 |
|
10974
6557833657d6
a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10973
diff
changeset
|
31 |
|
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
32 |
class JsonpController(basecontrollers.ViewController): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
33 |
"""The jsonp controller is the same as a ViewController but : |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
34 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
35 |
- anonymize request (avoid CSRF attacks) |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
36 |
- if ``vid`` parameter is passed, make sure it's sensible (i.e. either |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
37 |
"jsonexport" or "ejsonexport") |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
38 |
- if ``callback`` request parameter is passed, it's used as json padding |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
39 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
40 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
41 |
Response's content-type will either be ``application/javascript`` or |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
42 |
``application/json`` depending on ``callback`` parameter presence or not. |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
43 |
""" |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
44 |
__regid__ = 'jsonp' |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
45 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
46 |
def publish(self, rset=None): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
47 |
if 'vid' in self._cw.form: |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
48 |
vid = self._cw.form['vid'] |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
49 |
if vid not in ('jsonexport', 'ejsonexport'): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
50 |
self.warning("vid %s can't be used with jsonp controller, " |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
51 |
"falling back to jsonexport", vid) |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
52 |
self._cw.form['vid'] = 'jsonexport' |
10974
6557833657d6
a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10973
diff
changeset
|
53 |
else: # if no vid is specified, use jsonexport |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
54 |
self._cw.form['vid'] = 'jsonexport' |
8601
1a6000ff2080
[web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
7877
diff
changeset
|
55 |
if self._cw.vreg.config['anonymize-jsonp-queries']: |
1a6000ff2080
[web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
7877
diff
changeset
|
56 |
with anonymized_request(self._cw): |
1a6000ff2080
[web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
7877
diff
changeset
|
57 |
return self._get_json_data(rset) |
1a6000ff2080
[web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
7877
diff
changeset
|
58 |
else: |
1a6000ff2080
[web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
7877
diff
changeset
|
59 |
return self._get_json_data(rset) |
1a6000ff2080
[web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
7877
diff
changeset
|
60 |
|
1a6000ff2080
[web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
7877
diff
changeset
|
61 |
def _get_json_data(self, rset): |
1a6000ff2080
[web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
7877
diff
changeset
|
62 |
json_data = super(JsonpController, self).publish(rset) |
10974
6557833657d6
a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10973
diff
changeset
|
63 |
if 'callback' in self._cw.form: # jsonp |
10397
1ce4594f9cf4
[web/json] Controller.publish must return encoded data
Julien Cristau <julien.cristau@logilab.fr>
parents:
10390
diff
changeset
|
64 |
json_padding = self._cw.form['callback'].encode('ascii') |
1ce4594f9cf4
[web/json] Controller.publish must return encoded data
Julien Cristau <julien.cristau@logilab.fr>
parents:
10390
diff
changeset
|
65 |
# use ``application/javascript`` if ``callback`` parameter is |
1ce4594f9cf4
[web/json] Controller.publish must return encoded data
Julien Cristau <julien.cristau@logilab.fr>
parents:
10390
diff
changeset
|
66 |
# provided, keep ``application/json`` otherwise |
8601
1a6000ff2080
[web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
7877
diff
changeset
|
67 |
self._cw.set_content_type('application/javascript') |
10700
a6d9d27f4253
[web/views] port JSON views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10666
diff
changeset
|
68 |
json_data = json_padding + b'(' + json_data + b')' |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
69 |
return json_data |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
70 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
71 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
72 |
class JsonMixIn(object): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
73 |
"""mixin class for json views |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
74 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
75 |
Handles the following optional request parameters: |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
76 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
77 |
- ``_indent`` : must be an integer. If found, it is used to pretty print |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
78 |
json output |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
79 |
""" |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
80 |
templatable = False |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
81 |
content_type = 'application/json' |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
82 |
binary = True |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
83 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
84 |
def wdata(self, data): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
85 |
if '_indent' in self._cw.form: |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
86 |
indent = int(self._cw.form['_indent']) |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
87 |
else: |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
88 |
indent = None |
10700
a6d9d27f4253
[web/views] port JSON views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10666
diff
changeset
|
89 |
# python's json.dumps escapes non-ascii characters |
a6d9d27f4253
[web/views] port JSON views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10666
diff
changeset
|
90 |
self.w(json_dumps(data, indent=indent).encode('ascii')) |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
91 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
92 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
93 |
class JsonRsetView(JsonMixIn, AnyRsetView): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
94 |
"""dumps raw result set in JSON format""" |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
95 |
__regid__ = 'jsonexport' |
10974
6557833657d6
a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10973
diff
changeset
|
96 |
__select__ = any_rset() # means rset might be empty or have any shape |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
97 |
title = _('json-export-view') |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
98 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
99 |
def call(self): |
9492
c7fc56eecd1a
English typography
Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr>
parents:
8900
diff
changeset
|
100 |
# XXX mimic w3c recommandations to serialize SPARQL results in json? |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
101 |
# http://www.w3.org/TR/rdf-sparql-json-res/ |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
102 |
self.wdata(self.cw_rset.rows) |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
103 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
104 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
105 |
class JsonEntityView(JsonMixIn, EntityView): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
106 |
"""dumps rset entities in JSON |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
107 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
108 |
The following additional metadata is added to each row : |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
109 |
|
10973
0939ad2edf63
[adapters] Use an adapter for serializing entities to a bare python structure
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
10700
diff
changeset
|
110 |
- ``cw_etype`` : entity type |
0939ad2edf63
[adapters] Use an adapter for serializing entities to a bare python structure
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
10700
diff
changeset
|
111 |
- ``cw_source`` : source url |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
112 |
""" |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
113 |
__regid__ = 'ejsonexport' |
10013
b68088082280
[web/json] an empty rset is just fine for ejsonexport
Julien Cristau <julien.cristau@logilab.fr>
parents:
9492
diff
changeset
|
114 |
__select__ = EntityView.__select__ | empty_rset() |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
115 |
title = _('json-entities-export-view') |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
116 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
117 |
def call(self): |
8657
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
118 |
entities = [] |
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
119 |
for entity in self.cw_rset.entities(): |
10973
0939ad2edf63
[adapters] Use an adapter for serializing entities to a bare python structure
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
10700
diff
changeset
|
120 |
serializer = entity.cw_adapt_to('ISerializable') |
0939ad2edf63
[adapters] Use an adapter for serializing entities to a bare python structure
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
10700
diff
changeset
|
121 |
entities.append(serializer.serialize()) |
8657
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
122 |
self.wdata(entities) |
10390
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
123 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
124 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
125 |
class _requested_vid(ExpectedValuePredicate): |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
126 |
"""predicate that checks vid parameter value |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
127 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
128 |
It differs from ``match_view`` in that it doesn't expect a ``view`` |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
129 |
parameter to be given to ``select`` but will rather check |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
130 |
``req.form['vid']`` to match expected vid. |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
131 |
""" |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
132 |
def __call__(self, cls, req, rset=None, **kwargs): |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
133 |
return req.form.get('vid') in self.expected |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
134 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
135 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
136 |
class JsonErrorView(JsonMixIn, management.ErrorView): |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
137 |
"""custom error view selected when client asks for a json view |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
138 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
139 |
The returned json object will contain err / traceback informations. |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
140 |
""" |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
141 |
__select__ = (management.ErrorView.__select__ & |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
142 |
_requested_vid('jsonexport', 'ejsonexport')) |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
143 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
144 |
def call(self): |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
145 |
errmsg, exclass, excinfo = self._excinfo() |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
146 |
self.wdata({ |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
147 |
'errmsg': errmsg, |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
148 |
'exclass': exclass, |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
149 |
'traceback': rest_traceback(excinfo, errmsg), |
10974
6557833657d6
a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10973
diff
changeset
|
150 |
}) |