Skip to content
Myr

Allow passing the endpoint to the route decorators on Flask's `application` and `blueprints`.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Pedro Algarvio · committed by Pedro Algarvio · parent 1bca65d · Browse files · 3 files+64 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+64 added−2 removed

  • tests:+60 added−0 removed
  • other:+4 added−2 removed

3 files+64 added−2 removed

modified:flask/app.py+2 added-1 removed
940 unmodified lines
941 :param options: other options to be forwarded to the underlying942 :class:`~werkzeug.routing.Rule` object.943 """944 def decorator(f):945 endpoint = options.pop("endpoint", None)945 self.add_url_rule(rule, None, f, **options)946 self.add_url_rule(rule, endpoint, f, **options)947 return f948 return decorator949950 @setupmethod
587 unmodified lines
modified:flask/blueprints.py+2 added-1 removed
155 unmodified lines
156 """Like :meth:`Flask.route` but for a blueprint. The endpoint for the157 :func:`url_for` function is prefixed with the name of the blueprint.158 """159 def decorator(f):160 endpoint = options.pop("endpoint", f.__name__)160 self.add_url_rule(rule, f.__name__, f, **options)161 self.add_url_rule(rule, endpoint, f, **options)162 return f163 return decorator164165 def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
154 unmodified lines
modified:tests/flask_tests.py+60 added
1003 unmodified lines
1004 with app.test_client() as c:1005 rv = c.post('/foo', data={}, follow_redirects=True)1006 self.assertEqual(rv.data, 'success')10071008 def test_route_decorator_custom_endpoint(self):1009 app = flask.Flask(__name__)1010 app.debug = True10111012 @app.route('/foo/')1013 def foo():1014 return flask.request.endpoint10151016 @app.route('/bar/', endpoint='bar')1017 def for_bar():1018 return flask.request.endpoint10191020 @app.route('/bar/123', endpoint='123')1021 def for_bar_foo():1022 return flask.request.endpoint10231024 with app.test_request_context():1025 assert flask.url_for('foo') == '/foo/'1026 assert flask.url_for('bar') == '/bar/'1027 assert flask.url_for('123') == '/bar/123'10281029 c = app.test_client()1030 self.assertEqual(c.get('/foo/').data, 'foo')1031 self.assertEqual(c.get('/bar/').data, 'bar')1032 self.assertEqual(c.get('/bar/123').data, '123')103310341035class InstanceTestCase(unittest.TestCase):10361037 def test_explicit_instance_paths(self):1038 here = os.path.abspath(os.path.dirname(__file__))
673 unmodified lines
1712 c = app.test_client()1713 self.assertEqual(c.get('/').data, '1')1714 self.assertEqual(c.get('/page/2').data, '2')17151716 def test_route_decorator_custom_endpoint(self):17171718 bp = flask.Blueprint('bp', __name__)17191720 @bp.route('/foo')1721 def foo():1722 return flask.request.endpoint17231724 @bp.route('/bar', endpoint='bar')1725 def foo_bar():1726 return flask.request.endpoint17271728 @bp.route('/bar/123', endpoint='123')1729 def foo_bar_foo():1730 return flask.request.endpoint17311732 @bp.route('/bar/foo')1733 def bar_foo():1734 return flask.request.endpoint17351736 app = flask.Flask(__name__)1737 app.register_blueprint(bp, url_prefix='/py')17381739 @app.route('/')1740 def index():1741 return flask.request.endpoint17421743 c = app.test_client()1744 self.assertEqual(c.get('/').data, 'index')1745 self.assertEqual(c.get('/py/foo').data, 'bp.foo')1746 self.assertEqual(c.get('/py/bar').data, 'bp.bar')1747 self.assertEqual(c.get('/py/bar/123').data, 'bp.123')1748 self.assertEqual(c.get('/py/bar/foo').data, 'bp.bar_foo')174917501751class SendfileTestCase(unittest.TestCase):17521753 def test_send_file_regular(self):1754 app = flask.Flask(__name__)
507 unmodified lines