devicehub-django/admin/forms.py

42 lines
1.3 KiB
Python

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):
label_name = forms.CharField(
label="Annotation Name",
max_length=50,
widget=forms.TextInput(attrs={'class': 'form-control'})
)
class CustomStatusValueForm(forms.Form):
label_state = forms.CharField(
label="Possible State",
max_length=50,
widget=forms.TextInput(attrs={'class': 'form-control'})
)
class CustomStatusValueFormSet(BaseFormSet):
"""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)