Java Jester

Custom Python Decorator

Decorators allow developers to use aspect oriented approach. Here is an example of a custom python/django decorator that checks if the user is a super user and redirects it to login page if not

def super_user(func):
    def call(request, *args, **kwds):
        user = request.user
        if user.is_authenticated() and user.is_superuser:
            return func(request, *args, **kwds)
        else:
            return utils.redirect(settings.LOGIN_URL)
    return call
Here is an example usage:
@super_user
def my_view(request):
    pass

 

Comments (0)


recommended books
Jester Cap Logo by Vonn reyjexter.com | Valid XHTML & CSS | 2008 | Design & Devt. by: vonnhugo

powered by:

django framework python google app engine