forked from orbit-oss/flask
Added support for subdomain bound modules
This commit is contained in:
parent
b49afa21ad
commit
7680d52f42
4 changed files with 35 additions and 2 deletions
3
CHANGES
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
|
- refactored the way url adapters are created. This process is now
|
||||||
fully customizable with the :meth:`~flask.Flask.create_url_adapter`
|
fully customizable with the :meth:`~flask.Flask.create_url_adapter`
|
||||||
method.
|
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
|
.. _blinker: http://pypi.python.org/pypi/blinker
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -454,6 +454,7 @@ class Flask(_PackageBoundObject):
|
||||||
provided.
|
provided.
|
||||||
"""
|
"""
|
||||||
options.setdefault('url_prefix', module.url_prefix)
|
options.setdefault('url_prefix', module.url_prefix)
|
||||||
|
options.setdefault('subdomain', module.subdomain)
|
||||||
state = _ModuleSetupState(self, **options)
|
state = _ModuleSetupState(self, **options)
|
||||||
for func in module._register_events:
|
for func in module._register_events:
|
||||||
func(state)
|
func(state)
|
||||||
|
|
|
||||||
|
|
@ -37,9 +37,10 @@ def _register_module(module, static_path):
|
||||||
|
|
||||||
class _ModuleSetupState(object):
|
class _ModuleSetupState(object):
|
||||||
|
|
||||||
def __init__(self, app, url_prefix=None):
|
def __init__(self, app, url_prefix=None, subdomain=None):
|
||||||
self.app = app
|
self.app = app
|
||||||
self.url_prefix = url_prefix
|
self.url_prefix = url_prefix
|
||||||
|
self.subdomain = subdomain
|
||||||
|
|
||||||
|
|
||||||
class Module(_PackageBoundObject):
|
class Module(_PackageBoundObject):
|
||||||
|
|
@ -94,6 +95,9 @@ class Module(_PackageBoundObject):
|
||||||
modules to refer to their own templates and static files. See
|
modules to refer to their own templates and static files. See
|
||||||
:ref:`modules-and-resources` for more information.
|
: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
|
:param import_name: the name of the Python package or module
|
||||||
implementing this :class:`Module`.
|
implementing this :class:`Module`.
|
||||||
:param name: the internal short name for the module. Unless specified
|
: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
|
:param url_prefix: an optional string that is used to prefix all the
|
||||||
URL rules of this module. This can also be specified
|
URL rules of this module. This can also be specified
|
||||||
when registering the module with the application.
|
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
|
:param static_path: can be used to specify a different path for the
|
||||||
static files on the web. Defaults to ``/static``.
|
static files on the web. Defaults to ``/static``.
|
||||||
This does not affect the folder the files are served
|
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,
|
def __init__(self, import_name, name=None, url_prefix=None,
|
||||||
static_path=None):
|
static_path=None, subdomain=None):
|
||||||
if name is None:
|
if name is None:
|
||||||
assert '.' in import_name, 'name required if package name ' \
|
assert '.' in import_name, 'name required if package name ' \
|
||||||
'does not point to a submodule'
|
'does not point to a submodule'
|
||||||
|
|
@ -116,6 +122,7 @@ class Module(_PackageBoundObject):
|
||||||
_PackageBoundObject.__init__(self, import_name)
|
_PackageBoundObject.__init__(self, import_name)
|
||||||
self.name = name
|
self.name = name
|
||||||
self.url_prefix = url_prefix
|
self.url_prefix = url_prefix
|
||||||
|
self.subdomain = subdomain
|
||||||
self._register_events = [_register_module(self, static_path)]
|
self._register_events = [_register_module(self, static_path)]
|
||||||
|
|
||||||
def route(self, rule, **options):
|
def route(self, rule, **options):
|
||||||
|
|
@ -140,6 +147,7 @@ class Module(_PackageBoundObject):
|
||||||
the_rule = rule
|
the_rule = rule
|
||||||
if state.url_prefix:
|
if state.url_prefix:
|
||||||
the_rule = state.url_prefix + rule
|
the_rule = state.url_prefix + rule
|
||||||
|
options.setdefault('subdomain', state.subdomain)
|
||||||
the_endpoint = endpoint
|
the_endpoint = endpoint
|
||||||
if the_endpoint is None:
|
if the_endpoint is None:
|
||||||
the_endpoint = _endpoint_from_view_func(view_func)
|
the_endpoint = _endpoint_from_view_func(view_func)
|
||||||
|
|
|
||||||
|
|
@ -1038,6 +1038,27 @@ class SubdomainTestCase(unittest.TestCase):
|
||||||
rv = c.get('/', 'http://mitsuhiko.localhost/')
|
rv = c.get('/', 'http://mitsuhiko.localhost/')
|
||||||
assert rv.data == 'index for mitsuhiko'
|
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):
|
class TestSignals(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue