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
Provenance
- Commit
68ec5a3- Tree
f8ba504- Author
Pedro Algarvio <pedro@algarvio.me>- Committer
Pedro Algarvio <pedro@algarvio.me>- Parent
1bca65d
3 files changed+64 added−2 removed
modified:flask/app.py+2 added-1 removed
940 unmodified lines941 :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 decorator949 950 @setupmethod587 unmodified linesmodified:flask/blueprints.py+2 added-1 removed
155 unmodified lines156 """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 decorator164 165 def add_url_rule(self, rule, endpoint=None, view_func=None, **options):154 unmodified linesmodified:tests/flask_tests.py+60 added
1003 unmodified lines1004 with app.test_client() as c:1005 rv = c.post('/foo', data={}, follow_redirects=True)1006 self.assertEqual(rv.data, 'success')1007 1008+ def test_route_decorator_custom_endpoint(self):1009+ app = flask.Flask(__name__)1010+ app.debug = True1011 1012+ @app.route('/foo/')1013+ def foo():1014+ return flask.request.endpoint1015+1016+ @app.route('/bar/', endpoint='bar')1017+ def for_bar():1018+ return flask.request.endpoint1019+1020+ @app.route('/bar/123', endpoint='123')1021+ def for_bar_foo():1022+ return flask.request.endpoint1023+1024+ 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'1028+1029+ 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')1033+1034+1035 class InstanceTestCase(unittest.TestCase):1036 1037 def test_explicit_instance_paths(self):1038 here = os.path.abspath(os.path.dirname(__file__))673 unmodified lines1712 c = app.test_client()1713 self.assertEqual(c.get('/').data, '1')1714 self.assertEqual(c.get('/page/2').data, '2')1715 1716+ def test_route_decorator_custom_endpoint(self):1717 1718+ bp = flask.Blueprint('bp', __name__)1719+1720+ @bp.route('/foo')1721+ def foo():1722+ return flask.request.endpoint1723+1724+ @bp.route('/bar', endpoint='bar')1725+ def foo_bar():1726+ return flask.request.endpoint1727+1728+ @bp.route('/bar/123', endpoint='123')1729+ def foo_bar_foo():1730+ return flask.request.endpoint1731+1732+ @bp.route('/bar/foo')1733+ def bar_foo():1734+ return flask.request.endpoint1735+1736+ app = flask.Flask(__name__)1737+ app.register_blueprint(bp, url_prefix='/py')1738+1739+ @app.route('/')1740+ def index():1741+ return flask.request.endpoint1742+1743+ 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')1749+1750+1751 class SendfileTestCase(unittest.TestCase):1752 1753 def test_send_file_regular(self):1754 app = flask.Flask(__name__)507 unmodified lines