Skip to content
Myr

Merge pull request #307 from s0undt3ch/master

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

route decorators custom enpoints

authored by Ron DuPlain · committed by Ron DuPlain · parents e942a09, a9e09ec · Browse files · 4 files+121 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

4 files+121 added−2 removed

  • other:+121 added−2 removed

4 files+121 added−2 removed

modified:flask/testsuite/basic.py+26 added
893 unmodified lines
894 with app.test_client() as c:895 rv = c.post('/foo', data={}, follow_redirects=True)896 self.assert_equal(rv.data, 'success')897898 def test_route_decorator_custom_endpoint(self):899 app = flask.Flask(__name__)900 app.debug = True901902 @app.route('/foo/')903 def foo():904 return flask.request.endpoint905906 @app.route('/bar/', endpoint='bar')907 def for_bar():908 return flask.request.endpoint909910 @app.route('/bar/123', endpoint='123')911 def for_bar_foo():912 return flask.request.endpoint913914 with app.test_request_context():915 assert flask.url_for('foo') == '/foo/'916 assert flask.url_for('bar') == '/bar/'917 assert flask.url_for('123') == '/bar/123'918919 c = app.test_client()920 self.assertEqual(c.get('/foo/').data, 'foo')921 self.assertEqual(c.get('/bar/').data, 'bar')922 self.assertEqual(c.get('/bar/123').data, '123')923924925class ContextTestCase(FlaskTestCase):926927 def test_context_binding(self):928 app = flask.Flask(__name__)
123 unmodified lines
modified:flask/testsuite/blueprints.py+86 added
413 unmodified lines
414 c = app.test_client()415 self.assert_equal(c.get('/').data, '1')416 self.assert_equal(c.get('/page/2').data, '2')417418 def test_route_decorator_custom_endpoint(self):419420 bp = flask.Blueprint('bp', __name__)421422 @bp.route('/foo')423 def foo():424 return flask.request.endpoint425426 @bp.route('/bar', endpoint='bar')427 def foo_bar():428 return flask.request.endpoint429430 @bp.route('/bar/123', endpoint='123')431 def foo_bar_foo():432 return flask.request.endpoint433434 @bp.route('/bar/foo')435 def bar_foo():436 return flask.request.endpoint437438 app = flask.Flask(__name__)439 app.register_blueprint(bp, url_prefix='/py')440441 @app.route('/')442 def index():443 return flask.request.endpoint444445 c = app.test_client()446 self.assertEqual(c.get('/').data, 'index')447 self.assertEqual(c.get('/py/foo').data, 'bp.foo')448 self.assertEqual(c.get('/py/bar').data, 'bp.bar')449 self.assertEqual(c.get('/py/bar/123').data, 'bp.123')450 self.assertEqual(c.get('/py/bar/foo').data, 'bp.bar_foo')451452 def test_route_decorator_custom_endpoint_with_dots(self):453 bp = flask.Blueprint('bp', __name__)454455 @bp.route('/foo')456 def foo():457 return flask.request.endpoint458459 try:460 @bp.route('/bar', endpoint='bar.bar')461 def foo_bar():462 return flask.request.endpoint463 except AssertionError:464 pass465 else:466 raise AssertionError('expected AssertionError not raised')467468 try:469 @bp.route('/bar/123', endpoint='bar.123')470 def foo_bar_foo():471 return flask.request.endpoint472 except AssertionError:473 pass474 else:475 raise AssertionError('expected AssertionError not raised')476477 def foo_foo_foo():478 pass479480 self.assertRaises(481 AssertionError,482 lambda: bp.add_url_rule(483 '/bar/123', endpoint='bar.123', view_func=foo_foo_foo484 )485 )486487 self.assertRaises(488 AssertionError,489 bp.route('/bar/123', endpoint='bar.123'),490 lambda: None491 )492493 app = flask.Flask(__name__)494 app.register_blueprint(bp, url_prefix='/py')495496 c = app.test_client()497 self.assertEqual(c.get('/py/foo').data, 'bp.foo')498 # The rule's din't actually made it through499 rv = c.get('/py/bar')500 assert rv.status_code == 404501 rv = c.get('/py/bar/123')502 assert rv.status_code == 404503504505def suite():506 suite = unittest.TestSuite()507 suite.addTest(unittest.makeSuite(BlueprintTestCase))508 suite.addTest(unittest.makeSuite(ModuleTestCase))
1 unmodified line
modified:flask/app.py+5 added-1 removed
943 unmodified lines
944 :param subdomain: specifies the rule for the subdomain in case945 subdomain matching is in use.946 :param strict_slashes: can be used to disable the strict slashes947 setting for this rule. See above.948 :param endpoint: Since version 0.8 you can also pass the enpoint,949 it will be used instead of generating the endpoint950 from the function name.951 :param options: other options to be forwarded to the underlying952 :class:`~werkzeug.routing.Rule` object.953 """954 def decorator(f):955 endpoint = options.pop("endpoint", None)952 self.add_url_rule(rule, None, f, **options)956 self.add_url_rule(rule, endpoint, f, **options)957 return f958 return decorator959960 @setupmethod
591 unmodified lines
modified:flask/blueprints.py+4 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):166 """Like :meth:`Flask.add_url_rule` but for a blueprint. The endpoint for167 the :func:`url_for` function is prefixed with the name of the blueprint.168 """169 if endpoint:170 assert '.' not in endpoint, "Blueprint endpoint's should not contain dot's"171 self.record(lambda s:172 s.add_url_rule(rule, endpoint, view_func, **options))173174 def endpoint(self, endpoint):
147 unmodified lines