2011-09-20 18:16:49 +00:00
|
|
|
from django.contrib import admin
|
2014-02-22 10:36:15 +00:00
|
|
|
from django.contrib.auth.admin import UserAdmin
|
|
|
|
from django.contrib.auth.models import User
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.core.paginator import Paginator
|
2011-09-20 18:16:49 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import Child, Event, Parent, Swallow
|
2011-10-13 18:51:33 +00:00
|
|
|
|
2011-09-20 18:16:49 +00:00
|
|
|
site = admin.AdminSite(name="admin")
|
|
|
|
|
2014-02-22 10:36:15 +00:00
|
|
|
site.register(User, UserAdmin)
|
|
|
|
|
2013-09-06 18:25:13 +00:00
|
|
|
|
2011-09-20 18:16:49 +00:00
|
|
|
class CustomPaginator(Paginator):
|
|
|
|
def __init__(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
|
|
|
|
super(CustomPaginator, self).__init__(queryset, 5, orphans=2,
|
|
|
|
allow_empty_first_page=allow_empty_first_page)
|
|
|
|
|
|
|
|
|
2012-04-29 09:51:12 +00:00
|
|
|
class EventAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ['event_date_func']
|
|
|
|
|
|
|
|
def event_date_func(self, event):
|
|
|
|
return event.date
|
|
|
|
|
|
|
|
site.register(Event, EventAdmin)
|
|
|
|
|
|
|
|
|
2011-09-20 18:16:49 +00:00
|
|
|
class ParentAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ['child__name']
|
|
|
|
search_fields = ['child__name']
|
|
|
|
|
|
|
|
|
|
|
|
class ChildAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ['name', 'parent']
|
|
|
|
list_per_page = 10
|
2012-11-25 19:39:23 +00:00
|
|
|
list_filter = ['parent', 'age']
|
2011-09-20 18:16:49 +00:00
|
|
|
|
2013-03-08 14:15:23 +00:00
|
|
|
def get_queryset(self, request):
|
|
|
|
return super(ChildAdmin, self).get_queryset(request).select_related("parent__name")
|
2011-09-20 18:16:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CustomPaginationAdmin(ChildAdmin):
|
|
|
|
paginator = CustomPaginator
|
|
|
|
|
|
|
|
|
|
|
|
class FilteredChildAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ['name', 'parent']
|
|
|
|
list_per_page = 10
|
|
|
|
|
2013-03-08 14:15:23 +00:00
|
|
|
def get_queryset(self, request):
|
|
|
|
return super(FilteredChildAdmin, self).get_queryset(request).filter(
|
2011-09-20 18:16:49 +00:00
|
|
|
name__contains='filtered')
|
|
|
|
|
|
|
|
|
|
|
|
class BandAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ['genres']
|
|
|
|
|
|
|
|
|
|
|
|
class GroupAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ['members']
|
|
|
|
|
|
|
|
|
|
|
|
class QuartetAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ['members']
|
|
|
|
|
|
|
|
|
|
|
|
class ChordsBandAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ['members']
|
|
|
|
|
|
|
|
|
2013-06-04 21:35:11 +00:00
|
|
|
class InvitationAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ('band', 'player')
|
|
|
|
list_select_related = ('player',)
|
|
|
|
|
|
|
|
|
2011-09-20 18:16:49 +00:00
|
|
|
class DynamicListDisplayChildAdmin(admin.ModelAdmin):
|
2011-10-26 12:19:18 +00:00
|
|
|
list_display = ('parent', 'name', 'age')
|
2011-09-20 18:16:49 +00:00
|
|
|
|
|
|
|
def get_list_display(self, request):
|
2011-10-26 12:19:18 +00:00
|
|
|
my_list_display = super(DynamicListDisplayChildAdmin, self).get_list_display(request)
|
2011-09-20 18:16:49 +00:00
|
|
|
if request.user.username == 'noparents':
|
2011-10-26 12:19:18 +00:00
|
|
|
my_list_display = list(my_list_display)
|
2011-09-20 18:16:49 +00:00
|
|
|
my_list_display.remove('parent')
|
|
|
|
return my_list_display
|
|
|
|
|
2013-09-06 18:25:13 +00:00
|
|
|
|
2011-10-26 12:19:18 +00: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']
|
|
|
|
|
2011-09-20 18:16:49 +00:00
|
|
|
site.register(Child, DynamicListDisplayChildAdmin)
|
2011-11-17 21:30:07 +00:00
|
|
|
|
2013-09-06 18:25:13 +00:00
|
|
|
|
|
|
|
class NoListDisplayLinksParentAdmin(admin.ModelAdmin):
|
|
|
|
list_display_links = None
|
|
|
|
|
|
|
|
site.register(Parent, NoListDisplayLinksParentAdmin)
|
|
|
|
|
|
|
|
|
2011-11-17 21:30:07 +00:00
|
|
|
class SwallowAdmin(admin.ModelAdmin):
|
2013-11-02 21:02:56 +00:00
|
|
|
actions = None # prevent ['action_checkbox'] + list(list_display)
|
2011-11-17 21:30:07 +00:00
|
|
|
list_display = ('origin', 'load', 'speed')
|
|
|
|
|
|
|
|
site.register(Swallow, SwallowAdmin)
|
2012-11-25 19:39:23 +00:00
|
|
|
|
2013-09-06 18:25:13 +00:00
|
|
|
|
2012-11-25 19:39:23 +00:00
|
|
|
class DynamicListFilterChildAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ('parent', 'name', 'age')
|
|
|
|
|
|
|
|
def get_list_filter(self, request):
|
|
|
|
my_list_filter = super(DynamicListFilterChildAdmin, self).get_list_filter(request)
|
|
|
|
if request.user.username == 'noparents':
|
|
|
|
my_list_filter = list(my_list_filter)
|
|
|
|
my_list_filter.remove('parent')
|
|
|
|
return my_list_filter
|
|
|
|
|
2013-09-06 18:25:13 +00:00
|
|
|
|
2013-08-03 16:15:15 +00:00
|
|
|
class DynamicSearchFieldsChildAdmin(admin.ModelAdmin):
|
|
|
|
search_fields = ('name',)
|
|
|
|
|
|
|
|
def get_search_fields(self, request):
|
|
|
|
search_fields = super(DynamicSearchFieldsChildAdmin, self).get_search_fields(request)
|
|
|
|
search_fields += ('age',)
|
|
|
|
return search_fields
|