2009-01-14 20:22:25 +00:00
|
|
|
"""
|
|
|
|
A second, custom AdminSite -- see tests.CustomAdminSiteTests.
|
|
|
|
"""
|
|
|
|
from django.contrib import admin
|
2012-02-09 18:57:40 +00:00
|
|
|
from django.contrib.auth.admin import UserAdmin
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.http import HttpResponse
|
2018-12-07 22:52:28 +00:00
|
|
|
from django.urls import path
|
2009-01-14 20:22:25 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from . import admin as base_admin
|
|
|
|
from . import forms, models
|
2011-10-13 18:51:33 +00:00
|
|
|
|
2009-01-14 20:22:25 +00:00
|
|
|
|
|
|
|
class Admin2(admin.AdminSite):
|
2013-07-31 06:37:54 +00:00
|
|
|
app_index_template = "custom_admin/app_index.html"
|
2010-12-02 00:44:35 +00:00
|
|
|
login_form = forms.CustomAdminAuthenticationForm
|
2009-01-14 20:22:25 +00:00
|
|
|
login_template = "custom_admin/login.html"
|
2010-01-12 23:34:46 +00:00
|
|
|
logout_template = "custom_admin/logout.html"
|
2013-11-02 21:02:56 +00:00
|
|
|
index_template = ["custom_admin/index.html"] # a list, to test fix for #18697
|
2010-01-12 23:34:46 +00:00
|
|
|
password_change_template = "custom_admin/password_change_form.html"
|
|
|
|
password_change_done_template = "custom_admin/password_change_done.html"
|
2009-07-16 16:16:13 +00:00
|
|
|
|
2009-01-14 20:22:25 +00:00
|
|
|
# A custom index view.
|
|
|
|
def index(self, request, extra_context=None):
|
2017-01-21 13:13:44 +00:00
|
|
|
return super().index(request, {"foo": "*bar*"})
|
2009-07-16 16:16:13 +00:00
|
|
|
|
2009-01-14 20:22:25 +00:00
|
|
|
def get_urls(self):
|
2014-04-02 00:46:34 +00:00
|
|
|
return [
|
2018-12-07 22:52:28 +00:00
|
|
|
path("my_view/", self.admin_view(self.my_view), name="my_view"),
|
2017-01-21 13:13:44 +00:00
|
|
|
] + super().get_urls()
|
2009-07-16 16:16:13 +00:00
|
|
|
|
2009-01-14 20:22:25 +00:00
|
|
|
def my_view(self, request):
|
|
|
|
return HttpResponse("Django is a magical pony!")
|
2009-07-16 16:16:13 +00:00
|
|
|
|
2014-11-29 06:19:59 +00:00
|
|
|
def password_change(self, request, extra_context=None):
|
2017-01-21 13:13:44 +00:00
|
|
|
return super().password_change(request, {"spam": "eggs"})
|
2014-11-29 06:19:59 +00:00
|
|
|
|
2022-03-05 16:09:42 +00:00
|
|
|
def get_app_list(self, request, app_label=None):
|
|
|
|
app_list = super().get_app_list(request, app_label=app_label)
|
|
|
|
# Reverse order of apps and models.
|
|
|
|
app_list = list(reversed(app_list))
|
|
|
|
for app in app_list:
|
|
|
|
app["models"].sort(key=lambda x: x["name"], reverse=True)
|
|
|
|
return app_list
|
|
|
|
|
2012-02-09 18:57:40 +00:00
|
|
|
|
|
|
|
class UserLimitedAdmin(UserAdmin):
|
|
|
|
# used for testing password change on a user not in queryset
|
2013-03-08 14:15:23 +00:00
|
|
|
def get_queryset(self, request):
|
2017-01-21 13:13:44 +00:00
|
|
|
qs = super().get_queryset(request)
|
2012-02-09 18:57:40 +00:00
|
|
|
return qs.filter(is_superuser=False)
|
|
|
|
|
|
|
|
|
2012-12-03 23:39:03 +00:00
|
|
|
class CustomPwdTemplateUserAdmin(UserAdmin):
|
2013-11-02 21:02:56 +00:00
|
|
|
change_user_password_template = [
|
|
|
|
"admin/auth/user/change_password.html"
|
|
|
|
] # a list, to test fix for #18697
|
2012-12-03 23:39:03 +00:00
|
|
|
|
|
|
|
|
2017-03-30 09:13:15 +00:00
|
|
|
class BookAdmin(admin.ModelAdmin):
|
|
|
|
def get_deleted_objects(self, objs, request):
|
|
|
|
return ["a deletable object"], {"books": 1}, set(), []
|
|
|
|
|
|
|
|
|
2009-01-14 20:22:25 +00:00
|
|
|
site = Admin2(name="admin2")
|
|
|
|
|
2011-09-20 18:16:49 +00:00
|
|
|
site.register(models.Article, base_admin.ArticleAdmin)
|
2017-03-30 09:13:15 +00:00
|
|
|
site.register(models.Book, BookAdmin)
|
2017-05-10 12:48:57 +00:00
|
|
|
site.register(
|
|
|
|
models.Section, inlines=[base_admin.ArticleInline], search_fields=["name"]
|
|
|
|
)
|
2011-09-20 18:16:49 +00:00
|
|
|
site.register(models.Thing, base_admin.ThingAdmin)
|
|
|
|
site.register(models.Fabric, base_admin.FabricAdmin)
|
|
|
|
site.register(models.ChapterXtra1, base_admin.ChapterXtra1Admin)
|
2012-02-09 18:57:40 +00:00
|
|
|
site.register(User, UserLimitedAdmin)
|
2012-07-22 01:10:24 +00:00
|
|
|
site.register(models.UndeletableObject, base_admin.UndeletableObjectAdmin)
|
2012-09-08 15:18:08 +00:00
|
|
|
site.register(models.Simple, base_admin.AttributeErrorRaisingAdmin)
|
2012-12-03 23:39:03 +00:00
|
|
|
|
|
|
|
simple_site = Admin2(name="admin4")
|
|
|
|
simple_site.register(User, CustomPwdTemplateUserAdmin)
|