Skip to content

Commit

Permalink
Merge pull request #33 from sloria/target-to-location
Browse files Browse the repository at this point in the history
Rename `target` -> `location`
  • Loading branch information
sloria committed Mar 2, 2015
2 parents 9b36d16 + d11f07c commit 17b0430
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 136 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ Changelog
0.11.0 (unreleased)
*******************

Changes:

* Add ``dest`` parameter to ``Arg`` constructor which determines the key to be added to the parsed arguments dictionary (:issue:`32`).
* *Backwards-incompatible*: Rename ``targets`` parameter to ``locations`` in ``Parser`` constructor, ``Parser#parse_arg``, ``Parser#parse``, ``Parser#use_args``, and ``Parser#use_kwargs``.
* *Backwards-incompatible*: Rename ``Parser#target_handler`` to ``Parser#location_handler``.

Deprecation:

* *Deprecation*: The ``source`` parameter is deprecated in favor of the ``dest`` parameter.

Bug fixes:

* Fix ``validate`` parameter of ``DjangoParser#use_args``.

0.10.0 (2014-12-23)
*******************
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ webargs
.. image:: https://badge.fury.io/py/webargs.png
:target: http://badge.fury.io/py/webargs

.. image:: https://travis-ci.org/sloria/webargs.png?branch=master
.. image:: https://travis-ci.org/sloria/webargs.png?branch=pypi
:target: https://travis-ci.org/sloria/webargs

Homepage: https://webargs.readthedocs.org/
Expand Down
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. _changelog:

.. include:: ../HISTORY.rst
.. include:: ../changelog.rst
22 changes: 11 additions & 11 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ Below are examples of the various parameters that :class:`Arg <webarsg.core.Arg>
'nickname': Arg(str, multiple=True),
# When you know where an argument should be parsed from
'active': Arg(bool, target='query')
'active': Arg(bool, location='query')
# When value is keyed on a variable-unsafe name or you want to rename a key
'content_type': Arg(str, source='Content-Type')
'Content-Type': Arg(str, dest='content_type')
}
To parse request arguments, use the :meth:`parse <webargs.core.Parser.parse>` method of a :class:`Parser <webargs.core.Parser>` object.
Expand Down Expand Up @@ -172,16 +172,16 @@ Alternatively, you can decorate your view with :meth:`use_args <webargs.core.Par
return render_template('settings.html', username=username, nickname=nickname)
By default, webargs will search for arguments from the URL querystring (e.g. ``"/?name=foo"``), form data, and JSON data (in that order). You can explicitly specify which targets to search, like so:
By default, webargs will search for arguments from the URL querystring (e.g. ``"/?name=foo"``), form data, and JSON data (in that order). You can explicitly specify which locations to search, like so:

.. code-block:: python
@app.route('/register')
@use_args(user_args, targets=('json', 'form'))
@use_args(user_args, locations=('json', 'form'))
def register(args):
return 'registration page'
Available targets include:
Available locations include:

- ``'querystring'`` (same as ``'query'``)
- ``'json'``
Expand All @@ -190,23 +190,23 @@ Available targets include:
- ``'cookies'``
- ``'files'``

Adding Custom Target Handlers
-----------------------------
Adding Custom Location Handlers
-------------------------------

To add your own custom target handler, write a function that receives a request, an argument name, and an :class:`Arg <webargs.core.Arg>` object, then decorate that function with :func:`Parser.target_handler <webargs.core.Parser.target_handler>`.
To add your own custom location handler, write a function that receives a request, an argument name, and an :class:`Arg <webargs.core.Arg>` object, then decorate that function with :func:`Parser.location_handler <webargs.core.Parser.location_handler>`.


.. code-block:: python
from webargs.flaskparser import parser
@parser.target_handler('data')
@parser.location_handler('data')
def parse_data(request, name, arg):
return request.data.get(name)
# Now 'data' can be specified as a target
# Now 'data' can be specified as a location
@parser.use_args({'per_page': Arg(int)}, targets=('data', ))
@parser.use_args({'per_page': Arg(int)}, locations=('data', ))
def posts(args):
return 'displaying {} posts'.format(args['per_page'])
Expand Down
10 changes: 5 additions & 5 deletions tests/test_bottleparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def foo(myvalue, name):
def test_parsing_headers(app, testapp):
@app.route('/echo2')
def echo2():
args = parser.parse(hello_args, request, targets=('headers',))
args = parser.parse(hello_args, request, locations=('headers',))
return args
res = testapp.get('/echo2', headers={'name': 'Fred'}).json
assert res == {'name': 'Fred'}
Expand All @@ -130,15 +130,15 @@ def setcookie():

@app.route('/echocookie')
def echocookie():
args = parser.parse(hello_args, request, targets=('cookies',))
args = parser.parse(hello_args, request, locations=('cookies',))
return args
testapp.get('/setcookie')
assert testapp.get('/echocookie').json == {'name': 'Fred'}

def test_arg_specific_targets(app, testapp):
def test_arg_specific_locations(app, testapp):
testargs = {
'name': Arg(str, target='json'),
'age': Arg(int, target='querystring'),
'name': Arg(str, location='json'),
'age': Arg(int, location='querystring'),
}

@app.route('/echo', method=['POST'])
Expand Down

0 comments on commit 17b0430

Please sign in to comment.