cmsmanage/doorcontrol/admin.py

83 lines
2.1 KiB
Python
Raw Normal View History

from django.contrib import admin
from django_object_actions import DjangoObjectActions, action
from .forms import AttributeScheduleRuleForm, DoorAdminForm
from .models import (
ActiveEventInstructorRule,
AttributeScheduleRule,
Door,
FlagScheduleRule,
HIDEvent,
Schedule,
)
from .tasks.scrapehidevents import q_getMessagesAllDoors
class FlagScheduleRuleInline(admin.TabularInline):
model = FlagScheduleRule
autocomplete_fields = ["flag"]
extra = 0
class AttributeScheduleRuleInline(admin.TabularInline):
model = AttributeScheduleRule
form = AttributeScheduleRuleForm
extra = 0
class ActiveEventInstructorRuleInline(admin.TabularInline):
model = ActiveEventInstructorRule
extra = 0
@admin.register(Schedule)
class ScheduleAdmin(admin.ModelAdmin):
inlines = [
FlagScheduleRuleInline,
AttributeScheduleRuleInline,
ActiveEventInstructorRuleInline,
]
@admin.register(Door)
class DoorAdmin(admin.ModelAdmin):
form = DoorAdminForm
list_display = ["name", "access_field"]
@admin.register(HIDEvent)
class HIDEventAdmin(DjangoObjectActions, admin.ModelAdmin):
search_fields = ["forename", "surname", "cardholder_id"]
date_hierarchy = "timestamp"
list_display = ["timestamp", "door", "event_type", "description", "_is_red"]
list_filter = [
"timestamp",
"door",
"event_type",
"is_red",
]
readonly_fields = ["decoded_card_number"]
changelist_actions = ("refresh_all_doors",)
@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",
)