Skip to content
Myr

Add syntatic sugar for route registration

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

This takes a popular API whereby instead of passing the HTTP method as an argument to route it is instead used as the method name i.e. @app.route("/", methods=["POST"]) is now writeable as, @app.post("/") This is simply syntatic sugar, it doesn't do anything else, but makes it slightly easier for users. I've included all the methods that are relevant and aren't auto generated i.e. not connect, head, options, and trace.

authored by pgjones · committed by David Lord · parent 82d69cd · 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

3 files+61 added−0 removed

modified:src/flask/scaffold.py+41 added
144 unmodified lines
145146 def _is_setup_finished(self):147 raise NotImplementedError148149 def _method_route(self, method, rule, options):150 if "methods" in options:151 raise TypeError("Use the 'route' decorator to use the 'methods' argument.")152153 return self.route(rule, methods=[method], **options)154155 def get(self, rule, **options):156 """Shortcut for :meth:`route` with ``methods=["GET"]``.157158 .. versionadded:: 2.0159 """160 return self._method_route("GET", rule, options)161162 def post(self, rule, **options):163 """Shortcut for :meth:`route` with ``methods=["POST"]``.164165 .. versionadded:: 2.0166 """167 return self._method_route("POST", rule, options)168169 def put(self, rule, **options):170 """Shortcut for :meth:`route` with ``methods=["PUT"]``.171172 .. versionadded:: 2.0173 """174 return self._method_route("PUT", rule, options)175176 def delete(self, rule, **options):177 """Shortcut for :meth:`route` with ``methods=["DELETE"]``.178179 .. versionadded:: 2.0180 """181 return self._method_route("DELETE", rule, options)182183 def patch(self, rule, **options):184 """Shortcut for :meth:`route` with ``methods=["PATCH"]``.185186 .. versionadded:: 2.0187 """188 return self._method_route("PATCH", rule, options)189190 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 lines
modified:tests/test_basic.py+17 added
46 unmodified lines
47 rv = client.open("/", method="OPTIONS")48 assert sorted(rv.allow) == ["GET", "HEAD", "OPTIONS", "POST", "PUT"]495051@pytest.mark.parametrize("method", ["get", "post", "put", "delete", "patch"])52def test_method_route(app, client, method):53 method_route = getattr(app, method)54 client_method = getattr(client, method)5556 @method_route("/")57 def hello():58 return "Hello"5960 assert client_method("/").data == b"Hello"616263def test_method_route_no_methods(app):64 with pytest.raises(TypeError):65 app.get("/", methods=["GET", "POST"])666768def test_provide_automatic_options_attr():69 app = flask.Flask(__name__)7071 def index():
1958 unmodified lines
modified:CHANGES.rst+3 added
62 unmodified lines
63- 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`707172Version 1.1.273-------------
1011 unmodified lines