author | Rabah Meradi <rabah.meradi@logilab.fr> |
Tue, 15 Dec 2015 08:55:58 +0100 | |
changeset 10973 | 0939ad2edf63 |
parent 10700 | a6d9d27f4253 |
child 10974 | 6557833657d6 |
permissions | -rw-r--r-- |
8601
1a6000ff2080
[web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
7877
diff
changeset
|
1 |
# copyright 2003-2012 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 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
31 |
class JsonpController(basecontrollers.ViewController): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
32 |
"""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
|
33 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
34 |
- anonymize request (avoid CSRF attacks) |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
35 |
- 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
|
36 |
"jsonexport" or "ejsonexport") |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
37 |
- 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
|
38 |
|
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 |
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
|
41 |
``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
|
42 |
""" |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
43 |
__regid__ = 'jsonp' |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
44 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
45 |
def publish(self, rset=None): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
46 |
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
|
47 |
vid = self._cw.form['vid'] |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
48 |
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
|
49 |
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
|
50 |
"falling back to jsonexport", vid) |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
51 |
self._cw.form['vid'] = 'jsonexport' |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
52 |
else: # if no vid is specified, use jsonexport |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
53 |
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
|
54 |
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
|
55 |
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
|
56 |
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
|
57 |
else: |
1a6000ff2080
[web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
7877
diff
changeset
|
58 |
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
|
59 |
|
1a6000ff2080
[web] add a ``anonymize-jsonp-queries`` option in file configuration (closes #2465388)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
7877
diff
changeset
|
60 |
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
|
61 |
json_data = super(JsonpController, self).publish(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 |
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
|
63 |
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
|
64 |
# 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
|
65 |
# 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
|
66 |
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
|
67 |
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
|
68 |
return json_data |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
69 |
|
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 |
class JsonMixIn(object): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
72 |
"""mixin class for json views |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
73 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
74 |
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
|
75 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
76 |
- ``_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
|
77 |
json output |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
78 |
""" |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
79 |
templatable = False |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
80 |
content_type = 'application/json' |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
81 |
binary = True |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
82 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
83 |
def wdata(self, data): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
84 |
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
|
85 |
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
|
86 |
else: |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
87 |
indent = None |
10700
a6d9d27f4253
[web/views] port JSON views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10666
diff
changeset
|
88 |
# 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
|
89 |
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
|
90 |
|
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 |
class JsonRsetView(JsonMixIn, AnyRsetView): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
93 |
"""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
|
94 |
__regid__ = 'jsonexport' |
8629
3ae893f9ec84
[json] enable jsonexport selection on empty rsets (closes #2544358)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8601
diff
changeset
|
95 |
__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
|
96 |
title = _('json-export-view') |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
97 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
98 |
def call(self): |
9492
c7fc56eecd1a
English typography
Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr>
parents:
8900
diff
changeset
|
99 |
# 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
|
100 |
# 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
|
101 |
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
|
102 |
|
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 |
class JsonEntityView(JsonMixIn, EntityView): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
105 |
"""dumps rset entities in JSON |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
106 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
107 |
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
|
108 |
|
10973
0939ad2edf63
[adapters] Use an adapter for serializing entities to a bare python structure
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
10700
diff
changeset
|
109 |
- ``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
|
110 |
- ``cw_source`` : source url |
7876
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
111 |
""" |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
112 |
__regid__ = 'ejsonexport' |
10013
b68088082280
[web/json] an empty rset is just fine for ejsonexport
Julien Cristau <julien.cristau@logilab.fr>
parents:
9492
diff
changeset
|
113 |
__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
|
114 |
title = _('json-entities-export-view') |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
115 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
116 |
def call(self): |
8657
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
117 |
entities = [] |
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
118 |
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
|
119 |
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
|
120 |
entities.append(serializer.serialize()) |
8657
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
121 |
self.wdata(entities) |
10390
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
122 |
|
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 |
class _requested_vid(ExpectedValuePredicate): |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
125 |
"""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
|
126 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
127 |
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
|
128 |
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
|
129 |
``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
|
130 |
""" |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
131 |
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
|
132 |
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
|
133 |
|
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 |
class JsonErrorView(JsonMixIn, management.ErrorView): |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
136 |
"""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
|
137 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
138 |
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
|
139 |
""" |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
140 |
__select__ = (management.ErrorView.__select__ & |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
141 |
_requested_vid('jsonexport', 'ejsonexport')) |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
142 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
143 |
def call(self): |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
144 |
errmsg, exclass, excinfo = self._excinfo() |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
145 |
self.wdata({ |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
146 |
'errmsg': errmsg, |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
147 |
'exclass': exclass, |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
148 |
'traceback': rest_traceback(excinfo, errmsg), |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
149 |
}) |