Skip to content

Commit

Permalink
Rewrite of the login required mixin so that it has the same features …
Browse files Browse the repository at this point in the history
…as the other 'access' mixins.
  • Loading branch information
chrisjones-brack3t committed Feb 27, 2013
1 parent 2332ae8 commit 37b31bc
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions braces/views.py
@@ -1,6 +1,5 @@
from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import login_required
from django.contrib.auth.views import redirect_to_login
from django.core import serializers
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
Expand Down Expand Up @@ -41,16 +40,25 @@ def get_success_url(self):

class LoginRequiredMixin(object):
"""
View mixin which verifies that the user has authenticated.
View mixin which verifies that the user is authenticated.
NOTE:
This should be the left-most mixin of a view.
"""
login_url = settings.LOGIN_URL # LOGIN_URL from project settings
raise_exception = False # Default whether to raise an exception to none
redirect_field_name = REDIRECT_FIELD_NAME # Set by django.contrib.auth

@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(LoginRequiredMixin, self).dispatch(request,
*args, **kwargs)
if not request.user.is_authenticated():
if self.raise_exception:
raise PermissionDenied # return a forbidden response
else:
return redirect_to_login(request.get_full_path(),
self.login_url, self.redirect_field_name)

return super(LoginRequiredMixin, self).dispatch(request, *args,
**kwargs)


class CsrfExemptMixin(object):
Expand Down

0 comments on commit 37b31bc

Please sign in to comment.