From b1bf33ac83d91aaabd5d5dee839de4d526e05ac4 Mon Sep 17 00:00:00 2001 From: Thomas Rusiecki Date: Fri, 1 Nov 2024 04:36:24 -0300 Subject: [PATCH] action view overhaul --- admin/forms.py | 29 ++++++++++++++++++--- admin/templates/reserved.html | 9 ++++--- admin/views.py | 47 +++++++++++++++++++++++++++-------- 3 files changed, 66 insertions(+), 19 deletions(-) diff --git a/admin/forms.py b/admin/forms.py index b1e2c88..a063b36 100644 --- a/admin/forms.py +++ b/admin/forms.py @@ -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) diff --git a/admin/templates/reserved.html b/admin/templates/reserved.html index d24a80c..0215fb7 100644 --- a/admin/templates/reserved.html +++ b/admin/templates/reserved.html @@ -13,8 +13,9 @@
{% csrf_token %}
- {{ form.annotation_name.label_tag }} - {{ form.annotation_name }} + +
{% translate "Status name" %}
+ {% bootstrap_field form.label_name show_label=False %}
{% translate "Possible States" %}
@@ -23,7 +24,7 @@ {% for form in formset %}
- {% bootstrap_field form.annotation_state show_label=False %} + {% bootstrap_field form.label_state show_label=False %}
{% if forloop.first %} @@ -48,7 +49,7 @@