Skip to content
Myr

adding `_scheme` parameter to `url_for`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

In order to better facilitate generation of URLs that make use of an HTTPS URL scheme this patch adds a parameter with this specific purpose in mind. To achieve this we explicitly pass in a param, `_scheme='https'`, and then set the `url_scheme` attribute of our `MapAdapter` instance appropriately. Importantly, `_external=True` must be set in order for this to work properly. As such, failure to do so results in a `ValueError` being raised.

authored by Max Countryman · committed by Max Countryman · parent 61d3bbf · Browse files · 2 files+34 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+34 added−0 removed

  • other:+34 added−0 removed

2 files+34 added−0 removed

modified:flask/testsuite/helpers.py+22 added
395 unmodified lines
396 with app.test_request_context():397 self.assert_equal(flask.url_for('index', _anchor='x y'),398 '/#x%20y')399400 def test_url_for_with_scheme(self):401 app = flask.Flask(__name__)402 @app.route('/')403 def index():404 return '42'405 with app.test_request_context():406 self.assert_equal(flask.url_for('index',407 _external=True,408 _scheme='https'),409 'https://localhost/')410411 def test_url_for_with_scheme_not_external(self):412 app = flask.Flask(__name__)413 @app.route('/')414 def index():415 return '42'416 with app.test_request_context():417 self.assert_raises(ValueError,418 flask.url_for,419 'index',420 _scheme='https')421422 def test_url_with_method(self):423 from flask.views import MethodView424 app = flask.Flask(__name__)425 class MyView(MethodView):
108 unmodified lines
modified:flask/helpers.py+12 added
227 unmodified lines
228 `endpoint` and `**values` are the arguments passed into `url_for`. Note229 that this is for building URLs outside the current application, and not for230 handling 404 NotFound errors.231232 .. versionadded:: 0.10233 The `_scheme` parameter was added.234235 .. versionadded:: 0.9236 The `_anchor` and `_method` parameters were added.237238 .. versionadded:: 0.9
4 unmodified lines
243 :param values: the variable arguments of the URL rule244 :param _external: if set to `True`, an absolute URL is generated. Server245 address can be changed via `SERVER_NAME` configuration variable which246 defaults to `localhost`.247 :param _scheme: a string specifying the desired URL scheme. The `_external`248 parameter must be set to `True` or a `ValueError` is raised.249 :param _anchor: if provided this is added as anchor to the URL.250 :param _method: if provided this explicitly specifies an HTTP method.251 """252 appctx = _app_ctx_stack.top
34 unmodified lines
287 external = values.pop('_external', True)288289 anchor = values.pop('_anchor', None)290 method = values.pop('_method', None)291 scheme = values.pop('_scheme', None)292 appctx.app.inject_url_defaults(endpoint, values)293294 if scheme is not None:295 if not external:296 raise ValueError('When specifying _scheme, _external must be True')297 url_adapter.url_scheme = scheme298299 try:300 rv = url_adapter.build(endpoint, values, method=method,301 force_external=external)302 except BuildError, error:
539 unmodified lines