Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,20 @@ method and `Flask-Classy` will take care of the rest::
def so_is_this(self):
return "Looking at me? I guess you're logged in."

You can also override the classmethod `get_decorators`::

class WhataGreatView(FlaskView):

@classmethod
def get_decorators(cls):
return [login_required]

def this_is_secret(self):
return "If you see this, you're logged in."

def so_is_this(self):
return "Looking at me? I guess you're logged in."


Before and After
----------------
Expand Down
9 changes: 6 additions & 3 deletions flask_classy.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class FlaskView(_FlaskViewBase):
route_prefix = None
trailing_slash = True

@classmethod
def get_decorators(cls):
return cls.decorators

@classmethod
def register(cls, app, route_base=None, subdomain=None, route_prefix=None,
trailing_slash=None):
Expand Down Expand Up @@ -190,9 +194,8 @@ def make_proxy_method(cls, name):
i = cls()
view = getattr(i, name)

if cls.decorators:
for decorator in cls.decorators:
view = decorator(view)
for decorator in cls.get_decorators():
view = decorator(view)

@functools.wraps(view)
def proxy(**forgettable_view_args):
Expand Down
13 changes: 10 additions & 3 deletions test_classy/test_decorators.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from flask import Flask, url_for
from .view_classes import DecoratedView
from .view_classes import (DecoratedView, AutoDecoratedView,
OverridenAutoDecoratedView)
from nose.tools import *

app = Flask("decorated")
DecoratedView.register(app)
AutoDecoratedView.register(app)
OverridenAutoDecoratedView.register(app)
client = app.test_client()


Expand Down Expand Up @@ -47,6 +50,10 @@ def test_recursive_with_route_with_parameter():
eq_(b"Anotherval 1234", resp.data)


def test_autodecorated():
resp = client.get('/autodecorated/')
eq_(b"foobar", resp.data)



def test_overriden_autodecorated():
resp = client.get('/overridenautodecorated/')
eq_(b"foobar", resp.data)
23 changes: 23 additions & 0 deletions test_classy/view_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,29 @@ def anotherval(self, val):
return "Anotherval " + val


def foo_decorator(func):
@wraps(func)
def wrapped(*args, **kwargs):
return 'foo' + func(*args, **kwargs)
return wrapped


class AutoDecoratedView(FlaskView):
decorators = [foo_decorator]

def index(self):
return 'bar'


class OverridenAutoDecoratedView(FlaskView):

@classmethod
def get_decorators(cls):
return [foo_decorator]

def index(self):
return 'bar'


class InheritanceView(BasicView):

Expand Down