Source code for agency.forms.mutation
from onegov.agency import _
from onegov.core.utils import ensure_scheme
from onegov.form import Form
from onegov.form.fields import HoneyPotField
from onegov.form.fields import MultiCheckboxField
from wtforms.fields import EmailField
from wtforms.fields import StringField
from wtforms.fields import TextAreaField
from wtforms.validators import Email
from wtforms.validators import InputRequired
from typing import Any
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from weakref import CallableProxyType
from wtforms import Field
[docs]
class MutationForm(Form):
[docs]
callout = _(
'This form can be used to report mutations to the data. '
'You can either leave us a message or directly suggest changes to '
'the corresponding fields.'
)
[docs]
submitter_email = EmailField(
fieldset=_('Your contact details'),
label=_('E-Mail'),
description='max.muster@example.org',
validators=[InputRequired(), Email()],
)
[docs]
submitter_message = TextAreaField(
fieldset=_('Your message'),
label=_('Message'),
render_kw={'rows': 8}
)
@property
[docs]
def proposal_fields(self) -> dict[str, 'CallableProxyType[Field]']:
for fieldset in self.fieldsets:
if fieldset.label == 'Proposed changes':
return fieldset.fields
return {}
@property
[docs]
def proposed_changes(self) -> dict[str, Any]:
return {
name: field.data
for name, field in self.proposal_fields.items()
if field.data
}
[docs]
def ensure_has_content(self) -> bool | None:
if (
not self.submitter_message.data
and not any(f.data for f in self.proposal_fields.values())
):
assert isinstance(self.submitter_message.errors, list)
self.submitter_message.errors.append(
_(
'Please enter a message or suggest some changes '
'using the fields below.'
)
)
return False
return None
[docs]
def on_request(self) -> None:
if self.model is None:
return
for name, field in self.proposal_fields.items():
field.description = getattr(self.model, name) # type:ignore
[docs]
class AgencyMutationForm(MutationForm):
""" Form to report a mutation of an organization. """
[docs]
location_address = TextAreaField(
fieldset=_('Proposed changes'),
label=_('Location address'),
render_kw={'rows': 2}
)
[docs]
location_code_city = StringField(
fieldset=_('Proposed changes'),
label=_('Location Code and City'),
)
[docs]
postal_address = TextAreaField(
fieldset=_('Proposed changes'),
label=_('Postal address'),
render_kw={'rows': 2}
)
[docs]
postal_code_city = StringField(
fieldset=_('Proposed changes'),
label=_('Postal Code and City'),
)
[docs]
phone_direct = StringField(
fieldset=_('Proposed changes'),
label=_('Direct Phone Number or Mobile')
)
[docs]
website = StringField(
fieldset=_('Proposed changes'),
label=_('Website'),
filters=(ensure_scheme, )
)
[docs]
opening_hours = TextAreaField(
fieldset=_('Proposed changes'),
label=_('Opening hours'),
render_kw={'rows': 3}
)
[docs]
class PersonMutationForm(MutationForm):
""" Form to report a mutation of a person. """
[docs]
phone_direct = StringField(
fieldset=_('Proposed changes'),
label=_('Direct Phone Number or Mobile')
)
[docs]
parliamentary_group = StringField(
fieldset=_('Proposed changes'),
label=_('Parliamentary Group')
)
[docs]
website = StringField(
fieldset=_('Proposed changes'),
label=_('Website'),
filters=(ensure_scheme, )
)
[docs]
website_2 = StringField(
fieldset=_('Proposed changes'),
label=_('Website 2'),
filters=(ensure_scheme, )
)
[docs]
location_address = TextAreaField(
fieldset=_('Proposed changes'),
label=_('Location address'),
render_kw={'rows': 2}
)
[docs]
location_code_city = StringField(
fieldset=_('Proposed changes'),
label=_('Location Code and City'),
)
[docs]
postal_address = TextAreaField(
fieldset=_('Proposed changes'),
label=_('Postal address'),
render_kw={'rows': 2}
)
[docs]
postal_code_city = StringField(
fieldset=_('Proposed changes'),
label=_('Postal Code and City'),
)
[docs]
notes = TextAreaField(
fieldset=_('Proposed changes'),
label=_('Notes'),
render_kw={'rows': 5}
)