Skip to content
Myr

Added flask.views.View.decorators to automatically decorate class based views.

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

authored by Armin Ronacher · committed by Armin Ronacher · parent 8340d3c · Browse files · 3 files+58 added−0 removed
This commit

State

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

Diff stats

3 files+58 added−0 removed

  • tests:+21 added−0 removed
  • other:+37 added−0 removed

3 files+58 added−0 removed

modified:flask/views.py+35 added
28 unmodified lines
29 def dispatch_request(self, name):30 return 'Hello %s!' % name3132 app.add_url_rule('/hello/<name>', view_func=MyView.as_view('myview'))3334 When you want to decorate a pluggable view you will have to either do that35 when the view function is created (by wrapping the return value of36 :meth:`as_view`) or you can use the :attr:`decorators` attribute::3738 class SecretView(View):39 methods = ['GET']40 decorators = [superuser_required]4142 def dispatch_request(self):43 ...4445 The decorators stored in the decorators list are applied one after another46 when the view function is created. Note that you can *not* use the class47 based decorators since those would decorate the view class and not the48 generated view function!49 """5051 #: A for which methods this pluggable view can handle.52 methods = None5354 #: The canonical way to decorate class based views is to decorate the55 #: return value of as_view(). However since this moves parts of the56 #: logic from the class declaration to the place where it's hooked57 #: into the routing system.58 #:59 #: You can place one or more decorators in this list and whenever the60 #: view function is created the result is automatically decorated.61 #:62 #: .. versionadded:: 0.863 decorators = []6465 def dispatch_request(self):66 """Subclasses have to override this method to implement the67 actual view functionc ode. This method is called with all68 the arguments from the URL rule.
12 unmodified lines
81 """82 def view(*args, **kwargs):83 self = view.view_class(*class_args, **class_kwargs)84 return self.dispatch_request(*args, **kwargs)8586 if cls.decorators:87 view.__name__ = name88 view.__module__ = cls.__module__89 for decorator in cls.decorators:90 view = decorator(view)9192 # we attach the view class to the view function for two reasons:93 # first of all it allows us to easily figure out what class based94 # view this thing came from, secondly it's also used for instanciating95 # the view class so you can actually replace it with something else
52 unmodified lines
modified:tests/flask_tests.py+21 added
2256 unmodified lines
22572258 meths = parse_set_header(c.open('/', method='OPTIONS').headers['Allow'])2259 self.assertEqual(sorted(meths), ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST'])22602261 def test_view_decorators(self):2262 app = flask.Flask(__name__)22632264 def add_x_parachute(f):2265 def new_function(*args, **kwargs):2266 resp = flask.make_response(f(*args, **kwargs))2267 resp.headers['X-Parachute'] = 'awesome'2268 return resp2269 return new_function22702271 class Index(flask.views.View):2272 decorators = [add_x_parachute]2273 def dispatch_request(self):2274 return 'Awesome'22752276 app.add_url_rule('/', view_func=Index.as_view('index'))2277 c = app.test_client()2278 rv = c.get('/')2279 self.assertEqual(rv.headers['X-Parachute'], 'awesome')2280 self.assertEqual(rv.data, 'Awesome')228122822283class DeprecationsTestCase(FlaskTestCase):22842285 def test_init_jinja_globals(self):2286 class MyFlask(flask.Flask):
43 unmodified lines
modified:CHANGES+2 added
36 unmodified lines
37 easily modify sessions from the test environment.38- Refactored test client internally. The ``APPLICATION_ROOT`` configuration39 variable as well as ``SERVER_NAME`` are now properly used by the test client40 as defaults.41- Added :attr:`flask.views.View.decorators` to support simpler decorating of42 pluggable (class based) views.4344Version 0.7.345-------------46
238 unmodified lines