forked from orbit-oss/flask
endpoint is optional for modules. This fixes #86
This commit is contained in:
parent
b1790cca55
commit
f5b8c08284
4 changed files with 35 additions and 7 deletions
|
|
@ -23,7 +23,7 @@ from werkzeug.routing import Map, Rule
|
||||||
from werkzeug.exceptions import HTTPException, InternalServerError, NotFound
|
from werkzeug.exceptions import HTTPException, InternalServerError, NotFound
|
||||||
|
|
||||||
from .helpers import _PackageBoundObject, url_for, get_flashed_messages, \
|
from .helpers import _PackageBoundObject, url_for, get_flashed_messages, \
|
||||||
_tojson_filter
|
_tojson_filter, _endpoint_from_view_func
|
||||||
from .wrappers import Request, Response
|
from .wrappers import Request, Response
|
||||||
from .config import ConfigAttribute, Config
|
from .config import ConfigAttribute, Config
|
||||||
from .ctx import _RequestContext
|
from .ctx import _RequestContext
|
||||||
|
|
@ -496,9 +496,7 @@ class Flask(_PackageBoundObject):
|
||||||
added and handled by the standard request handling.
|
added and handled by the standard request handling.
|
||||||
"""
|
"""
|
||||||
if endpoint is None:
|
if endpoint is None:
|
||||||
assert view_func is not None, 'expected view func if endpoint ' \
|
endpoint = _endpoint_from_view_func(view_func)
|
||||||
'is not provided.'
|
|
||||||
endpoint = view_func.__name__
|
|
||||||
options['endpoint'] = endpoint
|
options['endpoint'] = endpoint
|
||||||
methods = options.pop('methods', ('GET',))
|
methods = options.pop('methods', ('GET',))
|
||||||
provide_automatic_options = False
|
provide_automatic_options = False
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,15 @@ else:
|
||||||
_tojson_filter = json.dumps
|
_tojson_filter = json.dumps
|
||||||
|
|
||||||
|
|
||||||
|
def _endpoint_from_view_func(view_func):
|
||||||
|
"""Internal helper that returns the default endpoint for a given
|
||||||
|
function. This always is the function name.
|
||||||
|
"""
|
||||||
|
assert view_func is not None, 'expected view func if endpoint ' \
|
||||||
|
'is not provided.'
|
||||||
|
return view_func.__name__
|
||||||
|
|
||||||
|
|
||||||
def jsonify(*args, **kwargs):
|
def jsonify(*args, **kwargs):
|
||||||
"""Creates a :class:`~flask.Response` with the JSON representation of
|
"""Creates a :class:`~flask.Response` with the JSON representation of
|
||||||
the given arguments with an `application/json` mimetype. The arguments
|
the given arguments with an `application/json` mimetype. The arguments
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
:license: BSD, see LICENSE for more details.
|
:license: BSD, see LICENSE for more details.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from .helpers import _PackageBoundObject
|
from .helpers import _PackageBoundObject, _endpoint_from_view_func
|
||||||
|
|
||||||
|
|
||||||
def _register_module(module, static_path):
|
def _register_module(module, static_path):
|
||||||
|
|
@ -127,15 +127,24 @@ class Module(_PackageBoundObject):
|
||||||
return f
|
return f
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
def add_url_rule(self, rule, endpoint, view_func=None, **options):
|
def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
|
||||||
"""Like :meth:`Flask.add_url_rule` but for a module. The endpoint for
|
"""Like :meth:`Flask.add_url_rule` but for a module. The endpoint for
|
||||||
the :func:`url_for` function is prefixed with the name of the module.
|
the :func:`url_for` function is prefixed with the name of the module.
|
||||||
|
|
||||||
|
.. versionchanged:: 0.6
|
||||||
|
The `endpoint` argument is now optional and will default to the
|
||||||
|
function name to consistent with the function of the same name
|
||||||
|
on the application object.
|
||||||
"""
|
"""
|
||||||
def register_rule(state):
|
def register_rule(state):
|
||||||
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
|
||||||
state.app.add_url_rule(the_rule, '%s.%s' % (self.name, endpoint),
|
the_endpoint = endpoint
|
||||||
|
if the_endpoint is None:
|
||||||
|
the_endpoint = _endpoint_from_view_func(view_func)
|
||||||
|
state.app.add_url_rule(the_rule, '%s.%s' % (self.name,
|
||||||
|
the_endpoint),
|
||||||
view_func, **options)
|
view_func, **options)
|
||||||
self._record(register_rule)
|
self._record(register_rule)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -612,6 +612,18 @@ class ModuleTestCase(unittest.TestCase):
|
||||||
assert c.get('/admin/login').data == 'admin login'
|
assert c.get('/admin/login').data == 'admin login'
|
||||||
assert c.get('/admin/logout').data == 'admin logout'
|
assert c.get('/admin/logout').data == 'admin logout'
|
||||||
|
|
||||||
|
def test_default_endpoint_name(self):
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
mod = flask.Module(__name__, 'frontend')
|
||||||
|
def index():
|
||||||
|
return 'Awesome'
|
||||||
|
mod.add_url_rule('/', view_func=index)
|
||||||
|
app.register_module(mod)
|
||||||
|
rv = app.test_client().get('/')
|
||||||
|
assert rv.data == 'Awesome'
|
||||||
|
with app.test_request_context():
|
||||||
|
assert flask.url_for('frontend.index') == '/'
|
||||||
|
|
||||||
def test_request_processing(self):
|
def test_request_processing(self):
|
||||||
catched = []
|
catched = []
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue