author | Rémi Cardona <remi.cardona@logilab.fr> |
Fri, 11 Sep 2015 18:05:38 +0200 | |
changeset 10666 | 7f6b5f023884 |
parent 10397 | 1ce4594f9cf4 |
child 10700 | a6d9d27f4253 |
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') |
10397
1ce4594f9cf4
[web/json] Controller.publish must return encoded data
Julien Cristau <julien.cristau@logilab.fr>
parents:
10390
diff
changeset
|
67 |
json_data = b'%s(%s)' % (json_padding, json_data) |
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 |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
88 |
self.w(json_dumps(data, indent=indent)) |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
89 |
|
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 |
class JsonRsetView(JsonMixIn, AnyRsetView): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
92 |
"""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
|
93 |
__regid__ = 'jsonexport' |
8629
3ae893f9ec84
[json] enable jsonexport selection on empty rsets (closes #2544358)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8601
diff
changeset
|
94 |
__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
|
95 |
title = _('json-export-view') |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
96 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
97 |
def call(self): |
9492
c7fc56eecd1a
English typography
Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr>
parents:
8900
diff
changeset
|
98 |
# 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
|
99 |
# 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
|
100 |
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
|
101 |
|
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 |
class JsonEntityView(JsonMixIn, EntityView): |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
104 |
"""dumps rset entities in JSON |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
105 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
106 |
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
|
107 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
108 |
- ``__cwetype__`` : entity type |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
109 |
""" |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
110 |
__regid__ = 'ejsonexport' |
10013
b68088082280
[web/json] an empty rset is just fine for ejsonexport
Julien Cristau <julien.cristau@logilab.fr>
parents:
9492
diff
changeset
|
111 |
__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
|
112 |
title = _('json-entities-export-view') |
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
113 |
|
df15d194a134
[views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
114 |
def call(self): |
8657
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
115 |
entities = [] |
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
116 |
for entity in self.cw_rset.entities(): |
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
117 |
entity.complete() # fetch all attributes |
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
118 |
# hack to add extra metadata |
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
119 |
entity.cw_attr_cache.update({ |
8900
010a59e12d89
use cw_etype instead of __regid__
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8694
diff
changeset
|
120 |
'__cwetype__': entity.cw_etype, |
8657
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
121 |
}) |
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
122 |
entities.append(entity) |
5ab6ec83d5ba
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8651
diff
changeset
|
123 |
self.wdata(entities) |
10390
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 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
126 |
class _requested_vid(ExpectedValuePredicate): |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
127 |
"""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
|
128 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
129 |
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
|
130 |
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
|
131 |
``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
|
132 |
""" |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
133 |
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
|
134 |
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
|
135 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
136 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
137 |
class JsonErrorView(JsonMixIn, management.ErrorView): |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
138 |
"""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
|
139 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
140 |
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
|
141 |
""" |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
142 |
__select__ = (management.ErrorView.__select__ & |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
143 |
_requested_vid('jsonexport', 'ejsonexport')) |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
144 |
|
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
145 |
def call(self): |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
146 |
errmsg, exclass, excinfo = self._excinfo() |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
147 |
self.wdata({ |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
148 |
'errmsg': errmsg, |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
149 |
'exclass': exclass, |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
150 |
'traceback': rest_traceback(excinfo, errmsg), |
0bbd211cf4d7
[views] implement a custom json error view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10013
diff
changeset
|
151 |
}) |