| 1 |
from django.contrib import admin |
| 2 |
from django.contrib.auth.admin import UserAdmin |
| 3 |
from django.contrib.auth.models import User |
| 4 |
from django.core.paginator import Paginator |
| 5 |
|
| 6 |
from .models import Band, Child, Event, Genre, GrandChild, Parent, ProxyUser, Swallow |
| 7 |
|
| 8 |
site = admin.AdminSite(name="admin") |
| 9 |
|
| 10 |
site.register(User, UserAdmin) |
| 11 |
|
| 12 |
|
| 13 |
class CustomPaginator(Paginator): |
| 14 |
def __init__(self, queryset, page_size, orphans=0, allow_empty_first_page=True): |
| 15 |
super().__init__( |
| 16 |
queryset, 5, orphans=2, allow_empty_first_page=allow_empty_first_page |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
class EventAdmin(admin.ModelAdmin): |
| 21 |
date_hierarchy = "date" |
| 22 |
list_display = ["event_date_func"] |
| 23 |
|
| 24 |
@admin.display |
| 25 |
def event_date_func(self, event): |
| 26 |
return event.date |
| 27 |
|
| 28 |
def has_add_permission(self, request): |
| 29 |
return False |
| 30 |
|
| 31 |
|
| 32 |
site.register(Event, EventAdmin) |
| 33 |
|
| 34 |
|
| 35 |
class ParentAdmin(admin.ModelAdmin): |
| 36 |
list_filter = ["child__name"] |
| 37 |
search_fields = ["child__name"] |
| 38 |
list_select_related = ["child"] |
| 39 |
|
| 40 |
|
| 41 |
class ParentAdminTwoSearchFields(admin.ModelAdmin): |
| 42 |
list_filter = ["child__name"] |
| 43 |
search_fields = ["child__name", "child__age"] |
| 44 |
list_select_related = ["child"] |
| 45 |
|
| 46 |
|
| 47 |
class ChildAdmin(admin.ModelAdmin): |
| 48 |
list_display = ["name", "parent"] |
| 49 |
list_per_page = 10 |
| 50 |
list_filter = ["parent", "age"] |
| 51 |
search_fields = ["age__exact", "name__exact"] |
| 52 |
|
| 53 |
def get_queryset(self, request): |
| 54 |
return super().get_queryset(request).select_related("parent") |
| 55 |
|
| 56 |
|
| 57 |
class GrandChildAdmin(admin.ModelAdmin): |
| 58 |
list_display = ["name", "parent__name", "parent__parent__name"] |
| 59 |
search_fields = ["parent__name__exact", "parent__age__exact"] |
| 60 |
|
| 61 |
|
| 62 |
site.register(GrandChild, GrandChildAdmin) |
| 63 |
|
| 64 |
|
| 65 |
class CustomPaginationAdmin(ChildAdmin): |
| 66 |
paginator = CustomPaginator |
| 67 |
|
| 68 |
|
| 69 |
class FilteredChildAdmin(admin.ModelAdmin): |
| 70 |
list_display = ["name", "parent"] |
| 71 |
list_per_page = 10 |
| 72 |
|
| 73 |
def get_queryset(self, request): |
| 74 |
return super().get_queryset(request).filter(name__contains="filtered") |
| 75 |
|
| 76 |
|
| 77 |
class BandAdmin(admin.ModelAdmin): |
| 78 |
list_filter = ["genres"] |
| 79 |
|
| 80 |
|
| 81 |
class NrOfMembersFilter(admin.SimpleListFilter): |
| 82 |
title = "number of members" |
| 83 |
parameter_name = "nr_of_members_partition" |
| 84 |
|
| 85 |
def lookups(self, request, model_admin): |
| 86 |
return [ |
| 87 |
("5", "0 - 5"), |
| 88 |
("more", "more than 5"), |
| 89 |
] |
| 90 |
|
| 91 |
def queryset(self, request, queryset): |
| 92 |
value = self.value() |
| 93 |
if value == "5": |
| 94 |
return queryset.filter(nr_of_members__lte=5) |
| 95 |
if value == "more": |
| 96 |
return queryset.filter(nr_of_members__gt=5) |
| 97 |
|
| 98 |
|
| 99 |
class BandCallableFilterAdmin(admin.ModelAdmin): |
| 100 |
list_filter = [NrOfMembersFilter] |
| 101 |
|
| 102 |
|
| 103 |
site.register(Band, BandCallableFilterAdmin) |
| 104 |
|
| 105 |
|
| 106 |
class GroupAdmin(admin.ModelAdmin): |
| 107 |
list_filter = ["members"] |
| 108 |
|
| 109 |
|
| 110 |
class ConcertAdmin(admin.ModelAdmin): |
| 111 |
list_filter = ["group__members"] |
| 112 |
search_fields = ["group__members__name"] |
| 113 |
|
| 114 |
|
| 115 |
class QuartetAdmin(admin.ModelAdmin): |
| 116 |
list_filter = ["members"] |
| 117 |
|
| 118 |
|
| 119 |
class ChordsBandAdmin(admin.ModelAdmin): |
| 120 |
list_filter = ["members"] |
| 121 |
|
| 122 |
|
| 123 |
class InvitationAdmin(admin.ModelAdmin): |
| 124 |
list_display = ("band", "player") |
| 125 |
list_select_related = ("player",) |
| 126 |
|
| 127 |
|
| 128 |
class DynamicListDisplayChildAdmin(admin.ModelAdmin): |
| 129 |
list_display = ("parent", "name", "age") |
| 130 |
|
| 131 |
def get_list_display(self, request): |
| 132 |
my_list_display = super().get_list_display(request) |
| 133 |
if request.user.username == "noparents": |
| 134 |
my_list_display = list(my_list_display) |
| 135 |
my_list_display.remove("parent") |
| 136 |
return my_list_display |
| 137 |
|
| 138 |
|
| 139 |
class DynamicListDisplayLinksChildAdmin(admin.ModelAdmin): |
| 140 |
list_display = ("parent", "name", "age") |
| 141 |
list_display_links = ["parent", "name"] |
| 142 |
|
| 143 |
def get_list_display_links(self, request, list_display): |
| 144 |
return ["age"] |
| 145 |
|
| 146 |
|
| 147 |
site.register(Child, DynamicListDisplayChildAdmin) |
| 148 |
|
| 149 |
|
| 150 |
class NoListDisplayLinksParentAdmin(admin.ModelAdmin): |
| 151 |
list_display_links = None |
| 152 |
list_display = ["name"] |
| 153 |
list_editable = ["name"] |
| 154 |
actions_on_bottom = True |
| 155 |
|
| 156 |
|
| 157 |
site.register(Parent, NoListDisplayLinksParentAdmin) |
| 158 |
|
| 159 |
|
| 160 |
class ListDisplayLinksGenreAdmin(admin.ModelAdmin): |
| 161 |
list_display = ["name", "file", "url"] |
| 162 |
list_display_links = ["file", "url"] |
| 163 |
|
| 164 |
|
| 165 |
site.register(Genre, ListDisplayLinksGenreAdmin) |
| 166 |
|
| 167 |
|
| 168 |
class SwallowAdmin(admin.ModelAdmin): |
| 169 |
actions = None # prevent ['action_checkbox'] + list(list_display) |
| 170 |
list_display = ("origin", "load", "speed", "swallowonetoone") |
| 171 |
list_editable = ["load", "speed"] |
| 172 |
list_per_page = 3 |
| 173 |
|
| 174 |
|
| 175 |
site.register(Swallow, SwallowAdmin) |
| 176 |
|
| 177 |
|
| 178 |
class DynamicListFilterChildAdmin(admin.ModelAdmin): |
| 179 |
list_filter = ("parent", "name", "age") |
| 180 |
|
| 181 |
def get_list_filter(self, request): |
| 182 |
my_list_filter = super().get_list_filter(request) |
| 183 |
if request.user.username == "noparents": |
| 184 |
my_list_filter = list(my_list_filter) |
| 185 |
my_list_filter.remove("parent") |
| 186 |
return my_list_filter |
| 187 |
|
| 188 |
|
| 189 |
class DynamicSearchFieldsChildAdmin(admin.ModelAdmin): |
| 190 |
search_fields = ("name",) |
| 191 |
|
| 192 |
def get_search_fields(self, request): |
| 193 |
search_fields = super().get_search_fields(request) |
| 194 |
search_fields += ("age",) |
| 195 |
return search_fields |
| 196 |
|
| 197 |
|
| 198 |
class EmptyValueChildAdmin(admin.ModelAdmin): |
| 199 |
empty_value_display = "-empty-" |
| 200 |
list_display = ("name", "age_display", "age") |
| 201 |
|
| 202 |
@admin.display(empty_value="†") |
| 203 |
def age_display(self, obj): |
| 204 |
return obj.age |
| 205 |
|
| 206 |
|
| 207 |
class UnescapedTitleFilter(admin.SimpleListFilter): |
| 208 |
title = "It's OK" |
| 209 |
parameter_name = "is_active" |
| 210 |
|
| 211 |
def lookups(self, request, model_admin): |
| 212 |
return [("yes", "yes"), ("no", "no")] |
| 213 |
|
| 214 |
def queryset(self, request, queryset): |
| 215 |
if self.value() == "yes": |
| 216 |
return queryset.filter(is_active=True) |
| 217 |
else: |
| 218 |
return queryset.filter(is_active=False) |
| 219 |
|
| 220 |
|
| 221 |
class CustomUserAdmin(UserAdmin): |
| 222 |
list_filter = [UnescapedTitleFilter] |
| 223 |
|
| 224 |
|
| 225 |
site.register(ProxyUser, CustomUserAdmin) |