Merge pull request #2252 from davidism/method-view-inheritance
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Continue #1936: Add the ability to combine MethodViews
authored by David Lord · committed by GitHub · parents 9569e8c, 648344d · Browse files · 3 files·+66 added−18 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+66 added−18 removed
- tests:+42 added−0 removed
- other:+24 added−18 removed
Provenance
- Commit
9b892e2- Tree
441901d- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
9569e8c- Parent 2
648344d
3 files changed+66 added−18 removed
modified:flask/views.py+22 added-18 removed
101 unmodified lines102 return view103 104 105 class MethodViewType(type):106+ """Metaclass for :class:`MethodView` that determines what methods the view107+ defines.108+ """109 107- def __new__(cls, name, bases, d):108- rv = type.__new__(cls, name, bases, d)110+ def __init__(cls, name, bases, d):111+ super(MethodViewType, cls).__init__(name, bases, d)112+113 if 'methods' not in d:110- methods = set(rv.methods or [])111- for key in d:112- if key in http_method_funcs:114+ methods = set()115+116+ for key in http_method_funcs:117+ if hasattr(cls, key):118 methods.add(key.upper())114- # If we have no method at all in there we don't want to115- # add a method list. (This is for instance the case for116- # the base class or another subclass of a base method view117- # that does not introduce new methods).119+120+ # If we have no method at all in there we don't want to add a121+ # method list. This is for instance the case for the base class122+ # or another subclass of a base method view that does not introduce123+ # new methods.124 if methods:119- rv.methods = sorted(methods)120- return rv125+ cls.methods = methods126 127 128 class MethodView(with_metaclass(MethodViewType, View)):124- """Like a regular class-based view but that dispatches requests to125- particular methods. For instance if you implement a method called126- :meth:`get` it means it will respond to ``'GET'`` requests and127- the :meth:`dispatch_request` implementation will automatically128- forward your request to that. Also :attr:`options` is set for you129- automatically::129+ """A class-based view that dispatches request methods to the corresponding130+ class methods. For example, if you implement a ``get`` method, it will be131+ used to handle ``GET`` requests. ::132 133 class CounterAPI(MethodView):132-134 def get(self):135 return session.get('counter', 0)136 137 def post(self):1 unmodified line139 return 'OK'140 141 app.add_url_rule('/counter', view_func=CounterAPI.as_view('counter'))142 """143+144 def dispatch_request(self, *args, **kwargs):145 meth = getattr(self, request.method.lower(), None)146+147 # If the request method is HEAD and we don't have a handler for it148 # retry with GET.149 if meth is None and request.method == 'HEAD':150 meth = getattr(self, 'get', None)151+152 assert meth is not None, 'Unimplemented method %r' % request.method153 return meth(*args, **kwargs)modified:tests/test_views.py+42 added
158 unmodified lines159 app.add_url_rule('/', view_func=Index.as_view('index'))160 161 # But these tests should still pass. We just log a warning.162 common_test(app)163+164+def test_multiple_inheritance():165+ app = flask.Flask(__name__)166+167+ class GetView(flask.views.MethodView):168+ def get(self):169+ return 'GET'170+171+ class DeleteView(flask.views.MethodView):172+ def delete(self):173+ return 'DELETE'174+175+ class GetDeleteView(GetView, DeleteView):176+ pass177+178+ app.add_url_rule('/', view_func=GetDeleteView.as_view('index'))179+180+ c = app.test_client()181+ assert c.get('/').data == b'GET'182+ assert c.delete('/').data == b'DELETE'183+ assert sorted(GetDeleteView.methods) == ['DELETE', 'GET']184+185+def test_remove_method_from_parent():186+ app = flask.Flask(__name__)187+188+ class GetView(flask.views.MethodView):189+ def get(self):190+ return 'GET'191+192+ class OtherView(flask.views.MethodView):193+ def post(self):194+ return 'POST'195+196+ class View(GetView, OtherView):197+ methods = ['GET']198+199+ app.add_url_rule('/', view_func=View.as_view('index'))200+201+ c = app.test_client()202+ assert c.get('/').data == b'GET'203+ assert c.post('/').status_code == 405204+ assert sorted(View.methods) == ['GET']modified:CHANGES+2 added
22 unmodified lines23 (`#2017`_)24 - Add support for ``provide_automatic_options`` in ``add_url_rule`` to disable25 adding OPTIONS method when the ``view_func`` argument is not a class.26 (`#1489`_).27+- ``MethodView`` can inherit method handlers from base classes. (`#1936`_)28 29 .. _#1489: https://github.com/pallets/flask/pull/148930+.. _#1936: https://github.com/pallets/flask/pull/193631 .. _#2017: https://github.com/pallets/flask/pull/201732 .. _#2223: https://github.com/pallets/flask/pull/222333 34 Version 0.12.1610 unmodified lines