diff --git a/db.sqlite3 b/db.sqlite3 index 230084d..c17f67e 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/fireworks_detect/__pycache__/admin.cpython-313.pyc b/fireworks_detect/__pycache__/admin.cpython-313.pyc index 9e1ed32..477b8be 100644 Binary files a/fireworks_detect/__pycache__/admin.cpython-313.pyc and b/fireworks_detect/__pycache__/admin.cpython-313.pyc differ diff --git a/fireworks_detect/__pycache__/tests.cpython-313.pyc b/fireworks_detect/__pycache__/tests.cpython-313.pyc index 460e069..65ad8bf 100644 Binary files a/fireworks_detect/__pycache__/tests.cpython-313.pyc and b/fireworks_detect/__pycache__/tests.cpython-313.pyc differ diff --git a/fireworks_detect/__pycache__/views.cpython-313.pyc b/fireworks_detect/__pycache__/views.cpython-313.pyc index 63c242c..b19e98a 100644 Binary files a/fireworks_detect/__pycache__/views.cpython-313.pyc and b/fireworks_detect/__pycache__/views.cpython-313.pyc differ diff --git a/fireworks_detect/admin.py b/fireworks_detect/admin.py index 3c63427..de2be82 100644 --- a/fireworks_detect/admin.py +++ b/fireworks_detect/admin.py @@ -1,11 +1,18 @@ import json +from urllib.error import HTTPError, URLError +from urllib.request import Request, urlopen from django.contrib import admin, messages +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 from .models import ClassificationRule, Event, PendingEvent, Sensor +from . import views as classifier_views @admin.register(ClassificationRule) @@ -35,6 +42,56 @@ class ClassificationAdmin(admin.ModelAdmin): @admin.register(PendingEvent) class PendingEventAdmin(ClassificationAdmin): actions = ("approve_selected", "deny_selected") + change_form_template = "admin/fireworks_detect/pending_change_form.html" + + def get_urls(self): + custom_urls = [ + path( + "/classify/", + self.admin_site.admin_view(self.classify_record), + name="fireworks_detect_pendingevent_classify", + ), + ] + return custom_urls + super().get_urls() + + def classify_record(self, request, object_id): + if request.method != "POST": + return HttpResponseNotAllowed(["POST"]) + pending = get_object_or_404(PendingEvent, pk=object_id) + if not self.has_change_permission(request, pending): + raise PermissionDenied + change_url = reverse("admin:fireworks_detect_pendingevent_change", args=[pending.pk]) + if not pending.audio_file: + self.message_user(request, _("This Pending event has no attached audio to classify."), messages.ERROR) + return redirect(change_url) + try: + if pending.audio_file.size > classifier_views.MAX_AUDIO_SIZE: + self.message_user(request, _("The attached audio exceeds the 25 MiB limit."), messages.ERROR) + return redirect(change_url) + with pending.audio_file.open("rb") as audio: + body, content_type = classifier_views._encode_audio_multipart(audio) + classifier_request = Request( + classifier_views.CLASSIFIER_URL, + data=body, + headers={"Content-Type": content_type, "Accept": "application/json"}, + method="POST", + ) + with urlopen(classifier_request, timeout=60) as response: + payload = json.loads(response.read().decode("utf-8")) + if not isinstance(payload, dict): + raise ValueError("Classifier response must be a JSON object") + except HTTPError as error: + self.message_user(request, _("The classifier returned HTTP %(code)s.") % {"code": error.code}, messages.ERROR) + return redirect(change_url) + except (URLError, TimeoutError, OSError, UnicodeDecodeError, json.JSONDecodeError, ValueError): + self.message_user(request, _("The classifier could not be reached or returned invalid JSON."), messages.ERROR) + return redirect(change_url) + + previous_response = pending.classifier_response if isinstance(pending.classifier_response, dict) else {} + pending.classifier_response = {**previous_response, **payload} + pending.save(update_fields=["classifier_response"]) + self.message_user(request, _("Classification saved. This event remains Pending."), messages.SUCCESS) + return redirect(change_url) def has_add_permission(self, request): return False diff --git a/fireworks_detect/templates/fireworks_detect/includes/manual_report_form.html b/fireworks_detect/templates/fireworks_detect/includes/manual_report_form.html index f0574b2..16f9374 100644 --- a/fireworks_detect/templates/fireworks_detect/includes/manual_report_form.html +++ b/fireworks_detect/templates/fireworks_detect/includes/manual_report_form.html @@ -1,9 +1,9 @@ {% load i18n %}
-

{% trans "No recording needed" %}

+

{% trans "Recording optional" %}

{% trans "Report fireworks now" %}

{% trans "Choose how many times you heard fireworks, then send it straight to Pending. The report is timestamped now." %}

-
+ {% csrf_token %} @@ -18,6 +18,21 @@
+
+ + + +
diff --git a/fireworks_detect/templates/fireworks_detect/includes/navigation.html b/fireworks_detect/templates/fireworks_detect/includes/navigation.html index 3ddffda..04ff6ee 100644 --- a/fireworks_detect/templates/fireworks_detect/includes/navigation.html +++ b/fireworks_detect/templates/fireworks_detect/includes/navigation.html @@ -1,7 +1,8 @@ {% load i18n %}
+ ✦ Sound Signal -
+