Django Custom Decorator for user group check I wrote a custom decorator to check whether user is in particular group of not. Please provide the feedback on optimizing code. I currently moved from Java to Django-Python so I wasn't able to optimize the logic.

Check whether student is in labsheet_collector, assignment_collector or fund_collector.

def labsheet_collector(view_func):
    def _wrapped_view_func(request, *args, **kwargs):
        print request.user
        if request.user.is_anonymous:
            return redirect('/admin/') #?next=%s' % request.path)
        if not (request.user.groups.filter(name='labsheet_collector').exists()) or request.user.is_superuser:
            raise Http404
        return view_func(request, *args, **kwargs)

    return _wrapped_view_func


def assignment_collector(view_func):
    def _wrapped_view_func(request, *args, **kwargs):
        if request.user.is_anonymous:
            return redirect('/admin/')
        if not (request.user.groups.filter(name='assignment_collector').exists()) \
                or request.user.is_superuser:
            raise Http404
        return view_func(request, *args, **kwargs)

    return _wrapped_view_func


def fund_collector(view_func):
    def _wrapped_view_func(request, *args, **kwargs):
        if request.user.is_anonymous:
            return redirect('/admin/')
        if not (request.user.groups.filter(name='fund_collector').exists()) \
                or request.user.is_superuser:
            raise Http404
        return view_func(request, *args, **kwargs)

    return _wrapped_view_func
As an alternative to the answer given by jcollado, Django has some built in decorators to help.

From the docs:

from django.contrib.auth.decorators import user_passes_test

def email_check(user):
    return '@example.com' in user.email

@user_passes_test(email_check)
def my_view(request):


https://docs.djangoproject.com/en/1.6/topics/auth/default/#limiting-access-to-logged-in-users-that-pass-a-test

This could be modified to something like:

from django.contrib.auth.decorators import user_passes_test

def group_check(user):
    return user.groups.filter(name__in=['assignment_collector',
                                        'fund_collector' 
                                        'labsheet_collector'])

@user_passes_test(group_check)
def my_view(request):
    ...


If they are not in these groups it will redirect by default to the login page.