Skip to content

Instantly share code, notes, and snippets.

@ratchit
Created October 23, 2012 22:23
Show Gist options
  • Save ratchit/3942066 to your computer and use it in GitHub Desktop.
Save ratchit/3942066 to your computer and use it in GitHub Desktop.
Flask MethodView decorators for individual request methods
from flask.views import MethodView
class MethodView(MethodView):
_decorators = {}
def dispatch_request(self, *args, **kwargs):
"""Derived MethodView dispatch to allow for decorators to be
applied to specific individual request methods - in addition
to the standard decorator assignment.
Example decorator use:
decorators = [user_required] # applies to all methods
_decorators = {
'post': [admin_required, format_results]
}
"""
view = super(MethodView, self).dispatch_request
decorators = self._decorators.get(request.method.lower())
if decorators:
for decorator in decorators:
view = decorator(view)
return view(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment