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

View File

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

View File

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