Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for subdomain bound modules

  • Loading branch information...
commit 7680d52f42a562eda2e3f75b34118abb08d93414 1 parent b49afa2
@mitsuhiko authored
Showing with 35 additions and 2 deletions.
  1. +3 −0  CHANGES
  2. +1 −0  flask/app.py
  3. +10 −2 flask/module.py
  4. +21 −0 tests/flask_tests.py
View
3  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
View
1  flask/app.py
@@ -454,6 +454,7 @@ def register_module(self, module, **options):
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)
View
12 flask/module.py
@@ -37,9 +37,10 @@ def _register(state):
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 @@ def login():
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 @@ def login():
: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 @@ def login():
"""
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 @@ def __init__(self, import_name, name=None, url_prefix=None,
_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 @@ def register_rule(state):
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)
View
21 tests/flask_tests.py
@@ -1038,6 +1038,27 @@ def index(user):
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):
Please sign in to comment.
Something went wrong with that request. Please try again.