Skip to content
Myr

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

3 files+66 added−18 removed

modified:flask/views.py+22 added-18 removed
101 unmodified lines
102 return view103104105class MethodViewType(type):106 """Metaclass for :class:`MethodView` that determines what methods the view107 defines.108 """109107 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)112113 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()115116 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).119120 # 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 = methods126127128class 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. ::132133 class CounterAPI(MethodView):132134 def get(self):135 return session.get('counter', 0)136137 def post(self):
1 unmodified line
139 return 'OK'140141 app.add_url_rule('/counter', view_func=CounterAPI.as_view('counter'))142 """143144 def dispatch_request(self, *args, **kwargs):145 meth = getattr(self, request.method.lower(), None)146147 # 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)151152 assert meth is not None, 'Unimplemented method %r' % request.method153 return meth(*args, **kwargs)
modified:tests/test_views.py+42 added
158 unmodified lines
159 app.add_url_rule('/', view_func=Index.as_view('index'))160161 # But these tests should still pass. We just log a warning.162 common_test(app)163164def test_multiple_inheritance():165 app = flask.Flask(__name__)166167 class GetView(flask.views.MethodView):168 def get(self):169 return 'GET'170171 class DeleteView(flask.views.MethodView):172 def delete(self):173 return 'DELETE'174175 class GetDeleteView(GetView, DeleteView):176 pass177178 app.add_url_rule('/', view_func=GetDeleteView.as_view('index'))179180 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']184185def test_remove_method_from_parent():186 app = flask.Flask(__name__)187188 class GetView(flask.views.MethodView):189 def get(self):190 return 'GET'191192 class OtherView(flask.views.MethodView):193 def post(self):194 return 'POST'195196 class View(GetView, OtherView):197 methods = ['GET']198199 app.add_url_rule('/', view_func=View.as_view('index'))200201 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 lines
23 (`#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`_)2829.. _#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/22233334Version 0.12.1
610 unmodified lines