Skip to content
Myr

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

2 files+24 added−22 removed

modified:flask/views.py+22 added-22 removed
100 unmodified lines
101 view.methods = cls.methods102 return view103104105def get_methods(cls):106 return getattr(cls, 'methods', []) or []107108105class MethodViewType(type):106 """Metaclass for :class:`MethodView` that determines what methods the view107 defines.108 """109111 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)112113 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()115116 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).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:123 rv.methods = sorted(methods)124 return rv125 cls.methods = methods126127128class 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. ::132133 class CounterAPI(MethodView):136134 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: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