Add the ability to combine MethodViews
This commit is contained in:
parent
67e391921c
commit
516ce59f95
2 changed files with 47 additions and 1 deletions
|
|
@ -102,12 +102,16 @@ class View(object):
|
||||||
return view
|
return view
|
||||||
|
|
||||||
|
|
||||||
|
def get_methods(cls):
|
||||||
|
return getattr(cls, 'methods', []) or []
|
||||||
|
|
||||||
|
|
||||||
class MethodViewType(type):
|
class MethodViewType(type):
|
||||||
|
|
||||||
def __new__(cls, name, bases, d):
|
def __new__(cls, name, bases, d):
|
||||||
rv = type.__new__(cls, name, bases, d)
|
rv = type.__new__(cls, name, bases, d)
|
||||||
if 'methods' not in d:
|
if 'methods' not in d:
|
||||||
methods = set(rv.methods or [])
|
methods = set(m for b in bases for m in get_methods(b))
|
||||||
for key in d:
|
for key in d:
|
||||||
if key in http_method_funcs:
|
if key in http_method_funcs:
|
||||||
methods.add(key.upper())
|
methods.add(key.upper())
|
||||||
|
|
|
||||||
|
|
@ -160,3 +160,45 @@ def test_endpoint_override():
|
||||||
|
|
||||||
# But these tests should still pass. We just log a warning.
|
# But these tests should still pass. We just log a warning.
|
||||||
common_test(app)
|
common_test(app)
|
||||||
|
|
||||||
|
def test_multiple_inheritance():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
|
class GetView(flask.views.MethodView):
|
||||||
|
def get(self):
|
||||||
|
return 'GET'
|
||||||
|
|
||||||
|
class DeleteView(flask.views.MethodView):
|
||||||
|
def delete(self):
|
||||||
|
return 'DELETE'
|
||||||
|
|
||||||
|
class GetDeleteView(GetView, DeleteView):
|
||||||
|
pass
|
||||||
|
|
||||||
|
app.add_url_rule('/', view_func=GetDeleteView.as_view('index'))
|
||||||
|
|
||||||
|
c = app.test_client()
|
||||||
|
assert c.get('/').data == b'GET'
|
||||||
|
assert c.delete('/').data == b'DELETE'
|
||||||
|
assert sorted(GetDeleteView.methods) == ['DELETE', 'GET']
|
||||||
|
|
||||||
|
def test_remove_method_from_parent():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
|
class GetView(flask.views.MethodView):
|
||||||
|
def get(self):
|
||||||
|
return 'GET'
|
||||||
|
|
||||||
|
class OtherView(flask.views.MethodView):
|
||||||
|
def post(self):
|
||||||
|
return 'POST'
|
||||||
|
|
||||||
|
class View(GetView, OtherView):
|
||||||
|
methods = ['GET']
|
||||||
|
|
||||||
|
app.add_url_rule('/', view_func=View.as_view('index'))
|
||||||
|
|
||||||
|
c = app.test_client()
|
||||||
|
assert c.get('/').data == b'GET'
|
||||||
|
assert c.post('/').status_code == 405
|
||||||
|
assert sorted(View.methods) == ['GET']
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue