sensors added to settings

This commit is contained in:
Joren de Graaf
2026-10-02 15:38:18 +02:00
parent 428caef29d
commit ab6cfc0bae
19 changed files with 360 additions and 19 deletions
+7 -16
View File
@@ -7,7 +7,6 @@ from django.core.exceptions import PermissionDenied
from django.http import HttpResponseNotAllowed
from django.shortcuts import get_object_or_404, redirect
from django.urls import path, reverse
from django.utils import timezone
from django.utils.html import format_html
from django.utils.translation import gettext as _, gettext_lazy, ngettext
@@ -124,29 +123,21 @@ class EventAdmin(ClassificationAdmin):
@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")
list_display = ("name", "owner", "is_active", "heartbeat_light", "last_heartbeat", "last_submission", "token_prefix", "archived_at")
list_filter = ("is_active", "owner", "archived_at")
search_fields = ("name", "token_prefix", "owner__username", "owner__email")
readonly_fields = ("token_prefix", "last_heartbeat", "last_submission", "created_at", "archived_at")
actions = ("issue_api_token",)
fieldsets = (
(gettext_lazy("Sensor"), {"fields": ("name", "is_active", "created_at")}),
(gettext_lazy("Sensor"), {"fields": ("name", "owner", "is_active", "created_at", "archived_at")}),
(gettext_lazy("API token"), {"fields": ("token_prefix",), "description": gettext_lazy("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.")}),
(gettext_lazy("Heartbeat"), {"fields": ("last_heartbeat", "last_submission"), "description": gettext_lazy("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=gettext_lazy("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")
status, label = obj.heartbeat_status()
colour = {"online": "#22c55e", "delayed": "#f59e0b"}.get(status, "#ef4444")
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=gettext_lazy("Issue or rotate API token (shown once)"))