devicehub-django/admin/forms.py

42 lines
1.3 KiB
Python
Raw Permalink Normal View History

2024-10-31 04:56:27 +00:00
from django import forms
from utils.device import create_annotation, create_doc, create_index
from utils.save_snapshots import move_json, save_in_disk
2024-11-01 07:36:24 +00:00
from django.forms.formsets import BaseFormSet
2024-10-31 04:56:27 +00:00
from django.forms import formset_factory
2024-11-01 07:36:24 +00:00
from django.core.exceptions import ValidationError
2024-10-31 04:56:27 +00:00
class CustomStatusLabelForm(forms.Form):
2024-11-01 07:36:24 +00:00
label_name = forms.CharField(
2024-10-31 04:56:27 +00:00
label="Annotation Name",
max_length=50,
widget=forms.TextInput(attrs={'class': 'form-control'})
)
2024-11-01 07:36:24 +00:00
class CustomStatusValueForm(forms.Form):
label_state = forms.CharField(
2024-10-31 04:56:27 +00:00
label="Possible State",
max_length=50,
widget=forms.TextInput(attrs={'class': 'form-control'})
)
2024-11-01 07:36:24 +00:00
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)
2024-10-31 04:56:27 +00:00
2024-11-01 07:36:24 +00:00
CustomStatusFormSet = formset_factory(CustomStatusValueForm ,formset = CustomStatusValueFormSet)