action view overhaul
This commit is contained in:
parent
3a93595396
commit
b1bf33ac83
|
@ -1,20 +1,41 @@
|
|||
from django import forms
|
||||
from utils.device import create_annotation, create_doc, create_index
|
||||
from utils.save_snapshots import move_json, save_in_disk
|
||||
|
||||
from django.forms.formsets import BaseFormSet
|
||||
from django.forms import formset_factory
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
class CustomStatusLabelForm(forms.Form):
|
||||
annotation_name = forms.CharField(
|
||||
label_name = forms.CharField(
|
||||
label="Annotation Name",
|
||||
max_length=50,
|
||||
widget=forms.TextInput(attrs={'class': 'form-control'})
|
||||
)
|
||||
annotation_state = forms.CharField(
|
||||
|
||||
|
||||
class CustomStatusValueForm(forms.Form):
|
||||
label_state = forms.CharField(
|
||||
label="Possible State",
|
||||
max_length=50,
|
||||
widget=forms.TextInput(attrs={'class': 'form-control'})
|
||||
)
|
||||
|
||||
class CustomStatusValueFormSet(BaseFormSet):
|
||||
|
||||
CustomStatusLabelFormSet = formset_factory(CustomStatusLabelForm, extra=1)
|
||||
"""Validation for inputs (no two values should be the same)"""
|
||||
def clean(self):
|
||||
if any(self.errors):
|
||||
return
|
||||
|
||||
label = []
|
||||
labels = []
|
||||
for form in self.forms:
|
||||
label = form.cleaned_data.get('label_state')
|
||||
if label:
|
||||
if label in labels:
|
||||
raise ValidationError("Duplicate labels are not allowed.")
|
||||
|
||||
labels.append(label)
|
||||
|
||||
|
||||
CustomStatusFormSet = formset_factory(CustomStatusValueForm ,formset = CustomStatusValueFormSet)
|
||||
|
|
|
@ -13,8 +13,9 @@
|
|||
<form role="form" method="post" novalidate>
|
||||
{% csrf_token %}
|
||||
<div class="mb-3">
|
||||
{{ form.annotation_name.label_tag }}
|
||||
{{ form.annotation_name }}
|
||||
|
||||
<h5 class="mt-4">{% translate "Status name" %}</h5>
|
||||
{% bootstrap_field form.label_name show_label=False %}
|
||||
</div>
|
||||
|
||||
<h5 class="mt-4">{% translate "Possible States" %}</h5>
|
||||
|
@ -23,7 +24,7 @@
|
|||
{% for form in formset %}
|
||||
<div class="row mb-3 formset-form">
|
||||
<div class="col-md-10">
|
||||
{% bootstrap_field form.annotation_state show_label=False %}
|
||||
{% bootstrap_field form.label_state show_label=False %}
|
||||
</div>
|
||||
<div class="col-md-2 d-flex align-items-center">
|
||||
{% if forloop.first %}
|
||||
|
@ -48,7 +49,7 @@
|
|||
</div>
|
||||
|
||||
<script>
|
||||
//fix this stub chatgpt code
|
||||
//TODO: change this to jquery formset plugin
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
var formsetDiv = document.getElementById('formset');
|
||||
var totalForms = document.getElementById('id_form-TOTAL_FORMS');
|
||||
|
|
|
@ -13,8 +13,9 @@ import logging
|
|||
from dashboard.mixins import DashboardView, Http403
|
||||
from user.models import User, Institution
|
||||
from admin.email import NotifyActivateUserByEmail
|
||||
from admin.forms import CustomStatusLabelForm, CustomStatusLabelFormSet
|
||||
from evidence.models import Annotation
|
||||
from admin.forms import CustomStatusLabelForm, CustomStatusValueForm, CustomStatusFormSet
|
||||
from evidence.models import Annotation, AllowedValue
|
||||
import uuid
|
||||
|
||||
logger = logging.getLogger('dhub')
|
||||
|
||||
|
@ -138,38 +139,62 @@ class AddReservedAnnotationView(AdminView, FormView):
|
|||
title = _("New Custom State Labels")
|
||||
breadcrumb = "Admin / Custom State Labels (new name?)"
|
||||
success_url = reverse_lazy('admin:panel')
|
||||
model = Annotation
|
||||
|
||||
|
||||
form_class = CustomStatusLabelForm
|
||||
formset_class = CustomStatusLabelFormSet
|
||||
formset_class = CustomStatusFormSet
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
if self.request.POST:
|
||||
context['form'] = self.form_class(self.request.POST)
|
||||
context['formset'] = self.formset_class(self.request.POST)
|
||||
else:
|
||||
context['form'] = self.form_class()
|
||||
context['formset'] = self.formset_class()
|
||||
|
||||
context['subtitle'] = _("Add Custom Status Label")
|
||||
return context
|
||||
|
||||
def form_valid(self, form):
|
||||
context = self.get_context_data()
|
||||
formset = context['formset']
|
||||
form = context['form']
|
||||
|
||||
if form.is_valid():
|
||||
label_name = form.cleaned_data['label_name']
|
||||
else:
|
||||
return self.form_invalid(form)
|
||||
|
||||
if formset.is_valid():
|
||||
annotation_name = form.cleaned_data['annotation_name']
|
||||
|
||||
logger.info("Saving to db: " + annotation_name)
|
||||
annotation = Annotation.objects.create(name=annotation_name)
|
||||
annotation = Annotation.objects.create(
|
||||
uuid=uuid.uuid4(),
|
||||
owner=self.request.user.institution,
|
||||
user=self.request.user,
|
||||
type=Annotation.Type.ADMIN,
|
||||
key=label_name,
|
||||
value=label_name
|
||||
)
|
||||
first_state = None
|
||||
for form in formset:
|
||||
state = form.cleaned_data.get('annotation_state')
|
||||
state = form.cleaned_data.get('label_state')
|
||||
if state and not first_state:
|
||||
first_state = state
|
||||
annotation.value = state
|
||||
annotation.save()
|
||||
if state:
|
||||
PossibleValue.objects.create(annotation=annotation, value=state)
|
||||
AllowedValue.objects.create(
|
||||
annotation=annotation,
|
||||
value=state
|
||||
)
|
||||
|
||||
logger.info("Saved to db: " + annotation_name)
|
||||
logger.info("Saving custom label to db: " + label_name)
|
||||
self.success_message = _("Custom status label has been added.")
|
||||
self.success_url = reverse_lazy('admin:panel')
|
||||
return super().form_valid(form)
|
||||
else:
|
||||
logger.error("Formset is not valid")
|
||||
logger.error(formset.errors)
|
||||
return self.form_invalid(form)
|
||||
return self.form_invalid(form)
|
||||
|
|
Loading…
Reference in New Issue