endpoint is optional for modules. This fixes #86
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent b1790cc · Browse files · 4 files·+35 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+35 added−7 removed
- tests:+12 added−0 removed
- other:+23 added−7 removed
Provenance
- Commit
f5b8c08- Tree
1d27010- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
b1790cc
4 files changed+35 added−7 removed
modified:flask/app.py+2 added-4 removed
21 unmodified lines22 from werkzeug.routing import Map, Rule23 from werkzeug.exceptions import HTTPException, InternalServerError, NotFound24 25 from .helpers import _PackageBoundObject, url_for, get_flashed_messages, \26- _tojson_filter26+ _tojson_filter, _endpoint_from_view_func27 from .wrappers import Request, Response28 from .config import ConfigAttribute, Config29 from .ctx import _RequestContext30 from .globals import _request_ctx_stack, request464 unmodified lines495 Starting with Flask 0.6, `OPTIONS` is implicitly496 added and handled by the standard request handling.497 """498 if endpoint is None:499- assert view_func is not None, 'expected view func if endpoint ' \500- 'is not provided.'501- endpoint = view_func.__name__499+ endpoint = _endpoint_from_view_func(view_func)500 options['endpoint'] = endpoint501 methods = options.pop('methods', ('GET',))502 provide_automatic_options = False503 if 'OPTIONS' not in methods:352 unmodified linesmodified:flask/helpers.py+9 added
57 unmodified lines58 else:59 _tojson_filter = json.dumps60 61 62+def _endpoint_from_view_func(view_func):63+ """Internal helper that returns the default endpoint for a given64+ function. This always is the function name.65+ """66+ assert view_func is not None, 'expected view func if endpoint ' \67+ 'is not provided.'68+ return view_func.__name__69+70+71 def jsonify(*args, **kwargs):72 """Creates a :class:`~flask.Response` with the JSON representation of73 the given arguments with an `application/json` mimetype. The arguments74 to this function are the same as to the :class:`dict` constructor.348 unmodified linesmodified:flask/module.py+12 added-3 removed
7 unmodified lines8 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11 12-from .helpers import _PackageBoundObject12+from .helpers import _PackageBoundObject, _endpoint_from_view_func13 14 15 def _register_module(module, static_path):16 """Internal helper function that returns a function for recording109 unmodified lines126 self.add_url_rule(rule, f.__name__, f, **options)127 return f128 return decorator129 130- def add_url_rule(self, rule, endpoint, view_func=None, **options):130+ def add_url_rule(self, rule, endpoint=None, view_func=None, **options):131 """Like :meth:`Flask.add_url_rule` but for a module. The endpoint for132 the :func:`url_for` function is prefixed with the name of the module.133+134+ .. versionchanged:: 0.6135+ The `endpoint` argument is now optional and will default to the136+ function name to consistent with the function of the same name137+ on the application object.138 """139 def register_rule(state):140 the_rule = rule141 if state.url_prefix:142 the_rule = state.url_prefix + rule143+ the_endpoint = endpoint144+ if the_endpoint is None:145+ the_endpoint = _endpoint_from_view_func(view_func)138- state.app.add_url_rule(the_rule, '%s.%s' % (self.name, endpoint),146+ state.app.add_url_rule(the_rule, '%s.%s' % (self.name,147+ the_endpoint),148 view_func, **options)149 self._record(register_rule)150 151 def before_request(self, f):62 unmodified linesmodified:tests/flask_tests.py+12 added
610 unmodified lines611 assert c.get('/admin/').data == 'admin index'612 assert c.get('/admin/login').data == 'admin login'613 assert c.get('/admin/logout').data == 'admin logout'614 615+ def test_default_endpoint_name(self):616+ app = flask.Flask(__name__)617+ mod = flask.Module(__name__, 'frontend')618+ def index():619+ return 'Awesome'620+ mod.add_url_rule('/', view_func=index)621+ app.register_module(mod)622+ rv = app.test_client().get('/')623+ assert rv.data == 'Awesome'624+ with app.test_request_context():625+ assert flask.url_for('frontend.index') == '/'626+627 def test_request_processing(self):628 catched = []629 app = flask.Flask(__name__)630 admin = flask.Module(__name__, 'admin', url_prefix='/admin')399 unmodified lines