| 1 |
""" |
| 2 |
A second, custom AdminSite -- see tests.CustomAdminSiteTests. |
| 3 |
""" |
| 4 |
|
| 5 |
from django.contrib import admin |
| 6 |
from django.contrib.auth.admin import UserAdmin |
| 7 |
from django.contrib.auth.models import User |
| 8 |
from django.http import HttpResponse |
| 9 |
from django.urls import path |
| 10 |
|
| 11 |
from . import admin as base_admin |
| 12 |
from . import forms, models |
| 13 |
|
| 14 |
|
| 15 |
class Admin2(admin.AdminSite): |
| 16 |
app_index_template = "custom_admin/app_index.html" |
| 17 |
login_form = forms.CustomAdminAuthenticationForm |
| 18 |
login_template = "custom_admin/login.html" |
| 19 |
logout_template = "custom_admin/logout.html" |
| 20 |
index_template = ["custom_admin/index.html"] # a list, to test fix for #18697 |
| 21 |
password_change_template = "custom_admin/password_change_form.html" |
| 22 |
password_change_done_template = "custom_admin/password_change_done.html" |
| 23 |
|
| 24 |
# A custom index view. |
| 25 |
def index(self, request, extra_context=None): |
| 26 |
return super().index(request, {"foo": "*bar*"}) |
| 27 |
|
| 28 |
def get_urls(self): |
| 29 |
return [ |
| 30 |
path("my_view/", self.admin_view(self.my_view), name="my_view"), |
| 31 |
] + super().get_urls() |
| 32 |
|
| 33 |
def my_view(self, request): |
| 34 |
return HttpResponse("Django is a magical pony!") |
| 35 |
|
| 36 |
def password_change(self, request, extra_context=None): |
| 37 |
return super().password_change(request, {"spam": "eggs"}) |
| 38 |
|
| 39 |
def get_app_list(self, request, app_label=None): |
| 40 |
app_list = super().get_app_list(request, app_label=app_label) |
| 41 |
# Reverse order of apps and models. |
| 42 |
app_list = list(reversed(app_list)) |
| 43 |
for app in app_list: |
| 44 |
app["models"].sort(key=lambda x: x["name"], reverse=True) |
| 45 |
return app_list |
| 46 |
|
| 47 |
|
| 48 |
class UserLimitedAdmin(UserAdmin): |
| 49 |
# used for testing password change on a user not in queryset |
| 50 |
def get_queryset(self, request): |
| 51 |
qs = super().get_queryset(request) |
| 52 |
return qs.filter(is_superuser=False) |
| 53 |
|
| 54 |
|
| 55 |
class CustomPwdTemplateUserAdmin(UserAdmin): |
| 56 |
change_user_password_template = [ |
| 57 |
"admin/auth/user/change_password.html" |
| 58 |
] # a list, to test fix for #18697 |
| 59 |
|
| 60 |
|
| 61 |
class BookAdmin(admin.ModelAdmin): |
| 62 |
def get_deleted_objects(self, objs, request): |
| 63 |
return ["a deletable object"], {"books": 1}, set(), [] |
| 64 |
|
| 65 |
|
| 66 |
site = Admin2(name="admin2") |
| 67 |
|
| 68 |
site.register(models.Article, base_admin.ArticleAdmin) |
| 69 |
site.register(models.Book, BookAdmin) |
| 70 |
site.register( |
| 71 |
models.Section, inlines=[base_admin.ArticleInline], search_fields=["name"] |
| 72 |
) |
| 73 |
site.register(models.Thing, base_admin.ThingAdmin) |
| 74 |
site.register(models.Fabric, base_admin.FabricAdmin) |
| 75 |
site.register(models.ChapterXtra1, base_admin.ChapterXtra1Admin) |
| 76 |
site.register(User, UserLimitedAdmin) |
| 77 |
site.register(models.UndeletableObject, base_admin.UndeletableObjectAdmin) |
| 78 |
site.register(models.Simple, base_admin.AttributeErrorRaisingAdmin) |
| 79 |
|
| 80 |
simple_site = Admin2(name="admin4") |
| 81 |
simple_site.register(User, CustomPwdTemplateUserAdmin) |