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 @@