Fix evaluation of empty label_pattern

This commit is contained in:
Jeremy Stretch 2020-06-30 16:30:54 -04:00
parent 89ea34015d
commit 7fab929194
2 changed files with 10 additions and 35 deletions

View File

@ -139,14 +139,14 @@ class LabeledComponentForm(BootstrapMixin, forms.Form):
def clean(self): def clean(self):
# Validate that the number of components being created from both the name_pattern and label_pattern are equal # Validate that the number of components being created from both the name_pattern and label_pattern are equal
name_pattern_count = len(self.cleaned_data['name_pattern']) if self.cleaned_data['label_pattern']:
label_pattern_count = len(self.cleaned_data['label_pattern']) name_pattern_count = len(self.cleaned_data['name_pattern'])
if label_pattern_count and name_pattern_count != label_pattern_count: label_pattern_count = len(self.cleaned_data['label_pattern'])
raise forms.ValidationError({ if name_pattern_count != label_pattern_count:
'label_pattern': 'The provided name pattern will create {} components, however {} labels will ' raise forms.ValidationError({
'be generated. These counts must match.'.format( 'label_pattern': f'The provided name pattern will create {name_pattern_count} components, however '
name_pattern_count, label_pattern_count) f'{label_pattern_count} labels will be generated. These counts must match.'
}, code='label_pattern_mismatch') }, code='label_pattern_mismatch')
# #

View File

@ -529,8 +529,8 @@ class ExpandableNameField(forms.CharField):
""" """
def to_python(self, value): def to_python(self, value):
if value is None: if not value:
return list() return ''
if re.search(ALPHANUMERIC_EXPANSION_PATTERN, value): if re.search(ALPHANUMERIC_EXPANSION_PATTERN, value):
return list(expand_alphanumeric_pattern(value)) return list(expand_alphanumeric_pattern(value))
return [value] return [value]
@ -830,31 +830,6 @@ class ImportForm(BootstrapMixin, forms.Form):
}) })
class LabeledComponentForm(BootstrapMixin, forms.Form):
"""
Base form for adding label pattern validation to `Create` forms
"""
name_pattern = ExpandableNameField(
label='Name'
)
label_pattern = ExpandableNameField(
label='Label',
required=False
)
def clean(self):
# Validate that the number of components being created from both the name_pattern and label_pattern are equal
name_pattern_count = len(self.cleaned_data['name_pattern'])
label_pattern_count = len(self.cleaned_data['label_pattern'])
if label_pattern_count and name_pattern_count != label_pattern_count:
raise forms.ValidationError({
'label_pattern': 'The provided name pattern will create {} components, however {} labels will '
'be generated. These counts must match.'.format(
name_pattern_count, label_pattern_count)
}, code='label_pattern_mismatch')
class TableConfigForm(BootstrapMixin, forms.Form): class TableConfigForm(BootstrapMixin, forms.Form):
""" """
Form for configuring user's table preferences. Form for configuring user's table preferences.