[views] Make JsonMixIn.wdata method usable with non-web connections

authorDavid Douard <david.douard@logilab.fr>
changesetc1992b98a992
branchdefault
phasedraft
hiddenno
parent revision#a4d465a3e77d fix(ci): manually remove the .tox/doc directory
child revision<not specified>
files modified by this revision
cubicweb/web/views/json.py
# HG changeset patch
# User David Douard <david.douard@logilab.fr>
# Date 1478078237 -3600
# Wed Nov 02 10:17:17 2016 +0100
# Node ID c1992b98a99213188b22100c6fd6a9e79698ff9e
# Parent a4d465a3e77d07cf6a79c121c10b2d6484cd7468
[views] Make JsonMixIn.wdata method usable with non-web connections

diff --git a/cubicweb/web/views/json.py b/cubicweb/web/views/json.py
@@ -80,14 +80,16 @@
1      templatable = False
2      content_type = 'application/json'
3      binary = True
4 
5      def wdata(self, data):
6 -        if '_indent' in self._cw.form:
7 -            indent = int(self._cw.form['_indent'])
8 -        else:
9 -            indent = None
10 +        indent = None
11 +        try:
12 +            if '_indent' in self._cw.form:
13 +                indent = int(self._cw.form['_indent'])
14 +        except AttributeError:
15 +            pass  # may occur with non-web connection
16          # python's json.dumps escapes non-ascii characters
17          self.w(json_dumps(data, indent=indent).encode('ascii'))
18 
19 
20  class JsonRsetView(JsonMixIn, AnyRsetView):