31
votes

How do I implement the @user_passes_test(lambda u: u.is_superuser) decorator for class based views? I have used this before for function based views, and I have a workaround but it feels unnaturally.

Shouldn't this be covered by the dispatch method?

4
See below, but also note that u.is_superuser is a boolean field on the User model, NOT a method. So u.is_superuser() is incorrect - should be just u.is_superuser. - shacker

4 Answers

40
votes

You use @method_decorator on the dispatch method of the class:

from django.views.generic import View
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import user_passes_test

class MyView(View):
    @method_decorator(user_passes_test(lambda u: u.is_superuser))
    def dispatch(self, *args, **kwargs):
        return super(MyView, self).dispatch(*args, **kwargs)
25
votes

Building on @Chris Pratt's answer, you'll probably want to do this in multiple view classes so it makes sense to turn it into a mixin.

class SuperuserRequiredMixin(object):
    @method_decorator(user_passes_test(lambda u: u.is_superuser))
    def dispatch(self, *args, **kwargs):
        return super(SuperuserRequiredMixin, self).dispatch(*args, **kwargs)

Usage

class MyView(SuperuserRequiredMixin, View):
    def get(self, request):
        ...

To prevent unexpected MRO bugs, ensure the mixin is the first inherited class.

You can implement a LoginRequiredMixin, or any other common tests you use in your app, in the same way.

Edit: Django 1.9 adds AccessMixin, LoginRequiredMixin, PermissionRequiredMixin, and UserPassesTestMixin

1
votes

I have used the @view_decorator from this snippet: http://djangosnippets.org/snippets/2505/ to wrap my normal function decorators.

1
votes

You should have a look to django-braces and its UserPassesTestMixin.