Skip to content

Commit

Permalink
Add form to set fields for fake incidents in admin
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Feb 14, 2024
1 parent ad042a0 commit 30ede8b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.d/669.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add possibility to set fields when creating fake incidents in Django admin
33 changes: 29 additions & 4 deletions src/argus/incident/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
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

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,
Expand Down Expand Up @@ -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"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{% extends "admin/base_site.html" %}
{% load i18n admin_urls static admin_modify %}

{% block extrahead %}{{ block.super }}
<script src="{% url 'admin:jsi18n' %}"></script>
{% endblock %}

{% block extrastyle %}{{ block.super }}<link rel="stylesheet" href="{% static "admin/css/forms.css" %}">{% endblock %}

{% block coltype %}colM{% endblock %}

{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-form{% endblock %}

{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'admin:index' %}">{% translate 'Home' %}</a>
&rsaquo; <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a>
&rsaquo; <a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a>
&rsaquo; {% blocktranslate with name=opts.verbose_name %}Add fake incident{% endblocktranslate %}
</div>
{% endblock %}

{% block content %}<div id="content-main">
<form method="post" id="{{ opts.model_name }}_form" novalidate>{% csrf_token %}{% block form_top %}{% endblock %}
<div>
{% if errors %}
<p class="errornote">
{% blocktranslate count counter=errors|length %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktranslate %}
</p>
{{ 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 %}
<div class="submit-row"><input type="submit" value="{% translate 'Save' %}" class="default" name="_save"></div>
{% endblock %}

{% block admin_change_form_document_ready %}
<script id="django-admin-form-add-constants"
src="{% static 'admin/js/change_form.js' %}"
data-model-name="{{ opts.model_name }}"
async>
</script>
{% endblock %}

{# JavaScript for prepopulated fields #}
{% prepopulated_fields_js %}

</div>
</form></div>
{% endblock %}

0 comments on commit 30ede8b

Please sign in to comment.