Chris McDonough
2011-07-04 b78effb723e5a6b2f3980dac7830f8932abd7890
- New request attribute: ``json``. If the request's ``content_type`` is
``application/json``, this attribute will contain the JSON-decoded
variant of the request body. If the request's ``content_type`` is not
``application/json``, this attribute will be ``None``.
5 files modified
44 ■■■■■ changed files
CHANGES.txt 5 ●●●●● patch | view | raw | blame | history
docs/api/request.rst 7 ●●●●● patch | view | raw | blame | history
docs/whatsnew-1.1.rst 5 ●●●●● patch | view | raw | blame | history
pyramid/request.py 7 ●●●●● patch | view | raw | blame | history
pyramid/tests/test_request.py 20 ●●●●● patch | view | raw | blame | history
CHANGES.txt
@@ -4,6 +4,11 @@
Features
--------
- New request attribute: ``json``. If the request's ``content_type`` is
  ``application/json``, this attribute will contain the JSON-decoded
  variant of the request body.  If the request's ``content_type`` is not
  ``application/json``, this attribute will be ``None``.
- A new value ``http_cache`` can be used as a view configuration
  parameter.
docs/api/request.rst
@@ -180,6 +180,13 @@
      object (exposed to view code as ``request.response``) to influence
      rendered response behavior.
   .. attribute:: json
      If the request's ``content_type`` is ``application/json``, this
      attribute will contain the JSON-decoded variant of the request body.
      If the request's ``content_type`` is not ``application/json``, this
      attribute will be ``None``.
.. note::
   For information about the API of a :term:`multidict` structure (such as
docs/whatsnew-1.1.rst
@@ -94,6 +94,11 @@
Minor Feature Additions
-----------------------
- New request attribute: ``json``. If the request's ``content_type`` is
  ``application/json``, this attribute will contain the JSON-decoded
  variant of the request body.  If the request's ``content_type`` is not
  ``application/json``, this attribute will be ``None``.
- A new value ``http_cache`` can be used as a :term:`view configuration`
  parameter.
pyramid/request.py
@@ -10,6 +10,7 @@
from pyramid.interfaces import ISessionFactory
from pyramid.interfaces import IResponseFactory
from pyramid.compat import json
from pyramid.exceptions import ConfigurationError
from pyramid.decorator import reify
from pyramid.response import Response
@@ -489,6 +490,12 @@
            return False
        return adapted is ob
    @property
    def json(self):
        if self.content_type == 'application/json':
            return json.loads(self.body, encoding=self.charset)
def route_request_iface(name, bases=()):
    iface = InterfaceClass('%s_IRequest' % name, bases=bases)
    # for exception view lookups
pyramid/tests/test_request.py
@@ -233,6 +233,26 @@
        request.registry.registerAdapter(adapter, (Foo,), IResponse)
        self.assertEqual(request.is_response(foo), True)
    def test_json_incorrect_mimetype(self):
        request = self._makeOne({})
        self.assertEqual(request.json, None)
    def test_json_correct_mimetype(self):
        request = self._makeOne({})
        request.content_type = 'application/json'
        request.body = '{"a":1}'
        self.assertEqual(request.json, {'a':1})
    def test_json_alternate_charset(self):
        from pyramid.compat import json
        request = self._makeOne({})
        request.content_type = 'application/json'
        request.charset = 'latin-1'
        la = unicode('La Pe\xc3\xb1a', 'utf-8')
        body = json.dumps({'a':la}, encoding='latin-1')
        request.body = body
        self.assertEqual(request.json, {'a':la})
class TestRequestDeprecatedMethods(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()