web/views/json.py
changeset 7876 df15d194a134
child 7877 662ad647306f
equal deleted inserted replaced
7875:65e460690139 7876:df15d194a134
       
     1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """json export views"""
       
    19 
       
    20 __docformat__ = "restructuredtext en"
       
    21 _ = unicode
       
    22 
       
    23 from cubicweb.utils import json_dumps
       
    24 from cubicweb.view import EntityView, AnyRsetView
       
    25 from cubicweb.web.application import anonymized_request
       
    26 from cubicweb.web.views import basecontrollers
       
    27 
       
    28 class JsonpController(basecontrollers.ViewController):
       
    29     """The jsonp controller is the same as a ViewController but :
       
    30 
       
    31     - anonymize request (avoid CSRF attacks)
       
    32     - if ``vid`` parameter is passed, make sure it's sensible (i.e. either
       
    33       "jsonexport" or "ejsonexport")
       
    34     - if ``callback`` request parameter is passed, it's used as json padding
       
    35 
       
    36 
       
    37     Response's content-type will either be ``application/javascript`` or
       
    38     ``application/json`` depending on ``callback`` parameter presence or not.
       
    39     """
       
    40     __regid__ = 'jsonp'
       
    41 
       
    42     def publish(self, rset=None):
       
    43         if 'vid' in self._cw.form:
       
    44             vid = self._cw.form['vid']
       
    45             if vid not in ('jsonexport', 'ejsonexport'):
       
    46                 self.warning("vid %s can't be used with jsonp controller, "
       
    47                              "falling back to jsonexport", vid)
       
    48                 self._cw.form['vid'] = 'jsonexport'
       
    49         else: # if no vid is specified, use jsonexport
       
    50             self._cw.form['vid'] = 'jsonexport'
       
    51         with anonymized_request(self._cw):
       
    52             json_data = super(JsonpController, self).publish(rset)
       
    53             if 'callback' in self._cw.form: # jsonp
       
    54                 json_padding = self._cw.form['callback']
       
    55                 # use ``application/javascript`` is ``callback`` parameter is
       
    56                 # provided, let ``application/json`` otherwise
       
    57                 self._cw.set_content_type('application/javascript')
       
    58                 json_data = '%s(%s)' % (json_padding, json_data)
       
    59         return json_data
       
    60 
       
    61 
       
    62 class JsonMixIn(object):
       
    63     """mixin class for json views
       
    64 
       
    65     Handles the following optional request parameters:
       
    66 
       
    67     - ``_indent`` : must be an integer. If found, it is used to pretty print
       
    68       json output
       
    69     """
       
    70     templatable = False
       
    71     content_type = 'application/json'
       
    72     binary = True
       
    73 
       
    74     def wdata(self, data):
       
    75         if '_indent' in self._cw.form:
       
    76             indent = int(self._cw.form['_indent'])
       
    77         else:
       
    78             indent = None
       
    79         self.w(json_dumps(data, indent=indent))
       
    80 
       
    81 
       
    82 class JsonRsetView(JsonMixIn, AnyRsetView):
       
    83     """dumps raw result set in JSON format"""
       
    84     __regid__ = 'jsonexport'
       
    85     title = _('json-export-view')
       
    86 
       
    87     def call(self):
       
    88         # XXX mimic w3c recommandations to serialize SPARQL results in json ?
       
    89         #     http://www.w3.org/TR/rdf-sparql-json-res/
       
    90         self.wdata(self.cw_rset.rows)
       
    91 
       
    92 
       
    93 class JsonEntityView(JsonMixIn, EntityView):
       
    94     """dumps rset entities in JSON
       
    95 
       
    96     The following additional metadata is added to each row :
       
    97 
       
    98     - ``__cwetype__`` : entity type
       
    99     """
       
   100     __regid__ = 'ejsonexport'
       
   101     title = _('json-entities-export-view')
       
   102 
       
   103     def call(self):
       
   104         entities = []
       
   105         for entity in self.cw_rset.entities():
       
   106             entity.complete() # fetch all attributes
       
   107             # hack to add extra metadata
       
   108             entity.cw_attr_cache.update({
       
   109                     '__cwetype__': entity.__regid__,
       
   110                     })
       
   111             entities.append(entity)
       
   112         self.wdata(entities)