cmsmanage/doorcontrol/admin.py

67 lines
1.8 KiB
Python
Raw Normal View History

from django.contrib import admin
from django_object_actions import DjangoObjectActions, action
from .models import Door, HIDEvent
from .tasks.scrapehidevents import q_getMessagesAllDoors
@admin.register(Door)
class DoorAdmin(admin.ModelAdmin):
pass
class IsRedFilter(admin.SimpleListFilter):
title = "Is Red"
parameter_name = "is_red"
def lookups(self, request, model_admin):
return (
("1", "Yes"),
("0", "No"),
)
def queryset(self, request, queryset):
if self.value() is None:
return queryset
else:
return queryset.filter(is_red=(self.value() == "1"))
@admin.register(HIDEvent)
class HIDEventAdmin(DjangoObjectActions, admin.ModelAdmin):
search_fields = ["forename", "surname", "cardholder_id"]
list_display = ["timestamp", "door", "event_type", "description", "is_red"]
list_filter = [
"timestamp",
"door",
"event_type",
IsRedFilter,
]
readonly_fields = ["decoded_card_number"]
changelist_actions = ("refresh_all_doors",)
def get_queryset(self, request):
return super().get_queryset(request).with_is_red().with_decoded_card_number()
@admin.display(boolean=True)
def is_red(self, obj):
return obj.is_red
def has_add_permission(self, request, obj=None):
return False
def has_change_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False
@action(label="Refresh All Doors")
def refresh_all_doors(self, request, obj):
q_getMessagesAllDoors()
self.message_user(
request,
"Queued refresh, please wait a few seconds/minutes then refresh the page",
)