2014-12-31 22:25:00 +01:00
|
|
|
"""
|
|
|
|
A custom AdminSite for AdminViewPermissionsTest.test_login_has_permission().
|
|
|
|
"""
|
2024-01-26 12:45:07 +01:00
|
|
|
|
2014-12-31 22:25:00 +01:00
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib.auth import get_permission_codename
|
|
|
|
from django.contrib.auth.forms import AuthenticationForm
|
2020-02-12 14:48:49 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
2014-12-31 22:25:00 +01:00
|
|
|
|
2015-01-28 13:35:27 +01:00
|
|
|
from . import admin as base_admin
|
|
|
|
from . import models
|
2014-12-31 22:25:00 +01:00
|
|
|
|
|
|
|
PERMISSION_NAME = "admin_views.%s" % get_permission_codename(
|
|
|
|
"change", models.Article._meta
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class PermissionAdminAuthenticationForm(AuthenticationForm):
|
|
|
|
def confirm_login_allowed(self, user):
|
|
|
|
if not user.is_active or not (user.is_staff or user.has_perm(PERMISSION_NAME)):
|
2020-02-12 14:48:49 +01:00
|
|
|
raise ValidationError("permission denied")
|
2014-12-31 22:25:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HasPermissionAdmin(admin.AdminSite):
|
|
|
|
login_form = PermissionAdminAuthenticationForm
|
|
|
|
|
|
|
|
def has_permission(self, request):
|
|
|
|
return request.user.is_active and (
|
|
|
|
request.user.is_staff or request.user.has_perm(PERMISSION_NAME)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
site = HasPermissionAdmin(name="has_permission_admin")
|
|
|
|
site.register(models.Article, base_admin.ArticleAdmin)
|