Source code for pas.forms.settings
from onegov.form import Form
from onegov.form.fields import MultiCheckboxField
from onegov.pas import _
from onegov.pas.custom import ABSCHLUSS_OPT_OUT_KEY
from onegov.pas.custom import notifiable_admins
from wtforms.fields import SelectChoice
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Collection
from onegov.org.models import Organisation
from onegov.pas.request import PasRequest
[docs]
class AbschlussNotificationSettingsForm(Form):
if TYPE_CHECKING:
[docs]
recipients = MultiCheckboxField(
label=_('Recipients'),
description=_(
'All administrators receive an e-mail when an Abschluss is set '
'for a commission. Deselect administrators who should not '
'receive it.'
),
choices=[],
)
[docs]
def on_request(self) -> None:
self.recipients.choices = [
SelectChoice(
user.id.hex,
f'{user.realname} ({user.username})'
if user.realname else user.username
)
for user in notifiable_admins(self.request.session)
]
[docs]
def populate_obj( # type:ignore[override]
self,
obj: Organisation, # type:ignore[override]
exclude: Collection[str] | None = None,
include: Collection[str] | None = None
) -> None:
super().populate_obj(obj, exclude={'recipients'}, include=include)
selected = set(self.recipients.data or ())
obj.meta[ABSCHLUSS_OPT_OUT_KEY] = [
choice.value
for choice in self.recipients.choices
if choice.value not in selected
]
[docs]
def process_obj(self, obj: Organisation) -> None: # type:ignore[override]
super().process_obj(obj)
opt_out_ids = set(obj.meta.get(ABSCHLUSS_OPT_OUT_KEY) or ())
self.recipients.data = [
choice.value
for choice in self.recipients.choices
if choice.value not in opt_out_ids
]