From 30ede8bcd6b29f861ab31b3f070d9a7f05bc1c6e Mon Sep 17 00:00:00 2001 From: Johanna England Date: Wed, 14 Feb 2024 11:55:39 +0100 Subject: [PATCH] Add form to set fields for fake incidents in admin --- changelog.d/669.added.md | 1 + src/argus/incident/admin.py | 33 ++++++++-- .../admin/fake_incident_add_form.html | 60 +++++++++++++++++++ 3 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 changelog.d/669.added.md create mode 100644 src/argus/incident/templates/incident/admin/fake_incident_add_form.html diff --git a/changelog.d/669.added.md b/changelog.d/669.added.md new file mode 100644 index 000000000..00930c204 --- /dev/null +++ b/changelog.d/669.added.md @@ -0,0 +1 @@ +Add possibility to set fields when creating fake incidents in Django admin \ No newline at end of file diff --git a/src/argus/incident/admin.py b/src/argus/incident/admin.py index a77ec03eb..d28a1fdb4 100644 --- a/src/argus/incident/admin.py +++ b/src/argus/incident/admin.py @@ -4,6 +4,7 @@ from django.contrib.admin import widgets as admin_widgets from django.db.models.functions import Concat from django.http import HttpResponseRedirect +from django.template.response import TemplateResponse from django.urls import path from django.utils.html import format_html_join, format_html from django.utils.safestring import mark_safe @@ -11,7 +12,7 @@ from argus.auth.models import User from argus.util.admin_utils import add_elements_to_deleted_objects, list_filter_factory from . import fields, widgets -from .forms import AddSourceSystemForm +from .forms import AddSourceSystemForm, FakeIncidentForm from .models import ( Acknowledgement, Event, @@ -185,11 +186,35 @@ def send_fake(self, request): def get_urls(self): orig_urls = super().get_urls() - urls = [ - path("fake/", self.send_fake), - ] + urls = [path("fake/", self.admin_site.admin_view(self.add_fake_incident), name="add-fake-incident")] return urls + orig_urls + def add_fake_incident(self, request): + request.current_app = self.admin_site.name + if request.method == "POST": + form = FakeIncidentForm(request.POST) + if form.is_valid(): + tags = form.cleaned_data.get("tags", None) + description = form.cleaned_data.get("description", None) + stateful = form.cleaned_data.get("stateful") + level = form.cleaned_data.get("level", None) + create_fake_incident(tags=tags, description=description, stateful=stateful, level=level) + return HttpResponseRedirect("../") + else: + form = FakeIncidentForm() + + fieldsets = [(None, {"fields": list(form.base_fields)})] + admin_form = admin.helpers.AdminForm(form, fieldsets, {}) + context = { + "title": "Add fake incident", + "subtitle": None, + "adminform": admin_form, + "opts": self.model._meta, + "errors": None, + **self.admin_site.each_context(request), + } + return TemplateResponse(request, "incident/admin/fake_incident_add_form.html", context) + def get_form(self, request, obj: Incident = None, **kwargs): form = super().get_form(request, obj, **kwargs) end_time_field = form.base_fields["end_time"] diff --git a/src/argus/incident/templates/incident/admin/fake_incident_add_form.html b/src/argus/incident/templates/incident/admin/fake_incident_add_form.html new file mode 100644 index 000000000..b3179efd1 --- /dev/null +++ b/src/argus/incident/templates/incident/admin/fake_incident_add_form.html @@ -0,0 +1,60 @@ +{% extends "admin/base_site.html" %} +{% load i18n admin_urls static admin_modify %} + +{% block extrahead %}{{ block.super }} + +{% endblock %} + +{% block extrastyle %}{{ block.super }}{% endblock %} + +{% block coltype %}colM{% endblock %} + +{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-form{% endblock %} + +{% block breadcrumbs %} + +{% endblock %} + +{% block content %}
+
{% csrf_token %}{% block form_top %}{% endblock %} +
+{% if errors %} +

+ {% blocktranslate count counter=errors|length %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktranslate %} +

+ {{ adminform.form.non_field_errors }} +{% endif %} + +{% block field_sets %} +{% for fieldset in adminform %} + {% include "admin/includes/fieldset.html" %} +{% endfor %} +{% endblock %} + +{% block after_field_sets %}{% endblock %} + +{% block after_related_objects %}{% endblock %} + +{% block submit_buttons_bottom %} +
+{% endblock %} + +{% block admin_change_form_document_ready %} + +{% endblock %} + +{# JavaScript for prepopulated fields #} +{% prepopulated_fields_js %} + +
+
+{% endblock %}