Merge pull request #3907 from pgjones/sugar
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add syntatic sugar for route registration
authored by David Lord · committed by GitHub · parents 82d69cd, 705e526 · Browse files · 3 files·+61 added−0 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+61 added−0 removed
- tests:+17 added−0 removed
- other:+44 added−0 removed
Provenance
- Commit
5fea7ca- Tree
c8a8111- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
82d69cd- Parent 2
705e526
3 files changed+61 added−0 removed
modified:src/flask/scaffold.py+41 added
144 unmodified lines145 146 def _is_setup_finished(self):147 raise NotImplementedError148 149+ def _method_route(self, method, rule, options):150+ if "methods" in options:151+ raise TypeError("Use the 'route' decorator to use the 'methods' argument.")152+153+ return self.route(rule, methods=[method], **options)154+155+ def get(self, rule, **options):156+ """Shortcut for :meth:`route` with ``methods=["GET"]``.157+158+ .. versionadded:: 2.0159+ """160+ return self._method_route("GET", rule, options)161+162+ def post(self, rule, **options):163+ """Shortcut for :meth:`route` with ``methods=["POST"]``.164+165+ .. versionadded:: 2.0166+ """167+ return self._method_route("POST", rule, options)168+169+ def put(self, rule, **options):170+ """Shortcut for :meth:`route` with ``methods=["PUT"]``.171+172+ .. versionadded:: 2.0173+ """174+ return self._method_route("PUT", rule, options)175+176+ def delete(self, rule, **options):177+ """Shortcut for :meth:`route` with ``methods=["DELETE"]``.178+179+ .. versionadded:: 2.0180+ """181+ return self._method_route("DELETE", rule, options)182+183+ def patch(self, rule, **options):184+ """Shortcut for :meth:`route` with ``methods=["PATCH"]``.185+186+ .. versionadded:: 2.0187+ """188+ return self._method_route("PATCH", rule, options)189+190 def route(self, rule, **options):191 """A decorator that is used to register a view function for a192 given URL rule. This does the same thing as :meth:`add_url_rule`193 but is intended for decorator usage::258 unmodified linesmodified:tests/test_basic.py+17 added
46 unmodified lines47 rv = client.open("/", method="OPTIONS")48 assert sorted(rv.allow) == ["GET", "HEAD", "OPTIONS", "POST", "PUT"]49 50 51+@pytest.mark.parametrize("method", ["get", "post", "put", "delete", "patch"])52+def test_method_route(app, client, method):53+ method_route = getattr(app, method)54+ client_method = getattr(client, method)55+56+ @method_route("/")57+ def hello():58+ return "Hello"59+60+ assert client_method("/").data == b"Hello"61+62+63+def test_method_route_no_methods(app):64+ with pytest.raises(TypeError):65+ app.get("/", methods=["GET", "POST"])66+67+68 def test_provide_automatic_options_attr():69 app = flask.Flask(__name__)70 71 def index():1958 unmodified linesmodified:CHANGES.rst+3 added
62 unmodified lines63 - The request context does route matching before opening the session.64 This could allow a session interface to change behavior based on65 ``request.endpoint``. :issue:`3776`66 - Use Jinja's implementation of the ``|tojson`` filter. :issue:`3881`67+- Add route decorators for common HTTP methods. For example,68+ ``@app.post("/login")`` is a shortcut for69+ ``@app.route("/login", methods=["POST"])``. :pr:`3907`70 71 72 Version 1.1.273 -------------1011 unmodified lines