mobile menu added
This commit is contained in:
@@ -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(
|
||||
"<path:object_id>/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
|
||||
|
||||
Reference in New Issue
Block a user