class PermissionRequiredMixin
from django.contrib.auth.mixins import PermissionRequiredMixin
Verify that the current user has all specified permissions.
Ancestors (MRO)
- PermissionRequiredMixin
- AccessMixin
Attributes
Defined in | |
---|---|
login_url = None
|
AccessMixin |
permission_denied_message = ''
|
AccessMixin |
permission_required = None
|
PermissionRequiredMixin |
raise_exception = False
|
AccessMixin |
redirect_field_name = 'next'
|
AccessMixin |
Methods
def
dispatch(self, request, *args, **kwargs):
PermissionRequiredMixin
¶
def
dispatch(self, request, *args, **kwargs):
PermissionRequiredMixin
¶
101 102 103 104 | def dispatch(self, request, *args, **kwargs):
if not self.has_permission():
return self.handle_no_permission()
return super().dispatch(request, *args, **kwargs)
|
def
get_login_url(self):
AccessMixin
¶
def
get_login_url(self):
AccessMixin
¶
Override this method to override the login_url attribute.
20 21 22 23 24 25 26 27 28 29 30 | def get_login_url(self):
"""
Override this method to override the login_url attribute.
"""
login_url = self.login_url or settings.LOGIN_URL
if not login_url:
raise ImproperlyConfigured(
'{0} is missing the login_url attribute. Define {0}.login_url, settings.LOGIN_URL, or override '
'{0}.get_login_url().'.format(self.__class__.__name__)
)
return str(login_url)
|
def
get_permission_denied_message(self):
AccessMixin
¶
def
get_permission_denied_message(self):
AccessMixin
¶
Override this method to override the permission_denied_message attribute.
32 33 34 35 36 | def get_permission_denied_message(self):
"""
Override this method to override the permission_denied_message attribute.
"""
return self.permission_denied_message
|
def
get_permission_required(self):
PermissionRequiredMixin
¶
def
get_permission_required(self):
PermissionRequiredMixin
¶
Override this method to override the permission_required attribute. Must return an iterable.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | def get_permission_required(self):
"""
Override this method to override the permission_required attribute.
Must return an iterable.
"""
if self.permission_required is None:
raise ImproperlyConfigured(
'{0} is missing the permission_required attribute. Define {0}.permission_required, or override '
'{0}.get_permission_required().'.format(self.__class__.__name__)
)
if isinstance(self.permission_required, str):
perms = (self.permission_required,)
else:
perms = self.permission_required
return perms
|
def
get_redirect_field_name(self):
AccessMixin
¶
def
get_redirect_field_name(self):
AccessMixin
¶
Override this method to override the redirect_field_name attribute.
38 39 40 41 42 | def get_redirect_field_name(self):
"""
Override this method to override the redirect_field_name attribute.
"""
return self.redirect_field_name
|
def
handle_no_permission(self):
AccessMixin
¶
def
handle_no_permission(self):
AccessMixin
¶
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | def handle_no_permission(self):
if self.raise_exception or self.request.user.is_authenticated:
raise PermissionDenied(self.get_permission_denied_message())
path = self.request.build_absolute_uri()
resolved_login_url = resolve_url(self.get_login_url())
# If the login url is the same scheme and net location then use the
# path as the "next" url.
login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
current_scheme, current_netloc = urlparse(path)[:2]
if (
(not login_scheme or login_scheme == current_scheme) and
(not login_netloc or login_netloc == current_netloc)
):
path = self.request.get_full_path()
return redirect_to_login(
path,
resolved_login_url,
self.get_redirect_field_name(),
)
|
def
has_permission(self):
PermissionRequiredMixin
¶
def
has_permission(self):
PermissionRequiredMixin
¶
Override this method to customize the way permissions are checked.
94 95 96 97 98 99 | def has_permission(self):
"""
Override this method to customize the way permissions are checked.
"""
perms = self.get_permission_required()
return self.request.user.has_perms(perms)
|