0

I need to make sure that authenticated user is active for access the page. Is there any mixin for that in django? If not then how to do so. Thanks in advance.

Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50

1 Answers1

1

It's working fine for me

from django.contrib.auth.mixins import AccessMixin
from django.contrib import messages
from django.core.exceptions import PermissionDenied, ImproperlyConfigured
from django.shortcuts import redirect
from django.contrib.auth.views import redirect_to_login


class ActiveOnlyMixin(AccessMixin):

permission_denied_message = ''
not_activated_message = ''
not_activated_redirect = ''

def get_not_activated_message(self):
    return self.not_activated_message

def handle_not_activated(self):

    message = self.get_not_activated_message()
    if self.raise_exception:
        raise PermissionDenied(message)
    messages.error(self.request, message)
    return redirect(self.get_not_activated_redirect())

def get_not_activated_redirect(self):


    if not self.not_activated_redirect:
        raise ImproperlyConfigured(
            '{0} is missing the not_activated_redirect attribute. Define {0}.not_activated_redirect, or override '
            '{0}.get_not_activated_redirect().'.format(self.__class__.__name__))
    return self.not_activated_redirect

def handle_no_permission(self):

    message = self.get_permission_denied_message()
    if self.raise_exception:
        raise PermissionDenied(message)
    messages.error(self.request, message)
    return redirect_to_login(self.request.get_full_path(), self.get_login_url(), self.get_redirect_field_name())

def dispatch(self, request, *args, **kwargs):
    if not request.user.is_authenticated():
        return self.handle_no_permission()
    if not request.user.is_active:
        return self.handle_not_activated()
    return super(ActiveOnlyMixin, self).dispatch(request, *args, **kwargs)

and in views.py

from myapp.mixins import ActiveOnlyMixin

class MyView(ActiveOnlyMixin, View):
    permission_denied_message = 'You must be logged in to view this page'
    not_activated_message = 'You haven\'t activated your account yet'

    not_activated_redirect = 'accounts:inactive_registration'
Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50