django active_login_required decorator


Here is a little hack I made to the auth.login_required decorator that checks to see if the user account has the is_active flag set as well as being logged in. Nothing earth shattering here but I find it useful. If you look at the original login_required decorator you will see its exactly the same except for the extra is_active flag check.

#A decorator that checks to makes sure a user is active and logged in

from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import user_passes_test
def active_login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
    actual_decorator = user_passes_test(
        lambda u: u.is_authenticated() and u.is_active,
        redirect_field_name=redirect_field_name
    )
    if function:
        return actual_decorator(function)
    return actual_decorator

You use it in the exact same way you use the normal login_required decorator.

In your view

@active_login_required
def my_view(request):
    # ...
Bookmark and Share

blog comments powered by Disqus