From 95750b3287bfa21fbb706f10a3a9fbcef01c089e Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 19 Apr 2010 23:49:02 +0200 Subject: [PATCH] Documented some routing converter possibilities. --- docs/api.rst | 2 +- flask.py | 23 +++++++++++++++++++---- tests/flask_tests.py | 17 ++++++++++++++++- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index bd39d69e..122da451 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -106,7 +106,7 @@ Incoming Request Data `script_root` ``/myapplication`` `url` ``http://www.example.com/myapplication/page.html`` `base_url` ``http://www.example.com/myapplication/page.html?x=y`` - `root_url` ``http://www.example.com/myapplication/`` + `url_root` ``http://www.example.com/myapplication/`` ============= ====================================================== .. attribute:: is_xhr diff --git a/flask.py b/flask.py index c5a969ca..055b46cf 100644 --- a/flask.py +++ b/flask.py @@ -16,7 +16,7 @@ import sys from jinja2 import Environment, PackageLoader, FileSystemLoader from werkzeug import Request as RequestBase, Response as ResponseBase, \ LocalStack, LocalProxy, create_environ, SharedDataMiddleware, \ - cached_property + ImmutableDict, cached_property from werkzeug.routing import Map, Rule from werkzeug.exceptions import HTTPException from werkzeug.contrib.securecookie import SecureCookie @@ -306,7 +306,7 @@ class Flask(object): session_cookie_name = 'session' #: options that are passed directly to the Jinja2 environment - jinja_options = dict( + jinja_options = ImmutableDict( autoescape=True, extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_'] ) @@ -361,11 +361,26 @@ class Flask(object): #: decorator. self.template_context_processors = [_default_template_ctx_processor] + #: the :class:`~werkzeug.routing.Map` for this instance. You can use + #: this to change the routing converters after the class was created + #: but before any routes are connected. Example:: + #: + #: from werkzeug import BaseConverter + #: + #: class ListConverter(BaseConverter): + #: def to_python(self, value): + #: return value.split(',') + #: def to_url(self, values): + #: return ','.join(BaseConverter.to_url(value) + #: for value in values) + #: + #: app = Flask(__name__) + #: app.url_map.converters['list'] = ListConverter self.url_map = Map() if self.static_path is not None: - self.url_map.add(Rule(self.static_path + '/', - build_only=True, endpoint='static')) + self.add_url_rule(self.static_path + '/', + build_only=True, endpoint='static') if pkg_resources is not None: target = (self.package_name, 'static') else: diff --git a/tests/flask_tests.py b/tests/flask_tests.py index 15166e9a..60a6b7df 100644 --- a/tests/flask_tests.py +++ b/tests/flask_tests.py @@ -63,7 +63,7 @@ class BasicFunctionalityTestCase(unittest.TestCase): return flask.request.method def more(): return flask.request.method - + app.add_url_rule('/', 'index', index) app.add_url_rule('/more', 'more', more, methods=['GET', 'POST']) @@ -181,6 +181,21 @@ class BasicFunctionalityTestCase(unittest.TestCase): with app.test_request_context(): assert flask.url_for('hello', name='test x') == '/hello/test%20x' + def test_custom_converters(self): + from werkzeug.routing import BaseConverter + class ListConverter(BaseConverter): + def to_python(self, value): + return value.split(',') + def to_url(self, value): + return ','.join(super(ListConverter, self).to_url(x) for x in value) + app = flask.Flask(__name__) + app.url_map.converters['list'] = ListConverter + @app.route('/') + def index(args): + return '|'.join(args) + c = app.test_client() + assert c.get('/1,2,3').data == '1|2|3' + def test_static_files(self): app = flask.Flask(__name__) rv = app.test_client().get('/static/index.html')