Skip to content
Myr

Add the ability to combine MethodViews

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

authored by Antoine Catton · committed by Antoine Catton · parent 67e3919 · Browse files · 2 files+47 added−1 removed
This commit

State

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

Diff stats

2 files+47 added−1 removed

  • tests:+42 added−0 removed
  • other:+5 added−1 removed

2 files+47 added−1 removed

modified:flask/views.py+5 added-1 removed
100 unmodified lines
101 view.methods = cls.methods102 return view103104105def get_methods(cls):106 return getattr(cls, 'methods', []) or []107108109class MethodViewType(type):110111 def __new__(cls, name, bases, d):112 rv = type.__new__(cls, name, bases, d)113 if 'methods' not in d:110 methods = set(rv.methods or [])114 methods = set(m for b in bases for m in get_methods(b))115 for key in d:116 if key in http_method_funcs:117 methods.add(key.upper())118 # If we have no method at all in there we don't want to
35 unmodified lines
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']