Browse Source

Started work on testcases for blueprints

pull/262/head
Armin Ronacher 14 years ago
parent
commit
e17e74d3a7
  1. 18
      flask/blueprints.py
  2. 21
      tests/flask_tests.py

18
flask/blueprints.py

@ -9,7 +9,6 @@
:copyright: (c) 2011 by Armin Ronacher. :copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
""" """
import os
from functools import update_wrapper from functools import update_wrapper
from .helpers import _PackageBoundObject, _endpoint_from_view_func from .helpers import _PackageBoundObject, _endpoint_from_view_func
@ -36,14 +35,24 @@ class BlueprintSetupState(object):
url_prefix = self.blueprint.url_prefix url_prefix = self.blueprint.url_prefix
self.url_prefix = url_prefix self.url_prefix = url_prefix
self.url_defaults = dict(self.blueprint.url_defaults)
self.url_defaults.update(self.options.get('url_defaults', ()))
def add_url_rule(self, rule, endpoint=None, view_func=None, **options): def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
"""A helper method to register a rule (and optionally a view function)
to the application. The endpoint is automatically prefixed with the
blueprint's name.
"""
if self.url_prefix: if self.url_prefix:
rule = self.url_prefix + rule rule = self.url_prefix + rule
options.setdefault('subdomain', self.subdomain) options.setdefault('subdomain', self.subdomain)
if endpoint is None: if endpoint is None:
endpoint = _endpoint_from_view_func(view_func) endpoint = _endpoint_from_view_func(view_func)
defaults = self.url_defaults
if 'defaults' in options:
defaults = dict(defaults, **options.pop('defaults'))
self.app.add_url_rule(rule, '%s.%s' % (self.blueprint.name, endpoint), self.app.add_url_rule(rule, '%s.%s' % (self.blueprint.name, endpoint),
view_func, **options) view_func, defaults=defaults, **options)
class Blueprint(_PackageBoundObject): class Blueprint(_PackageBoundObject):
@ -57,7 +66,7 @@ class Blueprint(_PackageBoundObject):
def __init__(self, name, import_name, static_folder=None, def __init__(self, name, import_name, static_folder=None,
static_url_path=None, template_folder=None, static_url_path=None, template_folder=None,
url_prefix=None, subdomain=None): url_prefix=None, subdomain=None, url_defaults=None):
_PackageBoundObject.__init__(self, import_name, template_folder) _PackageBoundObject.__init__(self, import_name, template_folder)
self.name = name self.name = name
self.url_prefix = url_prefix self.url_prefix = url_prefix
@ -66,6 +75,9 @@ class Blueprint(_PackageBoundObject):
self.static_url_path = static_url_path self.static_url_path = static_url_path
self.deferred_functions = [] self.deferred_functions = []
self.view_functions = {} self.view_functions = {}
if url_defaults is None:
url_defaults = {}
self.url_defaults = url_defaults
def record(self, func): def record(self, func):
"""Registers a function that is called when the blueprint is """Registers a function that is called when the blueprint is

21
tests/flask_tests.py

@ -1272,6 +1272,27 @@ class BlueprintTestCase(unittest.TestCase):
assert c.get('/backend-no').data == 'backend says no' assert c.get('/backend-no').data == 'backend says no'
assert c.get('/what-is-a-sideend').data == 'application itself says no' assert c.get('/what-is-a-sideend').data == 'application itself says no'
def test_blueprint_url_definitions(self):
bp = flask.Blueprint('test', __name__)
@bp.route('/foo', defaults={'baz': 42})
def foo(bar, baz):
return '%s/%d' % (bar, baz)
@bp.route('/bar')
def bar(bar):
return unicode(bar)
app = flask.Flask(__name__)
app.register_blueprint(bp, url_prefix='/1', url_defaults={'bar': 23})
app.register_blueprint(bp, url_prefix='/2', url_defaults={'bar': 19})
c = app.test_client()
self.assertEqual(c.get('/1/foo').data, u'23/42')
self.assertEqual(c.get('/2/foo').data, u'19/42')
self.assertEqual(c.get('/1/bar').data, u'23')
self.assertEqual(c.get('/2/bar').data, u'19')
class SendfileTestCase(unittest.TestCase): class SendfileTestCase(unittest.TestCase):

Loading…
Cancel
Save