Added support for _method to url_for()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 61a9519 · Browse files · 3 files·+33 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+33 added−2 removed
- other:+33 added−2 removed
Provenance
- Commit
f52e7a9- Tree
b125956- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
61a9519
3 files changed+33 added−2 removed
modified:flask/testsuite/helpers.py+26 added
302 unmodified lines303 with app.test_request_context():304 self.assert_equal(flask.url_for('index', _anchor='x y'),305 '/#x%20y')306 307+ def test_url_with_method(self):308+ from flask.views import MethodView309+ app = flask.Flask(__name__)310+ class MyView(MethodView):311+ def get(self, id=None):312+ if id is None:313+ return 'List'314+ return 'Get %d' % id315+ def post(self):316+ return 'Create'317+ myview = MyView.as_view('myview')318+ app.add_url_rule('/myview/', methods=['GET'],319+ view_func=myview)320+ app.add_url_rule('/myview/<int:id>', methods=['GET'],321+ view_func=myview)322+ app.add_url_rule('/myview/create', methods=['POST'],323+ view_func=myview)324 325+ with app.test_request_context():326+ self.assert_equal(flask.url_for('myview', _method='GET'),327+ '/myview/')328+ self.assert_equal(flask.url_for('myview', id=42, _method='GET'),329+ '/myview/42')330+ self.assert_equal(flask.url_for('myview', _method='POST'),331+ '/myview/create')332+333+334 def suite():335 suite = unittest.TestSuite()336 if flask.json_available:337 suite.addTest(unittest.makeSuite(JSONTestCase))3 unmodified linesmodified:flask/helpers.py+5 added-2 removed
185 unmodified lines186 187 For more information, head over to the :ref:`Quickstart <url-building>`.188 189 .. versionadded:: 0.9190- The `_anchor` parameter was added.190+ The `_anchor` and `_method` parameters were added.191 192 :param endpoint: the endpoint of the URL (name of the function)193 :param values: the variable arguments of the URL rule194 :param _external: if set to `True`, an absolute URL is generated.195 :param _anchor: if provided this is added as anchor to the URL.196+ :param _method: if provided this explicitly specifies an HTTP method.197 """198 ctx = _request_ctx_stack.top199 blueprint_name = request.blueprint200 if not ctx.request._is_old_module:10 unmodified lines211 elif endpoint.startswith('.'):212 endpoint = endpoint[1:]213 external = values.pop('_external', False)214 anchor = values.pop('_anchor', None)215+ method = values.pop('_method', None)216 ctx.app.inject_url_defaults(endpoint, values)215- rv = ctx.url_adapter.build(endpoint, values, force_external=external)217+ rv = ctx.url_adapter.build(endpoint, values, method=method,218+ force_external=external)219 if anchor is not None:220 rv += '#' + url_quote(anchor)221 return rv222 440 unmodified linesmodified:CHANGES+2 added
8 unmodified lines9 Relase date to be decided, codename to be chosen.10 11 - The :func:`flask.url_for` function now can generate anchors to the12 generated links.13+- The :func:`flask.url_for` function now can also explicitly generate14+ URL rules specific to a given HTTP method.15 - Logger now only returns the debug log setting if it was not set16 explicitly.17 18 Version 0.8.1296 unmodified lines