Move valdiation of replicated field to form

This commit is contained in:
jeremystretch 2022-09-09 10:48:27 -04:00
parent 305701b387
commit 21fe202597
3 changed files with 20 additions and 23 deletions

View File

@ -42,22 +42,20 @@ class ComponentCreateForm(forms.Form):
help_text='Alphanumeric ranges are supported. (Must match the number of names being created.)'
)
# TODO: Incorporate this validation
# def clean(self):
# super().clean()
#
# # Validate that all patterned fields generate an equal number of values
# patterned_fields = [
# field_name for field_name in self.fields if field_name.endswith('_pattern')
# ]
# pattern_count = len(self.cleaned_data['name_pattern'])
# for field_name in patterned_fields:
# value_count = len(self.cleaned_data[field_name])
# if self.cleaned_data[field_name] and value_count != pattern_count:
# raise forms.ValidationError({
# field_name: f'The provided pattern specifies {value_count} values, but {pattern_count} are '
# f'expected.'
# }, code='label_pattern_mismatch')
replication_fields = ('name', 'label')
def clean(self):
super().clean()
# Validate that all patterned fields generate an equal number of values
pattern_count = len(self.cleaned_data[self.replication_fields[0]])
for field_name in self.replication_fields:
value_count = len(self.cleaned_data[field_name])
if self.cleaned_data[field_name] and value_count != pattern_count:
raise forms.ValidationError({
field_name: f'The provided pattern specifies {value_count} values, but {pattern_count} are '
f'expected.'
}, code='label_pattern_mismatch')
# class ModularComponentTemplateCreateForm(ComponentCreateForm):
@ -190,9 +188,10 @@ class ModuleBayTemplateCreateForm(ComponentCreateForm, model_forms.ModuleBayTemp
help_text='Alphanumeric ranges are supported. (Must match the number of names being created.)'
)
field_order = ('device_type', 'name', 'label')
replication_fields = ('name', 'label', 'position')
class Meta(model_forms.ModuleBayTemplateForm.Meta):
exclude = ('name', 'label')
exclude = ('name', 'label', 'position')
class InventoryItemTemplateCreateForm(ComponentCreateForm, model_forms.InventoryItemTemplateForm):
@ -309,9 +308,10 @@ class ModuleBayCreateForm(ComponentCreateForm, model_forms.ModuleBayForm):
help_text='Alphanumeric ranges are supported. (Must match the number of names being created.)'
)
field_order = ('device', 'name', 'label')
replication_fields = ('name', 'label', 'position')
class Meta(model_forms.ModuleBayForm.Meta):
exclude = ('name', 'label')
exclude = ('name', 'label', 'position')
class InventoryItemCreateForm(ComponentCreateForm, model_forms.InventoryItemForm):

View File

@ -1360,7 +1360,6 @@ class ModuleBayTemplateCreateView(generic.ComponentCreateView):
queryset = ModuleBayTemplate.objects.all()
form = forms.ModuleBayTemplateCreateForm
model_form = forms.ModuleBayTemplateForm
patterned_fields = ('name', 'label', 'position')
class ModuleBayTemplateEditView(generic.ObjectEditView):
@ -2314,7 +2313,6 @@ class ModuleBayCreateView(generic.ComponentCreateView):
queryset = ModuleBay.objects.all()
form = forms.ModuleBayCreateForm
model_form = forms.ModuleBayForm
patterned_fields = ('name', 'label', 'position')
class ModuleBayEditView(generic.ObjectEditView):

View File

@ -541,7 +541,6 @@ class ComponentCreateView(GetReturnURLMixin, BaseObjectView):
template_name = 'generic/object_edit.html'
form = None
model_form = None
patterned_fields = ('name', 'label')
def get_required_permission(self):
return get_permission_for_model(self.queryset.model, 'add')
@ -575,10 +574,10 @@ class ComponentCreateView(GetReturnURLMixin, BaseObjectView):
if form.is_valid():
new_components = []
data = deepcopy(request.POST)
pattern_count = len(form.cleaned_data[self.patterned_fields[0]])
pattern_count = len(form.cleaned_data[self.form.replication_fields[0]])
for i in range(pattern_count):
for field_name in self.patterned_fields:
for field_name in self.form.replication_fields:
if form.cleaned_data.get(field_name):
data[field_name] = form.cleaned_data[field_name][i]