Skip to content
Myr

Added support for basic URL generation without request contexts.

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 4728823 · Browse files · 4 files+87 added−21 removed
This commit

State

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

Diff stats

4 files+87 added−21 removed

  • other:+87 added−21 removed

4 files+87 added−21 removed

added:flask/testsuite/appctx.py+38 added
1# -*- coding: utf-8 -*-2"""3 flask.testsuite.appctx4 ~~~~~~~~~~~~~~~~~~~~~~56 Tests the application context.78 :copyright: (c) 2012 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112from __future__ import with_statement1314import flask15import unittest16from flask.testsuite import FlaskTestCase171819class AppContextTestCase(FlaskTestCase):2021 def test_basic_support(self):22 app = flask.Flask(__name__)23 app.config['SERVER_NAME'] = 'localhost'24 app.config['PREFERRED_URL_SCHEME'] = 'https'2526 @app.route('/')27 def index():28 pass2930 with app.app_context():31 rv = flask.url_for('index')32 self.assert_equal(rv, 'https://localhost/')333435def suite():36 suite = unittest.TestSuite()37 suite.addTest(unittest.makeSuite(AppContextTestCase))38 return suite
modified:flask/app.py+16 added-3 removed
248 unmodified lines
249 'SESSION_COOKIE_HTTPONLY': True,250 'SESSION_COOKIE_SECURE': False,251 'MAX_CONTENT_LENGTH': None,252 'TRAP_BAD_REQUEST_ERRORS': False,253 'TRAP_HTTP_EXCEPTIONS': False253 'TRAP_HTTP_EXCEPTIONS': False,254 'PREFERRED_URL_SCHEME': 'http'255 })256257 #: The rule object to use for URL rules created. This is used by258 #: :meth:`add_url_rule`. Defaults to :class:`werkzeug.routing.Rule`.
1111 unmodified lines
1370 is created at a point where the request context is not yet set up1371 so the request is passed explicitly.13721373 .. versionadded:: 0.613741375 .. versionchanged:: 0.91376 This can now also be called without a request object when the1377 UR adapter is created for the application context.1378 """1379 if request is not None:1374 return self.url_map.bind_to_environ(request.environ,1375 server_name=self.config['SERVER_NAME'])1380 return self.url_map.bind_to_environ(request.environ,1381 server_name=self.config['SERVER_NAME'])1382 # We need at the very least the server name to be set for this1383 # to work.1384 if self.config['SERVER_NAME'] is not None:1385 return self.url_map.bind(1386 self.config['SERVER_NAME'],1387 script_name=self.config['APPLICATION_ROOT'] or '/',1388 url_scheme=self.config['PREFERRED_URL_SCHEME'])13891390 def inject_url_defaults(self, endpoint, values):1391 """Injects the URL defaults for the given endpoint directly into1392 the values dictionary passed. This is used internally and
180 unmodified lines
modified:flask/ctx.py+1 added
68 unmodified lines
69 """7071 def __init__(self, app):72 self.app = app73 self.url_adapter = app.create_url_adapter(None)7475 def push(self):76 """Binds the app context to the current context."""77 _app_ctx_stack.push(self)
155 unmodified lines
modified:flask/helpers.py+32 added-18 removed
48 unmodified lines
49 from werkzeug.utils import wrap_file5051from jinja2 import FileSystemLoader5253from .globals import session, _request_ctx_stack, current_app, request53from .globals import session, _request_ctx_stack, _app_ctx_stack, \54 current_app, request555657def _assert_have_json():58 """Helper function that fails if JSON is unavailable."""
138 unmodified lines
197 :param _external: if set to `True`, an absolute URL is generated.198 :param _anchor: if provided this is added as anchor to the URL.199 :param _method: if provided this explicitly specifies an HTTP method.200 """201 appctx = _app_ctx_stack.top202 reqctx = _request_ctx_stack.top203204 # If request specific information is available we have some extra205 # features that support "relative" urls.206 if reqctx is not None:200 ctx = _request_ctx_stack.top201 blueprint_name = request.blueprint202 if not ctx.request._is_old_module:203 if endpoint[:1] == '.':204 if blueprint_name is not None:205 endpoint = blueprint_name + endpoint206 else:207 url_adapter = reqctx.url_adapter208 blueprint_name = request.blueprint209 if not reqctx.request._is_old_module:210 if endpoint[:1] == '.':211 if blueprint_name is not None:212 endpoint = blueprint_name + endpoint213 else:214 endpoint = endpoint[1:]215 else:216 # TODO: get rid of this deprecated functionality in 1.0217 if '.' not in endpoint:218 if blueprint_name is not None:219 endpoint = blueprint_name + '.' + endpoint220 elif endpoint.startswith('.'):221 endpoint = endpoint[1:]222 external = values.pop('_external', False)223224 # Otherwise go with the url adapter from the appctx and make225 # the urls external by default.226 else:209 # TODO: get rid of this deprecated functionality in 1.0210 if '.' not in endpoint:211 if blueprint_name is not None:212 endpoint = blueprint_name + '.' + endpoint213 elif endpoint.startswith('.'):214 endpoint = endpoint[1:]215 external = values.pop('_external', False)227 url_adapter = appctx.url_adapter228 external = values.pop('_external', True)229230 anchor = values.pop('_anchor', None)231 method = values.pop('_method', None)218 ctx.app.inject_url_defaults(endpoint, values)219 rv = ctx.url_adapter.build(endpoint, values, method=method,220 force_external=external)232 appctx.app.inject_url_defaults(endpoint, values)233 rv = url_adapter.build(endpoint, values, method=method,234 force_external=external)235 if anchor is not None:236 rv += '#' + url_quote(anchor)237 return rv238
464 unmodified lines