Skip to content

Instantly share code, notes, and snippets.

@dstufft
Created September 15, 2011 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstufft/1220199 to your computer and use it in GitHub Desktop.
Save dstufft/1220199 to your computer and use it in GitHub Desktop.
Login Required Decorator
from django.conf import settings
from django.http import HttpResponseRedirect
class LoginRequired(object):
def dispatch(self, request, *args, **kwargs):
if self.check_logged_in(request.user):
return super(LoginRequired, self).dispatch(request, *args, **kwargs)
else:
return self.not_logged_in(self):
def check_logged_in(self, user):
return user.is_authenticated()
def not_logged_in(self):
return HttpResponseRedirect(settings.LOGIN_URL)
## No Special Behavior, just Login Required
class ProtectedView(LoginRequired, TemplateView):
template_name = "secret.html"
## Check to make sure the user is active and logged in
class ProtectedView(LoginRequired, TemplateView):
template_name = "secret.html"
def check_logged_in(self, user):
return super(ProtectedView, self).check_logged_in(user) and user.is_active
## Check Logged in, and return a 403
class ProtectedView(LoginRequired, TemplateView):
template_name = "secret.html"
def not_logged_in(self):
return HttpResponseForbidden()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment