Make it easier to associate a rule, endpoint and view_function
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This resolves http://github.com/mitsuhiko/flask/issues/issue/11
authored by unknown · committed by Armin Ronacher · parent 36f105c · Browse files · 2 files·+33 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+33 added−3 removed
- tests:+24 added−0 removed
- other:+9 added−3 removed
Provenance
- Commit
3088a93- Tree
f776a7f- Author
unknown <C:\Users\cgrinds\AppData\Roaming\The Bat!>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
36f105c
2 files changed+33 added−3 removed
modified:tests/flask_tests.py+24 added
55 unmodified lines56 rv = c.delete('/more')57 assert rv.status_code == 40558 assert sorted(rv.allow) == ['GET', 'HEAD', 'POST']59 60+ def test_url_mapping(self):61+ app = flask.Flask(__name__)62+ def index():63+ return flask.request.method64+ def more():65+ return flask.request.method66+ 67+ app.add_url_rule('/', 'index', index)68+ app.add_url_rule('/more', 'more', more, methods=['GET', 'POST'])69+70+ c = app.test_client()71+ assert c.get('/').data == 'GET'72+ rv = c.post('/')73+ assert rv.status_code == 40574+ assert sorted(rv.allow) == ['GET', 'HEAD']75+ rv = c.head('/')76+ assert rv.status_code == 20077+ assert not rv.data # head truncates78+ assert c.post('/more').data == 'POST'79+ assert c.get('/more').data == 'GET'80+ rv = c.delete('/more')81+ assert rv.status_code == 40582+ assert sorted(rv.allow) == ['GET', 'HEAD', 'POST']83+84 def test_session(self):85 app = flask.Flask(__name__)86 app.secret_key = 'testkey'87 @app.route('/set', methods=['POST'])179 unmodified linesmodified:flask.py+9 added-3 removed
479 unmodified lines480 :param response: an instance of :attr:`response_class`481 """482 session.save_cookie(response, self.session_cookie_name)483 484- def add_url_rule(self, rule, endpoint, **options):484+ def add_url_rule(self, rule, endpoint, view_func=None, **options):485 """Connects a URL rule. Works exactly like the :meth:`route`486- decorator but does not register the view function for the endpoint.486+ decorator. If a view_func is provided it will be registered with the endpoint.487 488 Basically this example::489 490 @app.route('/')3 unmodified lines494 Is equivalent to the following::495 496 def index():497 pass498- app.add_url_rule('index', '/')498+ app.add_url_rule('/', 'index', index)499+ 500+ If the view_func is not provided you will need to connect the endpoint to a 501+ view function like so:502 app.view_functions['index'] = index503 504 :param rule: the URL rule as string505 :param endpoint: the endpoint for the registered URL rule. Flask506 itself assumes the name of the view function as507 endpoint508+ :param view_func: the function to call when servicing a request to the provided endpoint509 :param options: the options to be forwarded to the underlying510 :class:`~werkzeug.routing.Rule` object511 """512 options['endpoint'] = endpoint513 options.setdefault('methods', ('GET',))514 self.url_map.add(Rule(rule, **options))515+ if view_func is not None:516+ self.view_functions[endpoint] = view_func517 518 def route(self, rule, **options):519 """A decorator that is used to register a view function for a520 given URL rule. Example::251 unmodified lines