-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmeeting.py
More file actions
102 lines (81 loc) · 2.98 KB
/
Copy pathmeeting.py
File metadata and controls
102 lines (81 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from __future__ import annotations
from wtforms import StringField
from wtforms.validators import Optional, InputRequired
from onegov.form import Form
from onegov.form.fields import TimezoneDateTimeField, ChosenSelectMultipleField
from onegov.org.forms.fields import HtmlField
from onegov.parliament import _
from onegov.parliament.models import Meeting, MeetingItem
from typing import TYPE_CHECKING
from collections.abc import Collection
if TYPE_CHECKING:
from collections.abc import Collection
class MeetingForm(Form):
title = StringField(
label=_('Title'),
validators=[InputRequired()],
)
start_datetime = TimezoneDateTimeField(
timezone='Europe/Zurich',
label=_('Start'),
validators=[Optional()],
)
end_datetime = TimezoneDateTimeField(
timezone='Europe/Zurich',
label=_('End'),
validators=[Optional()],
)
address = HtmlField(
label=_('Address'),
validators=[InputRequired()],
render_kw={'rows': 3}
)
description = HtmlField(
label=_('Description'),
validators=[Optional()],
render_kw={'rows': 5}
)
agenda_items = ChosenSelectMultipleField(
label=_('Agenda Items'),
validators=[Optional()],
choices=[],
)
def on_request(self) -> None:
meetings = (self.request.session.query(MeetingItem)
.order_by(MeetingItem.number, MeetingItem.title)
.all())
self.agenda_items.choices = [
(item.id.hex, item.display_name())
for item in meetings
]
def populate_obj(
self,
obj: object,
exclude: Collection[str] | None = None,
include: Collection[str] | None = None
) -> None:
if not isinstance(obj, Meeting):
raise TypeError('Object must be a MeetingItem')
meeting: Meeting = obj
item: MeetingItem | None
super().populate_obj(obj, exclude=exclude, include=include)
meeting_item_ids = {item.id.hex for item in meeting.meeting_items}
new_item_ids = set(self.agenda_items.data or [])
items_to_add = new_item_ids - meeting_item_ids
for new_id in items_to_add:
item = (self.request.session.query(MeetingItem)
.filter(MeetingItem.id == new_id).one())
if item is not None:
meeting.meeting_items.append(item)
items_to_remove = meeting_item_ids - new_item_ids
for current_id in items_to_remove:
item = self.request.session.query(MeetingItem).get(current_id)
if item is not None:
meeting.meeting_items.remove(item)
def process_obj(self, obj: MeetingItem) -> None: # type: ignore[override]
if not isinstance(obj, Meeting):
raise TypeError('Object must be a MeetingItem')
super().process_obj(obj)
self.agenda_items.data = [
item.id.hex for item in obj.meeting_items
]