Files
FireworksApp/fireworks_detect/admin.py
T
2026-09-26 20:59:01 +02:00

91 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import json
from django.contrib import admin, messages
from django.utils import timezone
from django.utils.html import format_html
from .models import Event, PendingEvent, Sensor
class ClassificationAdmin(admin.ModelAdmin):
list_display = ("original_filename", "sensor", "primary_label", "primary_score", "duration", "created_at")
list_filter = ("created_at",)
search_fields = ("original_filename",)
readonly_fields = ("audio_file", "sensor", "original_filename", "created_at", "pretty_classifier_response")
fields = ("audio_file", "sensor", "original_filename", "created_at")
change_form_template = "admin/fireworks_detect/classification_audio_change_form.html"
@admin.display(description="Classifier response")
def pretty_classifier_response(self, obj):
return format_html("<pre>{}</pre>", json.dumps(obj.classifier_response, indent=2, sort_keys=True))
@admin.register(PendingEvent)
class PendingEventAdmin(ClassificationAdmin):
actions = ("approve_selected", "deny_selected")
def has_add_permission(self, request):
return False
@admin.action(description="Approve selected samples and move them to Events")
def approve_selected(self, request, queryset):
approved = 0
for pending in queryset:
pending.approve()
approved += 1
self.message_user(request, f"{approved} sample(s) approved and moved to Events.", messages.SUCCESS)
@admin.action(description="Deny selected samples and delete their uploaded audio")
def deny_selected(self, request, queryset):
denied = 0
for pending in queryset:
pending.deny()
denied += 1
self.message_user(request, f"{denied} sample(s) denied and removed.", messages.WARNING)
@admin.register(Event)
class EventAdmin(ClassificationAdmin):
readonly_fields = ClassificationAdmin.readonly_fields + ("approved_at",)
fields = ClassificationAdmin.fields + ("approved_at",)
def has_add_permission(self, request):
return False
@admin.register(Sensor)
class SensorAdmin(admin.ModelAdmin):
list_display = ("name", "is_active", "heartbeat_light", "last_heartbeat", "last_submission", "token_prefix")
list_filter = ("is_active",)
search_fields = ("name", "token_prefix")
readonly_fields = ("token_prefix", "last_heartbeat", "last_submission", "created_at")
actions = ("issue_api_token",)
fieldsets = (
("Sensor", {"fields": ("name", "is_active", "created_at")}),
("API token", {"fields": ("token_prefix",), "description": "Save the sensor first, select it in the list, then choose ‘Issue or rotate API token’. The complete token is shown once only; its salted hash is the only value stored."}),
("Heartbeat", {"fields": ("last_heartbeat", "last_submission"), "description": "Devices should POST to /fireworks/heartbeat/ with Authorization: Bearer <token>. Green means seen within 5 minutes; amber within 15; red is older or never seen."}),
)
@admin.display(description="Heartbeat")
def heartbeat_light(self, obj):
if not obj.last_heartbeat:
colour, label = "#ef4444", "Never seen"
else:
age = timezone.now() - obj.last_heartbeat
if age.total_seconds() <= 300:
colour, label = "#22c55e", "Online"
elif age.total_seconds() <= 900:
colour, label = "#f59e0b", "Delayed"
else:
colour, label = "#ef4444", "Offline"
return format_html('<span style="display:inline-flex;align-items:center;gap:6px"><span style="width:10px;height:10px;border-radius:50%;background:{}"></span>{}</span>', colour, label)
@admin.action(description="Issue or rotate API token (shown once)")
def issue_api_token(self, request, queryset):
if queryset.count() != 1:
self.message_user(request, "Select exactly one sensor to issue a token.", messages.ERROR)
return
sensor = queryset.get()
token = sensor.issue_token()
self.message_user(request, format_html('Copy this token now; it is not stored and will not be shown again: <code>{}</code>', token), messages.WARNING)