@@ -102,38 +102,35 @@ def view(*args, **kwargs):
102
102
return view
103
103
104
104
105
- def get_methods (cls ):
106
- return getattr (cls , 'methods' , []) or []
107
-
108
-
109
105
class MethodViewType (type ):
106
+ """Metaclass for :class:`MethodView` that determines what methods the view
107
+ defines.
108
+ """
109
+
110
+ def __init__ (cls , name , bases , d ):
111
+ super (MethodViewType , cls ).__init__ (name , bases , d )
110
112
111
- def __new__ (cls , name , bases , d ):
112
- rv = type .__new__ (cls , name , bases , d )
113
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 ):
117
118
methods .add (key .upper ())
118
- # If we have no method at all in there we don't want to
119
- # add a method list. (This is for instance the case for
120
- # the base class or another subclass of a base method view
121
- # that does not introduce new methods).
119
+
120
+ # If we have no method at all in there we don't want to add a
121
+ # method list. This is for instance the case for the base class
122
+ # or another subclass of a base method view that does not introduce
123
+ # new methods.
122
124
if methods :
123
- rv .methods = sorted (methods )
124
- return rv
125
+ cls .methods = methods
125
126
126
127
127
128
class MethodView (with_metaclass (MethodViewType , View )):
128
- """Like a regular class-based view but that dispatches requests to
129
- particular methods. For instance if you implement a method called
130
- :meth:`get` it means it will respond to ``'GET'`` requests and
131
- the :meth:`dispatch_request` implementation will automatically
132
- forward your request to that. Also :attr:`options` is set for you
133
- automatically::
129
+ """A class-based view that dispatches request methods to the corresponding
130
+ class methods. For example, if you implement a ``get`` method, it will be
131
+ used to handle ``GET`` requests. ::
134
132
135
133
class CounterAPI(MethodView):
136
-
137
134
def get(self):
138
135
return session.get('counter', 0)
139
136
@@ -143,11 +140,14 @@ def post(self):
143
140
144
141
app.add_url_rule('/counter', view_func=CounterAPI.as_view('counter'))
145
142
"""
143
+
146
144
def dispatch_request (self , * args , ** kwargs ):
147
145
meth = getattr (self , request .method .lower (), None )
146
+
148
147
# If the request method is HEAD and we don't have a handler for it
149
148
# retry with GET.
150
149
if meth is None and request .method == 'HEAD' :
151
150
meth = getattr (self , 'get' , None )
151
+
152
152
assert meth is not None , 'Unimplemented method %r' % request .method
153
153
return meth (* args , ** kwargs )
0 commit comments