2011-09-20 20:16:49 +02:00
|
|
|
from django.contrib import admin
|
2014-02-22 11:36:15 +01:00
|
|
|
from django.contrib.auth.admin import UserAdmin
|
|
|
|
from django.contrib.auth.models import User
|
2015-01-28 13:35:27 +01:00
|
|
|
from django.core.paginator import Paginator
|
2011-09-20 20:16:49 +02:00
|
|
|
|
2023-11-16 09:11:27 +01:00
|
|
|
from .models import Band, Child, Event, GrandChild, Parent, ProxyUser, Swallow
|
2011-10-13 20:51:33 +02:00
|
|
|
|
2011-09-20 20:16:49 +02:00
|
|
|
site = admin.AdminSite(name="admin")
|
|
|
|
|
2014-02-22 11:36:15 +01:00
|
|
|
site.register(User, UserAdmin)
|
|
|
|
|
2013-09-06 20:25:13 +02:00
|
|
|
|
2011-09-20 20:16:49 +02:00
|
|
|
class CustomPaginator(Paginator):
|
|
|
|
def __init__(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
|
2017-01-21 14:13:44 +01:00
|
|
|
super().__init__(
|
|
|
|
queryset, 5, orphans=2, allow_empty_first_page=allow_empty_first_page
|
|
|
|
)
|
2011-09-20 20:16:49 +02:00
|
|
|
|
|
|
|
|
2012-04-29 11:51:12 +02:00
|
|
|
class EventAdmin(admin.ModelAdmin):
|
2017-12-16 19:50:11 +01:00
|
|
|
date_hierarchy = "date"
|
2012-04-29 11:51:12 +02:00
|
|
|
list_display = ["event_date_func"]
|
|
|
|
|
2021-01-13 17:19:22 +01:00
|
|
|
@admin.display
|
2012-04-29 11:51:12 +02:00
|
|
|
def event_date_func(self, event):
|
|
|
|
return event.date
|
|
|
|
|
2015-11-07 12:31:06 +01:00
|
|
|
def has_add_permission(self, request):
|
|
|
|
return False
|
|
|
|
|
2016-11-12 18:11:23 +01:00
|
|
|
|
2012-04-29 11:51:12 +02:00
|
|
|
site.register(Event, EventAdmin)
|
|
|
|
|
|
|
|
|
2011-09-20 20:16:49 +02:00
|
|
|
class ParentAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ["child__name"]
|
|
|
|
search_fields = ["child__name"]
|
2021-04-26 12:07:16 +02:00
|
|
|
list_select_related = ["child"]
|
2011-09-20 20:16:49 +02:00
|
|
|
|
|
|
|
|
2021-10-06 03:38:15 +02:00
|
|
|
class ParentAdminTwoSearchFields(admin.ModelAdmin):
|
|
|
|
list_filter = ["child__name"]
|
|
|
|
search_fields = ["child__name", "child__age"]
|
|
|
|
list_select_related = ["child"]
|
|
|
|
|
|
|
|
|
2011-09-20 20:16:49 +02:00
|
|
|
class ChildAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ["name", "parent"]
|
|
|
|
list_per_page = 10
|
2012-11-25 20:39:23 +01:00
|
|
|
list_filter = ["parent", "age"]
|
2024-11-01 08:26:10 +01:00
|
|
|
search_fields = ["age__exact", "name__exact"]
|
2011-09-20 20:16:49 +02:00
|
|
|
|
2013-03-08 15:15:23 +01:00
|
|
|
def get_queryset(self, request):
|
2017-01-21 14:13:44 +01:00
|
|
|
return super().get_queryset(request).select_related("parent")
|
2011-09-20 20:16:49 +02:00
|
|
|
|
|
|
|
|
2023-04-04 16:11:11 +02:00
|
|
|
class GrandChildAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ["name", "parent__name", "parent__parent__name"]
|
2024-11-04 16:09:55 +01:00
|
|
|
search_fields = ["parent__name__exact", "parent__age__exact"]
|
2023-04-04 16:11:11 +02:00
|
|
|
|
|
|
|
|
2023-11-16 09:11:27 +01:00
|
|
|
site.register(GrandChild, GrandChildAdmin)
|
|
|
|
|
|
|
|
|
2011-09-20 20:16:49 +02:00
|
|
|
class CustomPaginationAdmin(ChildAdmin):
|
|
|
|
paginator = CustomPaginator
|
|
|
|
|
|
|
|
|
|
|
|
class FilteredChildAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ["name", "parent"]
|
|
|
|
list_per_page = 10
|
|
|
|
|
2013-03-08 15:15:23 +01:00
|
|
|
def get_queryset(self, request):
|
2017-01-21 14:13:44 +01:00
|
|
|
return super().get_queryset(request).filter(name__contains="filtered")
|
2011-09-20 20:16:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BandAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ["genres"]
|
|
|
|
|
|
|
|
|
2020-05-19 09:03:04 +02:00
|
|
|
class NrOfMembersFilter(admin.SimpleListFilter):
|
|
|
|
title = "number of members"
|
|
|
|
parameter_name = "nr_of_members_partition"
|
|
|
|
|
|
|
|
def lookups(self, request, model_admin):
|
|
|
|
return [
|
|
|
|
("5", "0 - 5"),
|
|
|
|
("more", "more than 5"),
|
|
|
|
]
|
|
|
|
|
|
|
|
def queryset(self, request, queryset):
|
|
|
|
value = self.value()
|
|
|
|
if value == "5":
|
|
|
|
return queryset.filter(nr_of_members__lte=5)
|
|
|
|
if value == "more":
|
|
|
|
return queryset.filter(nr_of_members__gt=5)
|
|
|
|
|
|
|
|
|
|
|
|
class BandCallableFilterAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = [NrOfMembersFilter]
|
|
|
|
|
|
|
|
|
|
|
|
site.register(Band, BandCallableFilterAdmin)
|
|
|
|
|
|
|
|
|
2011-09-20 20:16:49 +02:00
|
|
|
class GroupAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ["members"]
|
|
|
|
|
|
|
|
|
2015-04-14 11:09:58 +02:00
|
|
|
class ConcertAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ["group__members"]
|
|
|
|
search_fields = ["group__members__name"]
|
|
|
|
|
|
|
|
|
2011-09-20 20:16:49 +02:00
|
|
|
class QuartetAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ["members"]
|
|
|
|
|
|
|
|
|
|
|
|
class ChordsBandAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ["members"]
|
|
|
|
|
|
|
|
|
2013-06-04 23:35:11 +02:00
|
|
|
class InvitationAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ("band", "player")
|
|
|
|
list_select_related = ("player",)
|
|
|
|
|
|
|
|
|
2011-09-20 20:16:49 +02:00
|
|
|
class DynamicListDisplayChildAdmin(admin.ModelAdmin):
|
2011-10-26 14:19:18 +02:00
|
|
|
list_display = ("parent", "name", "age")
|
2011-09-20 20:16:49 +02:00
|
|
|
|
|
|
|
def get_list_display(self, request):
|
2017-01-21 14:13:44 +01:00
|
|
|
my_list_display = super().get_list_display(request)
|
2011-09-20 20:16:49 +02:00
|
|
|
if request.user.username == "noparents":
|
2011-10-26 14:19:18 +02:00
|
|
|
my_list_display = list(my_list_display)
|
2011-09-20 20:16:49 +02:00
|
|
|
my_list_display.remove("parent")
|
|
|
|
return my_list_display
|
|
|
|
|
2013-09-06 20:25:13 +02:00
|
|
|
|
2011-10-26 14:19:18 +02:00
|
|
|
class DynamicListDisplayLinksChildAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ("parent", "name", "age")
|
|
|
|
list_display_links = ["parent", "name"]
|
|
|
|
|
|
|
|
def get_list_display_links(self, request, list_display):
|
|
|
|
return ["age"]
|
|
|
|
|
2016-11-12 18:11:23 +01:00
|
|
|
|
2011-09-20 20:16:49 +02:00
|
|
|
site.register(Child, DynamicListDisplayChildAdmin)
|
2011-11-17 22:30:07 +01:00
|
|
|
|
2013-09-06 20:25:13 +02:00
|
|
|
|
|
|
|
class NoListDisplayLinksParentAdmin(admin.ModelAdmin):
|
|
|
|
list_display_links = None
|
2020-04-26 02:15:16 +02:00
|
|
|
list_display = ["name"]
|
|
|
|
list_editable = ["name"]
|
2021-09-21 19:58:00 +02:00
|
|
|
actions_on_bottom = True
|
2013-09-06 20:25:13 +02:00
|
|
|
|
2016-11-12 18:11:23 +01:00
|
|
|
|
2013-09-06 20:25:13 +02:00
|
|
|
site.register(Parent, NoListDisplayLinksParentAdmin)
|
|
|
|
|
|
|
|
|
2011-11-17 22:30:07 +01:00
|
|
|
class SwallowAdmin(admin.ModelAdmin):
|
2013-11-02 22:02:56 +01:00
|
|
|
actions = None # prevent ['action_checkbox'] + list(list_display)
|
2015-05-27 17:43:22 +02:00
|
|
|
list_display = ("origin", "load", "speed", "swallowonetoone")
|
2015-11-19 17:13:39 +01:00
|
|
|
list_editable = ["load", "speed"]
|
|
|
|
list_per_page = 3
|
2011-11-17 22:30:07 +01:00
|
|
|
|
2016-11-12 18:11:23 +01:00
|
|
|
|
2011-11-17 22:30:07 +01:00
|
|
|
site.register(Swallow, SwallowAdmin)
|
2012-11-25 20:39:23 +01:00
|
|
|
|
2013-09-06 20:25:13 +02:00
|
|
|
|
2012-11-25 20:39:23 +01:00
|
|
|
class DynamicListFilterChildAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ("parent", "name", "age")
|
|
|
|
|
|
|
|
def get_list_filter(self, request):
|
2017-01-21 14:13:44 +01:00
|
|
|
my_list_filter = super().get_list_filter(request)
|
2012-11-25 20:39:23 +01:00
|
|
|
if request.user.username == "noparents":
|
|
|
|
my_list_filter = list(my_list_filter)
|
|
|
|
my_list_filter.remove("parent")
|
|
|
|
return my_list_filter
|
|
|
|
|
2013-09-06 20:25:13 +02:00
|
|
|
|
2013-08-03 18:15:15 +02:00
|
|
|
class DynamicSearchFieldsChildAdmin(admin.ModelAdmin):
|
|
|
|
search_fields = ("name",)
|
|
|
|
|
|
|
|
def get_search_fields(self, request):
|
2017-01-21 14:13:44 +01:00
|
|
|
search_fields = super().get_search_fields(request)
|
2013-08-03 18:15:15 +02:00
|
|
|
search_fields += ("age",)
|
|
|
|
return search_fields
|
2015-03-13 11:08:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
class EmptyValueChildAdmin(admin.ModelAdmin):
|
|
|
|
empty_value_display = "-empty-"
|
|
|
|
list_display = ("name", "age_display", "age")
|
|
|
|
|
2021-01-13 17:19:22 +01:00
|
|
|
@admin.display(empty_value="†")
|
2015-03-13 11:08:03 +01:00
|
|
|
def age_display(self, obj):
|
|
|
|
return obj.age
|
2023-01-25 20:53:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UnescapedTitleFilter(admin.SimpleListFilter):
|
|
|
|
title = "It's OK"
|
|
|
|
parameter_name = "is_active"
|
|
|
|
|
|
|
|
def lookups(self, request, model_admin):
|
|
|
|
return [("yes", "yes"), ("no", "no")]
|
|
|
|
|
|
|
|
def queryset(self, request, queryset):
|
|
|
|
if self.value() == "yes":
|
|
|
|
return queryset.filter(is_active=True)
|
|
|
|
else:
|
|
|
|
return queryset.filter(is_active=False)
|
|
|
|
|
|
|
|
|
|
|
|
class CustomUserAdmin(UserAdmin):
|
|
|
|
list_filter = [UnescapedTitleFilter]
|
|
|
|
|
|
|
|
|
|
|
|
site.register(ProxyUser, CustomUserAdmin)
|