Source code for agency.forms.agency
from cgi import FieldStorage
from io import BytesIO
from markupsafe import Markup
from wtforms import EmailField, TextAreaField
from onegov.agency import _
from onegov.agency.collections import ExtendedAgencyCollection
from onegov.agency.models import ExtendedAgency
from onegov.agency.utils import handle_empty_p_tags
from onegov.core.security import Private
from onegov.core.utils import linkify, ensure_scheme
from onegov.file.attachments import IMAGE_MAX_SIZE
from onegov.form import Form
from onegov.form.fields import ChosenSelectField, HtmlField
from onegov.form.fields import MultiCheckboxField
from onegov.form.fields import UploadField
from onegov.form.validators import (
ImageSizeLimit,
MIME_TYPES_IMAGE,
)
from onegov.gis import CoordinatesField
from sqlalchemy import func
from wtforms.fields import SelectChoice
from wtforms.fields import StringField
from wtforms.validators import InputRequired
from typing import cast
from typing import Any
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Callable, Collection
from webob.request import _FieldStorageWithFile
[docs]
class ExtendedAgencyForm(Form):
""" Form to edit agencies. """
[docs]
organigram = UploadField(
label=_('Organigram'),
validators=[
ImageSizeLimit(
max_bytes=1 * 1024 * 1024, max_dimensions=IMAGE_MAX_SIZE)
],
allowed_mimetypes=MIME_TYPES_IMAGE,
)
[docs]
coordinates = CoordinatesField(
label=_('Location'),
description=_(
'Search for the exact address to set a marker. The zoom of '
'the map will be saved as well.'
),
fieldset=_('Map'),
render_kw={'data-map-type': 'marker'},
)
[docs]
export_fields = MultiCheckboxField(
label=_('Fields to include for each membership'),
choices=[
SelectChoice(
'membership.title',
_('Membership: Title')
),
SelectChoice(
'membership.since',
_('Membership: Since')
),
SelectChoice(
'membership.addition',
_('Membership: Addition')
),
SelectChoice(
'person.title',
_('Person: Title')
),
SelectChoice(
'person.function',
_('Person: Function')
),
SelectChoice(
'person.last_name',
_('Person: Last Name')
),
SelectChoice(
'person.first_name',
_('Person: First Name')
),
SelectChoice(
'person.born',
_('Person: Born')
),
SelectChoice(
'person.academic_title',
_('Person: Academic Title')
),
SelectChoice(
'person.profession',
_('Person: Profession')
),
SelectChoice(
'person.location_address',
_('Person: Location address')
),
SelectChoice(
'person.location_code_city',
_('Person: Location Code and City')
),
SelectChoice(
'person.postal_address',
_('Person: Postal address')
),
SelectChoice(
'person.postal_code_city',
_('Person: Postal Code and City')
),
SelectChoice(
'person.political_party',
_('Person: Political Party')
),
SelectChoice(
'person.parliamentary_group',
_('Person: Parliamentary Group')
),
SelectChoice(
'person.phone',
_('Person: Phone')
),
SelectChoice(
'person.phone_direct',
_('Person: Direct Phone')
),
],
default=['membership.title', 'person.title'],
fieldset=_('PDF Export'),
render_kw={'class_': 'sortable-multi-checkbox'}
)
[docs]
def get_useful_data(self) -> dict[str, Any]: # type:ignore[override]
result = super().get_useful_data(exclude={'csrf_token', 'organigram'})
if self.organigram.data:
result['organigram_file'] = self.organigram.file
if self.portrait.data:
result['portrait'] = linkify(self.portrait.data)
return result
[docs]
def update_model(self, model: ExtendedAgency) -> None:
assert self.title.data is not None
model.title = self.title.data
model.portrait = handle_empty_p_tags(linkify(self.portrait.data))
model.location_address = self.location_address.data
model.location_code_city = self.location_code_city.data
model.postal_address = self.postal_address.data
model.postal_code_city = self.postal_code_city.data
model.phone = self.phone.data
model.phone_direct = self.phone_direct.data
model.email = self.email.data
model.website = self.website.data
model.opening_hours = self.opening_hours.data
model.export_fields = self.export_fields.data or []
if self.organigram.action == 'delete':
del model.organigram
elif self.organigram.action == 'replace' and self.organigram.data:
model.organigram_file = self.organigram.file # type:ignore
model.coordinates = self.coordinates.data
if hasattr(self, 'access'):
model.access = self.access.data
if hasattr(self, 'publication_start'):
model.publication_start = self.publication_start.data
if hasattr(self, 'publication_end'):
model.publication_end = self.publication_end.data
[docs]
def reorder_export_fields(self) -> None:
chosen = self.export_fields.data or []
# sort the chosen values in the same order they were chosen in
# the rest of the values go at the end
def sort_key(choice: SelectChoice) -> float:
for idx, c in enumerate(chosen):
if c == choice.value:
return idx
return float('Inf')
self.export_fields.choices = sorted(
self.export_fields.choices,
key=sort_key,
)
[docs]
def apply_model(self, model: ExtendedAgency) -> None:
self.title.data = model.title
self.portrait.data = model.portrait or Markup('')
self.location_address.data = model.location_address
self.location_code_city.data = model.location_code_city
self.postal_address.data = model.postal_address
self.postal_code_city.data = model.postal_code_city
self.phone.data = model.phone
self.phone_direct.data = model.phone_direct
self.email.data = model.email
self.website.data = model.website
self.opening_hours.data = model.opening_hours
self.export_fields.data = model.export_fields
if model.organigram_file:
fs = FieldStorage()
fs.file = BytesIO(model.organigram_file.read())
fs.type = model.organigram_file.content_type
fs.filename = model.organigram_file.filename
self.organigram.data = self.organigram.process_fieldstorage(
cast('_FieldStorageWithFile', fs)
)
self.coordinates.data = model.coordinates
if hasattr(self, 'access'):
self.access.data = model.access
if hasattr(self, 'publication_start'):
self.publication_start.process_data(model.publication_start)
if hasattr(self, 'publication_end'):
self.publication_end.process_data(model.publication_end)
self.reorder_export_fields()
[docs]
def get_parent_id_choices_callback(
excluded: Collection[str] = ()
) -> Callable[[MoveAgencyForm, ChosenSelectField], list[SelectChoice]]:
def parent_id_choices(
form: MoveAgencyForm,
field: ChosenSelectField
) -> list[SelectChoice]:
agencies = ExtendedAgencyCollection(form.request.session)
choices = [
SelectChoice(str(agency.id), agency.title)
for agency in agencies.query().order_by(None).order_by(
func.unaccent(ExtendedAgency.title)
)
if form.request.has_permission(agency, Private)
if str(agency.id) not in excluded
]
if (
form.request.has_permission(agencies, Private)
and 'root' not in excluded
):
choices.insert(
0, SelectChoice('root', form.request.translate(_('- Root -')))
)
return choices
return parent_id_choices
[docs]
class MoveAgencyForm(Form):
""" Form to move an agency. """
[docs]
parent_id = ChosenSelectField(
label=_('Destination'),
choices=get_parent_id_choices_callback(),
validators=[
InputRequired()
]
)
[docs]
def ensure_valid_parent(self) -> bool:
"""
As a new destination (parent page) every menu item is valid except
yourself. You cannot assign yourself as the new destination
:return: bool
"""
if self.parent_id.data and self.parent_id.data.isdigit():
new_parent_id = int(self.parent_id.data)
# prevent selecting yourself as new parent
if self.model.id == new_parent_id:
assert isinstance(self.parent_id.errors, list)
self.parent_id.errors.append(
_('Invalid destination selected'))
return False
return True
[docs]
def update_model(self, model: ExtendedAgency) -> None:
session = self.request.session
agencies = ExtendedAgencyCollection(session)
new_parent_id = None
new_parent = None
if self.parent_id.data and self.parent_id.data.isdigit():
new_parent_id = int(self.parent_id.data)
new_parent = agencies.by_id(new_parent_id)
model.name = agencies.get_unique_child_name(model.title, new_parent)
model.parent_id = new_parent_id
[docs]
def apply_model(self, model: ExtendedAgency) -> None:
removed: set[str] = set()
def remove(item: ExtendedAgency) -> None:
removed.add(str(item.id))
def remove_with_children(item: ExtendedAgency) -> None:
remove(item)
for child in item.children:
remove_with_children(child)
if model.parent:
remove(model.parent)
else:
removed.add('root')
remove_with_children(model)
self.parent_id.choices = get_parent_id_choices_callback(removed)