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] delay = HoneyPotField()
[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] title = StringField( fieldset=_('Proposed changes'), label=_('Title'), )
[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 = StringField( fieldset=_('Proposed changes'), label=_('Phone') )
[docs] phone_direct = StringField( fieldset=_('Proposed changes'), label=_('Direct Phone Number or Mobile') )
[docs] email = EmailField( fieldset=_('Proposed changes'), label=_('E-Mail') )
[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] salutation = StringField( fieldset=_('Proposed changes'), label=_('Salutation') )
[docs] academic_title = StringField( fieldset=_('Proposed changes'), label=_('Academic Title') )
[docs] first_name = StringField( fieldset=_('Proposed changes'), label=_('First name') )
[docs] last_name = StringField( fieldset=_('Proposed changes'), label=_('Last name') )
[docs] function = StringField( fieldset=_('Proposed changes'), label=_('Function') )
[docs] email = EmailField( fieldset=_('Proposed changes'), label=_('E-Mail') )
[docs] phone = StringField( fieldset=_('Proposed changes'), label=_('Phone') )
[docs] phone_direct = StringField( fieldset=_('Proposed changes'), label=_('Direct Phone Number or Mobile') )
[docs] born = StringField( fieldset=_('Proposed changes'), label=_('Born') )
[docs] profession = StringField( fieldset=_('Proposed changes'), label=_('Profession') )
[docs] political_party = StringField( fieldset=_('Proposed changes'), label=_('Political Party') )
[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} )
[docs] class ApplyMutationForm(Form):
[docs] changes = MultiCheckboxField( label=_('Proposed changes'), choices=[] )
[docs] def on_request(self) -> None: def translate(name: str) -> str: return self.request.translate(self.model.labels.get(name, name)) self.changes.choices = [ (name, f'{translate(name)}: {value}') for name, value in self.model.changes.items() ]
[docs] def apply_model(self) -> None: self.changes.data = list(self.model.changes.keys())
[docs] def update_model(self) -> None: self.model.apply(self.changes.data)