Move filter out of class definition and change name of form field
This commit is contained in:
parent
ab9b62f54b
commit
9037c44498
@ -1,4 +1,4 @@
|
|||||||
from .forms import BaseForm
|
from .forms import BaseForm, remove_empty_string
|
||||||
from wtforms.fields import StringField, TextAreaField, FieldList
|
from wtforms.fields import StringField, TextAreaField, FieldList
|
||||||
from wtforms.validators import Required, Optional
|
from wtforms.validators import Required, Optional
|
||||||
from atst.forms.validators import ListItemRequired, ListItemsUnique
|
from atst.forms.validators import ListItemRequired, ListItemsUnique
|
||||||
@ -9,7 +9,7 @@ class EditEnvironmentForm(BaseForm):
|
|||||||
name = StringField(
|
name = StringField(
|
||||||
label=translate("forms.environments.name_label"),
|
label=translate("forms.environments.name_label"),
|
||||||
validators=[Required()],
|
validators=[Required()],
|
||||||
filters=[BaseForm.remove_empty_string],
|
filters=[remove_empty_string],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -17,12 +17,12 @@ class NameAndDescriptionForm(BaseForm):
|
|||||||
name = StringField(
|
name = StringField(
|
||||||
label=translate("forms.application.name_label"),
|
label=translate("forms.application.name_label"),
|
||||||
validators=[Required()],
|
validators=[Required()],
|
||||||
filters=[BaseForm.remove_empty_string],
|
filters=[remove_empty_string],
|
||||||
)
|
)
|
||||||
description = TextAreaField(
|
description = TextAreaField(
|
||||||
label=translate("forms.application.description_label"),
|
label=translate("forms.application.description_label"),
|
||||||
validators=[Optional()],
|
validators=[Optional()],
|
||||||
filters=[BaseForm.remove_empty_string],
|
filters=[remove_empty_string],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ class EnvironmentsForm(BaseForm):
|
|||||||
environment_names = FieldList(
|
environment_names = FieldList(
|
||||||
StringField(
|
StringField(
|
||||||
label=translate("forms.application.environment_names_label"),
|
label=translate("forms.application.environment_names_label"),
|
||||||
filters=[BaseForm.remove_empty_string],
|
filters=[remove_empty_string],
|
||||||
),
|
),
|
||||||
validators=[
|
validators=[
|
||||||
ListItemRequired(
|
ListItemRequired(
|
||||||
|
@ -7,6 +7,14 @@ from atst.utils.flash import formatted_flash as flash
|
|||||||
EMPTY_LIST_FIELD = ["", None]
|
EMPTY_LIST_FIELD = ["", None]
|
||||||
|
|
||||||
|
|
||||||
|
def remove_empty_string(value):
|
||||||
|
# only return strings that contain non whitespace characters
|
||||||
|
if value and re.search(r"\S", value):
|
||||||
|
return value.strip()
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
class BaseForm(FlaskForm):
|
class BaseForm(FlaskForm):
|
||||||
def __init__(self, formdata=None, **kwargs):
|
def __init__(self, formdata=None, **kwargs):
|
||||||
# initialize the form with data from the cache
|
# initialize the form with data from the cache
|
||||||
@ -36,11 +44,3 @@ class BaseForm(FlaskForm):
|
|||||||
if not valid and flash_invalid:
|
if not valid and flash_invalid:
|
||||||
flash("form_errors")
|
flash("form_errors")
|
||||||
return valid
|
return valid
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def remove_empty_string(cls, value):
|
|
||||||
# only return strings that contain non whitespace characters
|
|
||||||
if value and re.search(r"\S", value):
|
|
||||||
return value.strip()
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
@ -8,7 +8,7 @@ from wtforms.fields import (
|
|||||||
from wtforms.validators import Length, Optional
|
from wtforms.validators import Length, Optional
|
||||||
from wtforms.widgets import ListWidget, CheckboxInput
|
from wtforms.widgets import ListWidget, CheckboxInput
|
||||||
|
|
||||||
from .forms import BaseForm
|
from .forms import BaseForm, remove_empty_string
|
||||||
from atst.utils.localization import translate
|
from atst.utils.localization import translate
|
||||||
|
|
||||||
from .data import (
|
from .data import (
|
||||||
@ -49,7 +49,7 @@ class PortfolioCreationForm(BaseForm):
|
|||||||
translate("forms.task_order.defense_component_label"),
|
translate("forms.task_order.defense_component_label"),
|
||||||
choices=SERVICE_BRANCHES,
|
choices=SERVICE_BRANCHES,
|
||||||
default="",
|
default="",
|
||||||
filters=[BaseForm.remove_empty_string],
|
filters=[remove_empty_string],
|
||||||
)
|
)
|
||||||
|
|
||||||
description = TextAreaField(
|
description = TextAreaField(
|
||||||
@ -85,7 +85,7 @@ class PortfolioCreationForm(BaseForm):
|
|||||||
complexity_other = StringField(
|
complexity_other = StringField(
|
||||||
translate("forms.task_order.complexity_other_label"),
|
translate("forms.task_order.complexity_other_label"),
|
||||||
default=None,
|
default=None,
|
||||||
filters=[BaseForm.remove_empty_string],
|
filters=[remove_empty_string],
|
||||||
)
|
)
|
||||||
|
|
||||||
dev_team = SelectMultipleField(
|
dev_team = SelectMultipleField(
|
||||||
@ -100,7 +100,7 @@ class PortfolioCreationForm(BaseForm):
|
|||||||
dev_team_other = StringField(
|
dev_team_other = StringField(
|
||||||
translate("forms.task_order.dev_team_other_label"),
|
translate("forms.task_order.dev_team_other_label"),
|
||||||
default=None,
|
default=None,
|
||||||
filters=[BaseForm.remove_empty_string],
|
filters=[remove_empty_string],
|
||||||
)
|
)
|
||||||
|
|
||||||
team_experience = RadioField(
|
team_experience = RadioField(
|
||||||
|
@ -2,7 +2,7 @@ import pytest
|
|||||||
from wtforms.fields import RadioField, FieldList, StringField
|
from wtforms.fields import RadioField, FieldList, StringField
|
||||||
from werkzeug.datastructures import ImmutableMultiDict
|
from werkzeug.datastructures import ImmutableMultiDict
|
||||||
|
|
||||||
from atst.forms.forms import BaseForm
|
from atst.forms.forms import BaseForm, remove_empty_string
|
||||||
|
|
||||||
|
|
||||||
class FormWithChoices(BaseForm):
|
class FormWithChoices(BaseForm):
|
||||||
@ -17,8 +17,8 @@ class FormWithChoices(BaseForm):
|
|||||||
|
|
||||||
|
|
||||||
class FormWithList(BaseForm):
|
class FormWithList(BaseForm):
|
||||||
list = FieldList(
|
fancy_list = FieldList(
|
||||||
StringField("a very fancy list", filters=[BaseForm.remove_empty_string])
|
StringField("a very fancy list", filters=[remove_empty_string])
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -46,11 +46,11 @@ class TestBaseForm:
|
|||||||
[["testing", "", "QA"], ["testing", " ", "QA"], ["testing", None, "QA"]],
|
[["testing", "", "QA"], ["testing", " ", "QA"], ["testing", None, "QA"]],
|
||||||
)
|
)
|
||||||
def test_blank_list_items_removed(self, form_data):
|
def test_blank_list_items_removed(self, form_data):
|
||||||
form = FormWithList(list=form_data)
|
form = FormWithList(fancy_list=form_data)
|
||||||
assert form.validate(flash_invalid=False)
|
assert form.validate(flash_invalid=False)
|
||||||
assert not form.data == ["testing", "QA"]
|
assert not form.data == ["testing", "QA"]
|
||||||
|
|
||||||
def test_remove_empty_string_clips_whitespace(self):
|
def test_remove_empty_string_clips_whitespace(self):
|
||||||
form = FormWithList(list=[" QA", " testing "])
|
form = FormWithList(fancy_list=[" QA", " testing "])
|
||||||
assert form.validate(flash_invalid=False)
|
assert form.validate(flash_invalid=False)
|
||||||
assert form.list.data == ["QA", "testing"]
|
assert form.fancy_list.data == ["QA", "testing"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user