Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ogc 1160 formcode warning for empty fieldsets #1444

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/onegov/form/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def __init__(self, line: int):
self.line = line


class EmptyFieldsetError(FormError):
def __init__(self, field_name: str):
self.field_name = field_name


class FieldCompileError(FormError):
def __init__(self, field_name: str):
self.field_name = field_name
Expand Down
6 changes: 6 additions & 0 deletions src/onegov/form/locale/de_ch/LC_MESSAGES/onegov.form.po
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ msgstr ""
"Ein Minimalpreis kann nur gesetzt werden, wenn mindestens ein "
"kostenpflichtiges Feld definiert ist."

#, python-format
msgid "The '{label}' group is empty and will not be visible. Either "
"remove the empty group or add fields to it."
msgstr "Die Gruppe '{label}' ist leer und wird nicht sichtbar sein. "
"Entweder entfernen Sie die leere Gruppe oder fügen Felder hinzu."

#, python-format
msgid ""
"Invalid field type for field '{label}'. For filters only 'select' or "
Expand Down
6 changes: 6 additions & 0 deletions src/onegov/form/locale/fr_CH/LC_MESSAGES/onegov.form.po
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ msgstr ""
"Un total de prix minimum ne peut être paramétré que si au moins un champ "
"payant est défini."

#, python-format
msgid "The '{label}' group is empty and will not be visible. Either "
"remove the empty group or add fields to it."
msgstr "Le groupe '{label}' est vide et ne sera pas visible. Supprimez le "
"groupe vide ou ajoutez des champs."

#, python-format
msgid ""
"Invalid field type for field '{label}'. For filters only 'select' or "
Expand Down
7 changes: 7 additions & 0 deletions src/onegov/form/locale/it_CH/LC_MESSAGES/onegov.form.po
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ msgstr ""
"È possibile impostare un prezzo minimo totale solo se è definito almeno un "
"campo prezzo."

#, python-format
msgid "The '{label}' group is empty and will not be visible. Either "
"remove the empty group or add fields to it."
msgstr ""
"Il gruppo '{label}' è vuoto e non sarà visibile. Rimuovere il gruppo vuoto "
"o aggiungere campi."

#, python-format
msgid ""
"Invalid field type for field '{label}'. For filters only 'select' or "
Expand Down
2 changes: 2 additions & 0 deletions src/onegov/form/parser/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,8 @@ def parse_formcode(
parse_field_block(block, field_classes, used_ids, fs)
for block in (fieldset[label] or ())
]
if not fs.fields:
raise errors.EmptyFieldsetError(label)
Tschuppi81 marked this conversation as resolved.
Show resolved Hide resolved

fieldsets.append(fs)

Expand Down
11 changes: 10 additions & 1 deletion src/onegov/form/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from dateutil.relativedelta import relativedelta
from mimetypes import types_map
from onegov.form import _
from onegov.form.errors import DuplicateLabelError, InvalidIndentSyntax
from onegov.form.errors import (DuplicateLabelError, InvalidIndentSyntax,
EmptyFieldsetError)
from onegov.form.errors import FieldCompileError
from onegov.form.errors import InvalidFormSyntax
from onegov.form.errors import MixedTypeError
Expand Down Expand Up @@ -189,6 +190,9 @@
"A minimum price total can only be set if at least one priced field "
"is defined."
)
empty_fieldset = _(
"The '{label}' group is empty and will not be visible. Either remove "
"the empty group or add fields to it.")

def __init__(
self,
Expand Down Expand Up @@ -218,6 +222,11 @@
raise ValidationError(
field.gettext(self.indent).format(line=exception.line)
) from exception
except EmptyFieldsetError as exception:
raise ValidationError(

Check warning on line 226 in src/onegov/form/validators.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/form/validators.py#L226

Added line #L226 was not covered by tests
field.gettext(self.empty_fieldset).format(
label=exception.field_name)
) from exception
except DuplicateLabelError as exception:
raise ValidationError(
field.gettext(self.duplicate).format(label=exception.label)
Expand Down
39 changes: 38 additions & 1 deletion tests/onegov/form/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ def test_normalization():
# no indent check while parsing
('', False, False),
])
def test_indentation_error_while_parsing(indent, indent_check, shall_raise):
def test_indentation_error(indent, indent_check, shall_raise):
# wrong indent see 'Telefonnummer'
text = dedent(
"""
Expand All @@ -1077,3 +1077,40 @@ def test_indentation_error_while_parsing(indent, indent_check, shall_raise):
parse_formcode(text, enable_indent_check=indent_check)
except InvalidIndentSyntax as e:
pytest.fail('Unexpected exception {}'.format(type(e).__name__))


def test_empty_fieldset_error():
with pytest.raises(errors.EmptyFieldsetError) as e:
parse_form('\n'.join((
"# Section 1",
"# Section 2",
"First Name *= ___",
"Last Name *= ___",
"E-mail *= @@@"
)))

assert e.value.field_name == 'Section 1'

with pytest.raises(errors.EmptyFieldsetError) as e:
parse_form('\n'.join((
"# Section 1",
"First Name *= ___",
"Last Name *= ___",
"# Section 2",
"E-mail *= @@@",
"# Section 3",
)))

assert e.value.field_name == 'Section 3'

with pytest.raises(errors.EmptyFieldsetError) as e:
parse_form('\n'.join((
"# Section 1",
"First Name *= ___",
"Last Name *= ___",
"# Section 2",
"# Section 3",
"E-mail *= @@@",
)))

assert e.value.field_name == 'Section 2'