From 7680d52f42a562eda2e3f75b34118abb08d93414 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Fri, 23 Jul 2010 13:29:21 +0100 Subject: [PATCH] Added support for subdomain bound modules --- CHANGES | 3 +++ flask/app.py | 1 + flask/module.py | 12 ++++++++++-- tests/flask_tests.py | 21 +++++++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 842c1a9c..c1f50998 100644 --- a/CHANGES +++ b/CHANGES @@ -34,6 +34,9 @@ Release date to be announced, codename to be decided. - refactored the way url adapters are created. This process is now fully customizable with the :meth:`~flask.Flask.create_url_adapter` method. +- modules can now register for a subdomain instead of just an URL + prefix. This makes it possible to bind a whole module to a + configurable subdomain. .. _blinker: http://pypi.python.org/pypi/blinker diff --git a/flask/app.py b/flask/app.py index c5f50135..151525e6 100644 --- a/flask/app.py +++ b/flask/app.py @@ -454,6 +454,7 @@ class Flask(_PackageBoundObject): provided. """ options.setdefault('url_prefix', module.url_prefix) + options.setdefault('subdomain', module.subdomain) state = _ModuleSetupState(self, **options) for func in module._register_events: func(state) diff --git a/flask/module.py b/flask/module.py index 9eaa4f82..e6e1ee36 100644 --- a/flask/module.py +++ b/flask/module.py @@ -37,9 +37,10 @@ def _register_module(module, static_path): class _ModuleSetupState(object): - def __init__(self, app, url_prefix=None): + def __init__(self, app, url_prefix=None, subdomain=None): self.app = app self.url_prefix = url_prefix + self.subdomain = subdomain class Module(_PackageBoundObject): @@ -94,6 +95,9 @@ class Module(_PackageBoundObject): modules to refer to their own templates and static files. See :ref:`modules-and-resources` for more information. + .. versionadded:: 0.6 + The `subdomain` parameter was added. + :param import_name: the name of the Python package or module implementing this :class:`Module`. :param name: the internal short name for the module. Unless specified @@ -101,6 +105,8 @@ class Module(_PackageBoundObject): :param url_prefix: an optional string that is used to prefix all the URL rules of this module. This can also be specified when registering the module with the application. + :param subdomain: used to set the subdomain setting for URL rules that + do not have a subdomain setting set. :param static_path: can be used to specify a different path for the static files on the web. Defaults to ``/static``. This does not affect the folder the files are served @@ -108,7 +114,7 @@ class Module(_PackageBoundObject): """ def __init__(self, import_name, name=None, url_prefix=None, - static_path=None): + static_path=None, subdomain=None): if name is None: assert '.' in import_name, 'name required if package name ' \ 'does not point to a submodule' @@ -116,6 +122,7 @@ class Module(_PackageBoundObject): _PackageBoundObject.__init__(self, import_name) self.name = name self.url_prefix = url_prefix + self.subdomain = subdomain self._register_events = [_register_module(self, static_path)] def route(self, rule, **options): @@ -140,6 +147,7 @@ class Module(_PackageBoundObject): the_rule = rule if state.url_prefix: the_rule = state.url_prefix + rule + options.setdefault('subdomain', state.subdomain) the_endpoint = endpoint if the_endpoint is None: the_endpoint = _endpoint_from_view_func(view_func) diff --git a/tests/flask_tests.py b/tests/flask_tests.py index 5d5c3794..3000e41c 100644 --- a/tests/flask_tests.py +++ b/tests/flask_tests.py @@ -1038,6 +1038,27 @@ class SubdomainTestCase(unittest.TestCase): rv = c.get('/', 'http://mitsuhiko.localhost/') assert rv.data == 'index for mitsuhiko' + def test_module_subdomain_support(self): + app = flask.Flask(__name__) + mod = flask.Module(__name__, 'test', subdomain='testing') + app.config['SERVER_NAME'] = 'localhost' + + @mod.route('/test') + def test(): + return 'Test' + + @mod.route('/outside', subdomain='xtesting') + def bar(): + return 'Outside' + + app.register_module(mod) + + c = app.test_client() + rv = c.get('/test', 'http://testing.localhost/') + assert rv.data == 'Test' + rv = c.get('/outside', 'http://xtesting.localhost/') + assert rv.data == 'Outside' + class TestSignals(unittest.TestCase):