Skip to content

Commit

Permalink
Merge pull request #47 from hartror/pyramid_matchdict
Browse files Browse the repository at this point in the history
Pyramid request matchdict support
  • Loading branch information
sloria committed Jun 16, 2015
2 parents 08adfdf + 85b0e03 commit b5c4bc0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,19 @@ When using the :meth:`use_args <webargs.pyramidparser.PyramidParser.use_args>` d
As with the other parser modules, :meth:`use_kwargs <webargs.pyramidparser.PyramidParser.use_kwargs>` will add keyword arguments to the view callable.

URL Matches
-----------

The :mod:`webargs.pyramidparser` module adds support for parsing values from the request matchdict.

.. code-block:: python
from pyramid.response import Response
from webargs.pyramidparser import use_args
@parser.use_args({'mymatch': Arg(int)}, locations=('matchdict',))
def matched(request, args):
return Response('The value for mymatch is {}'.format(args['mymatch'])))
API Reference
=============
Expand Down
10 changes: 10 additions & 0 deletions tests/test_pyramidparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def __call__(self, args):
def baz(request, myvalue):
return {'myvalue': myvalue}

@parser.use_args({'mymatch': Arg(int)}, locations=('matchdict',))
def matched(request, args):
return args

config = Configurator()

config.add_route('echo', '/echo')
Expand All @@ -76,6 +80,7 @@ def baz(request, myvalue):
config.add_route('foo', '/foo')
config.add_route('bar', '/bar')
config.add_route('baz', '/baz')
config.add_route('matched', '/matched/{mymatch:\d+}')

config.add_view(echo, route_name='echo', renderer='json')
config.add_view(echomulti, route_name='echomulti', renderer='json')
Expand All @@ -86,6 +91,7 @@ def baz(request, myvalue):
config.add_view(foo, route_name='foo', renderer='json')
config.add_view(Bar, route_name='bar', renderer='json')
config.add_view(baz, route_name='baz', renderer='json')
config.add_view(matched, route_name='matched', renderer='json')

app = config.make_wsgi_app()

Expand Down Expand Up @@ -143,3 +149,7 @@ def test_use_args_decorator_class(testapp):

def test_user_kwargs_decorator(testapp):
assert testapp.post('/baz', {'myvalue': 42}).json == {'myvalue': 42}

def test_parse_matchdict(testapp):
res = testapp.get('/matched/1')
assert res.json == {'mymatch': 1}
7 changes: 7 additions & 0 deletions webargs/pyramidparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def hello_world(request, args):
class PyramidParser(core.Parser):
"""Pyramid request argument parser."""

__location_map__ = dict(
matchdict='parse_matchdict',
**core.Parser.__location_map__)

def parse_querystring(self, req, name, arg):
"""Pull a querystring value from the request."""
return core.get_value(req.GET, name, arg.multiple)
Expand Down Expand Up @@ -69,6 +73,9 @@ def parse_files(self, req, name, arg):
files = ((k, v) for k, v in req.POST.items() if hasattr(v, 'file'))
return core.get_value(MultiDict(files), name, arg.multiple)

def parse_matchdict(self, req, name, arg):
return core.get_value(req.matchdict, name, arg.multiple)

def handle_error(self, error):
"""Handles errors during parsing. Aborts the current HTTP request and
responds with a 400 error.
Expand Down

0 comments on commit b5c4bc0

Please sign in to comment.