shiftregister/shiftregister/app/admin.py

93 lines
2.4 KiB
Python

from django.contrib import admin
from django.contrib.admin import DateFieldListFilter
from .models import Helper, LoginToken, Message, Room, Shift, ShiftRegistration
@admin.register(Room)
class RoomAdmin(admin.ModelAdmin):
list_display = ("name", "description_length")
def description_length(self, object):
if object.description:
return len(object.description)
return 0
@admin.register(Shift)
class ShiftAdmin(admin.ModelAdmin):
list_display = ("room_name", "description", "start_at", "free_slots", "deleted")
def room_name(self, object):
return object.room.name
# def helpers(self, object):
# object.helpers.count()
def free_slots(self, object):
return (
object.required_helpers or object.room.required_helpers
) - object.registration_count()
def send_login(modeladmin, request, queryset):
for helper in queryset:
helper.send_confirmation()
class RegistrationInline(admin.TabularInline):
model = ShiftRegistration
@admin.register(Helper)
class HelperAdmin(admin.ModelAdmin):
readonly_fields = ("phone",)
fields = ("name", "phone", "number_validated")
list_display = ("name",)
inlines = (RegistrationInline,)
actions = (send_login,)
@admin.register(ShiftRegistration)
class ShiftRegistrationAdmin(admin.ModelAdmin):
list_display = ("start_at", "room", "helper_name", "state")
list_filter = (("shift__start_at", DateFieldListFilter), "shift__room", "state")
def room(self, obj):
return obj.shift.room.name
def start_at(self, obj):
return obj.shift.start_at
start_at.admin_order_field = "shift__start_at"
def helper_name(self, obj):
return obj.helper.name
class WasSentListFilter(admin.SimpleListFilter):
parameter_name = "sent"
title = "sent status"
def lookups(self, request, model_admin):
return [("0", "Not sent"), ("1", "Sent")]
def queryset(self, request, queryset):
if self.value() == "1":
return queryset.exclude(sent_at=None)
elif self.value() == "0":
return queryset.filter(sent_at=None)
@admin.register(Message)
class MessageAdmin(admin.ModelAdmin):
list_display = ("__str__", "was_sent")
list_filter = (WasSentListFilter,)
@admin.display(boolean=True)
def was_sent(self, obj):
return obj.sent_at is not None
admin.site.register(LoginToken)