Anonymous required decorator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from django.http import HttpResponseRedirect

def anonymous_required( view_function, redirect_to = None ):
    return AnonymousRequired( view_function, redirect_to )

class AnonymousRequired( object ):
    def __init__( self, view_function, redirect_to ):
        if redirect_to is None:
            from django.conf import settings
            redirect_to = settings.LOGIN_REDIRECT_URL
        self.view_function = view_function
        self.redirect_to = redirect_to

    def __call__( self, request, *args, **kwargs ):
        if request.user is not None and request.user.is_authenticated():
            return HttpResponseRedirect( self.redirect_to ) 
        return self.view_function( request, *args, **kwargs )

More like this

  1. LoginRequiredMixin by slink 1 year, 12 months ago
  2. Auth decorators with 403 by Magus 5 years, 11 months ago
  3. Log username in Apache access logs by arthur 3 years, 2 months ago
  4. Login + logout script from a nub by alandelevie 4 years, 4 months ago
  5. New view decorator to only cache pages for anonymous users by vaughnkoch 2 years, 7 months ago

Comments

Motanelu (on February 11, 2010):

Fixed the condition.

#

(Forgotten your password?)