From bd473c158788bd2c1153fc8fe45831aaa648824a Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 6 Jul 2011 10:16:56 +0200 Subject: [PATCH] Fixed an issue that broke url processors for blueprints. Added testcases --- flask/blueprints.py | 4 +-- tests/flask_tests.py | 58 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/flask/blueprints.py b/flask/blueprints.py index a0dfd521..0c3a7d0f 100644 --- a/flask/blueprints.py +++ b/flask/blueprints.py @@ -56,7 +56,7 @@ class BlueprintSetupState(object): #: A dictionary with URL defaults that is added to each and every #: URL that was defined with the blueprint. - self.url_defaults = dict(self.blueprint.url_defaults) + self.url_defaults = dict(self.blueprint.url_values_defaults) self.url_defaults.update(self.options.get('url_defaults', ())) def add_url_rule(self, rule, endpoint=None, view_func=None, **options): @@ -102,7 +102,7 @@ class Blueprint(_PackageBoundObject): self.view_functions = {} if url_defaults is None: url_defaults = {} - self.url_defaults = url_defaults + self.url_values_defaults = url_defaults def record(self, func): """Registers a function that is called when the blueprint is diff --git a/tests/flask_tests.py b/tests/flask_tests.py index fc99cd16..e13dfd36 100644 --- a/tests/flask_tests.py +++ b/tests/flask_tests.py @@ -830,6 +830,37 @@ class BasicFunctionalityTestCase(unittest.TestCase): rv = c.post('/accept', data={'myfile': 'foo' * 100}) assert rv.data == '42' + def test_url_processors(self): + app = flask.Flask(__name__) + + @app.url_defaults + def add_language_code(endpoint, values): + if flask.g.lang_code is not None and \ + app.url_map.is_endpoint_expecting(endpoint, 'lang_code'): + values.setdefault('lang_code', flask.g.lang_code) + + @app.url_value_preprocessor + def pull_lang_code(endpoint, values): + flask.g.lang_code = values.pop('lang_code', None) + + @app.route('//') + def index(): + return flask.url_for('about') + + @app.route('//about') + def about(): + return flask.url_for('something_else') + + @app.route('/foo') + def something_else(): + return flask.url_for('about', lang_code='en') + + c = app.test_client() + + self.assertEqual(c.get('/de/').data, '/de/about') + self.assertEqual(c.get('/de/about').data, '/foo') + self.assertEqual(c.get('/foo').data, '/en/about') + class JSONTestCase(unittest.TestCase): @@ -1309,6 +1340,33 @@ class BlueprintTestCase(unittest.TestCase): self.assertEqual(c.get('/1/bar').data, u'23') self.assertEqual(c.get('/2/bar').data, u'19') + def test_blueprint_url_processors(self): + bp = flask.Blueprint('frontend', __name__, url_prefix='/') + + @bp.url_defaults + def add_language_code(endpoint, values): + values.setdefault('lang_code', flask.g.lang_code) + + @bp.url_value_preprocessor + def pull_lang_code(endpoint, values): + flask.g.lang_code = values.pop('lang_code') + + @bp.route('/') + def index(): + return flask.url_for('.about') + + @bp.route('/about') + def about(): + return flask.url_for('.index') + + app = flask.Flask(__name__) + app.register_blueprint(bp) + + c = app.test_client() + + self.assertEqual(c.get('/de/').data, '/de/about') + self.assertEqual(c.get('/de/about').data, '/de/') + def test_templates_and_static(self): from blueprintapp import app c = app.test_client()