Source code for org.forms.form_definition
from onegov.core.utils import normalize_for_url
from onegov.form import Form, merge_forms, FormDefinitionCollection
from onegov.form.validators import ValidFormDefinition, ValidSurveyDefinition
from onegov.org import _
from onegov.org.forms.fields import HtmlField
from onegov.org.forms.generic import PaymentForm
from wtforms.fields import StringField
from wtforms.fields import TextAreaField
from wtforms.validators import InputRequired
from typing import TYPE_CHECKING
[docs]
class FormDefinitionBaseForm(Form):
[docs]
lead = TextAreaField(
label=_('Lead'),
description=_('Describes what this form is about'),
render_kw={'rows': 4})
[docs]
group = StringField(
label=_('Group'),
description=_('Used to group the form in the overview'))
[docs]
definition = TextAreaField(
label=_('Definition'),
validators=[InputRequired(), ValidFormDefinition()],
render_kw={'rows': 32, 'data-editor': 'form'},
default='E-Mail *= @@@')
[docs]
pick_up = TextAreaField(
label=_('Pick-Up'),
description=_('Describes how this resource can be picked up. '
'This text is used on the ticket status page to '
'inform the user')
)
if TYPE_CHECKING:
# we help mypy understand merge_forms this way, eventually we should
# write a mypy plugin for merge_forms/move_fields, that does the same
# substitution
else:
class FormDefinitionForm(merge_forms(
FormDefinitionBaseForm,
PaymentForm
)):
pass
[docs]
class SurveyDefinitionForm(Form):
""" Form to create surveys. """
# This class is needed to hide forbidden fields from the form editor
[docs]
lead = TextAreaField(
label=_('Lead'),
description=_('Short description of the survey'),
render_kw={'rows': 4})
[docs]
group = StringField(
label=_('Group'),
description=_('Used to group the form in the overview'))
[docs]
definition = TextAreaField(
label=_('Definition'),
validators=[InputRequired(), ValidSurveyDefinition()],
render_kw={'rows': 32, 'data-editor': 'form'})
[docs]
class FormDefinitionUrlForm(Form):
[docs]
def ensure_correct_name(self) -> bool | None:
if not self.name.data:
return None
assert isinstance(self.name.errors, list)
if self.model.name == self.name.data:
self.name.errors.append(
_('Please fill out a new name')
)
return False
normalized_name = normalize_for_url(self.name.data)
if self.name.data != normalized_name:
self.name.errors.append(
_('Invalid name. A valid suggestion is: ${name}',
mapping={'name': normalized_name})
)
return False
other_entry = FormDefinitionCollection(self.request.session).by_name(
normalized_name)
if other_entry:
self.name.errors.append(_('An entry with the same name exists'))
return False
return None