use mro to collect methods
Before adoption: this commit came in with the history the project adopted, so no landing records it.
ignore methods attr unless explicitly set add changelog
authored by David Lord · committed by David Lord · parent 0d9d3d8 · Browse files · 2 files·+24 added−22 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+24 added−22 removed
- other:+24 added−22 removed
Provenance
- Commit
648344d- Tree
441901d- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
0d9d3d8
2 files changed+24 added−22 removed
modified:flask/views.py+22 added-22 removed
100 unmodified lines101 view.methods = cls.methods102 return view103 104 105-def get_methods(cls):106- return getattr(cls, 'methods', []) or []107-108-105 class MethodViewType(type):106+ """Metaclass for :class:`MethodView` that determines what methods the view107+ defines.108+ """109 111- def __new__(cls, name, bases, d):112- 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: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:114+ methods = set()115+116+ for key in http_method_funcs:117+ if hasattr(cls, key):118 methods.add(key.upper())118- # If we have no method at all in there we don't want to119- # add a method list. (This is for instance the case for120- # the base class or another subclass of a base method view121- # 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:123- rv.methods = sorted(methods)124- return rv125+ cls.methods = methods126 127 128 class MethodView(with_metaclass(MethodViewType, View)):128- """Like a regular class-based view but that dispatches requests to129- particular methods. For instance if you implement a method called130- :meth:`get` it means it will respond to ``'GET'`` requests and131- the :meth:`dispatch_request` implementation will automatically132- forward your request to that. Also :attr:`options` is set for you133- 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):136-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: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