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
Provenance
- Commit
ef0f626- Tree
cf0a600- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
8340d3c
3 files changed+58 added−0 removed
modified:flask/views.py+35 added
28 unmodified lines29 def dispatch_request(self, name):30 return 'Hello %s!' % name31 32 app.add_url_rule('/hello/<name>', view_func=MyView.as_view('myview'))33+34+ 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::37+38+ class SecretView(View):39+ methods = ['GET']40+ decorators = [superuser_required]41+42+ def dispatch_request(self):43+ ...44+45+ 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 """50 51+ #: A for which methods this pluggable view can handle.52 methods = None53 54+ #: 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 = []64+65 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 lines81 """82 def view(*args, **kwargs):83 self = view.view_class(*class_args, **class_kwargs)84 return self.dispatch_request(*args, **kwargs)85+86+ if cls.decorators:87+ view.__name__ = name88+ view.__module__ = cls.__module__89+ for decorator in cls.decorators:90+ view = decorator(view)91+92 # 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 else52 unmodified linesmodified:tests/flask_tests.py+21 added
2256 unmodified lines2257 2258 meths = parse_set_header(c.open('/', method='OPTIONS').headers['Allow'])2259 self.assertEqual(sorted(meths), ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST'])2260 2261+ def test_view_decorators(self):2262+ app = flask.Flask(__name__)2263 2264+ 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_function2270+2271+ class Index(flask.views.View):2272+ decorators = [add_x_parachute]2273+ def dispatch_request(self):2274+ return 'Awesome'2275+2276+ 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')2281+2282+2283 class DeprecationsTestCase(FlaskTestCase):2284 2285 def test_init_jinja_globals(self):2286 class MyFlask(flask.Flask):43 unmodified linesmodified:CHANGES+2 added
36 unmodified lines37 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.43 44 Version 0.7.345 -------------46 238 unmodified lines