-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
234 lines (182 loc) · 7.29 KB
/
Copy pathapp.py
File metadata and controls
234 lines (182 loc) · 7.29 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from __future__ import annotations
from onegov.agency.api import AgencyApiEndpoint
from onegov.agency.api import MembershipApiEndpoint
from onegov.agency.api import PersonApiEndpoint
from onegov.agency.custom import get_global_tools
from onegov.agency.custom import get_top_navigation
from onegov.agency.custom import get_modules
from onegov.agency.forms import UserGroupForm
from onegov.agency.initial_content import create_new_organisation
from onegov.agency.pdf import AgencyPdfAr, AgencyPdfBs, AgencyPdfLu
from onegov.agency.pdf import AgencyPdfDefault
from onegov.agency.pdf import AgencyPdfZg
from onegov.agency.request import AgencyRequest
from onegov.agency.theme import AgencyTheme
from onegov.core import utils
from onegov.town6 import TownApp
from onegov.town6.app import get_editor_asset as editor_assets
from onegov.town6.app import get_i18n_localedirs as get_org_i18n_localedirs
from typing import Any
from typing import Self
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from _typeshed import SupportsRead
from collections.abc import Callable
from collections.abc import Iterator
from datetime import datetime
from fs.base import FS
from fs.base import SubFS
from onegov.api import ApiEndpoint
from onegov.core.types import RenderData
from onegov.org.models import Organisation
class AgencyApp(TownApp):
request_class: type[AgencyRequest[Self]] = AgencyRequest
if TYPE_CHECKING:
# FIXME: Maybe we should consider just raising an exception
# if filestorage is accesed without it being configured
@property
def filestorage(self) -> SubFS[FS]: ...
@property
def root_pdf_exists(self) -> bool:
return self.filestorage.exists('root.pdf')
@property
def people_xlsx_exists(self) -> bool:
return self.filestorage.exists('people.xlsx')
@property
def root_pdf_modified(self) -> datetime | None:
if self.root_pdf_exists:
return self.filestorage.getdetails('root.pdf').modified
return None
@property
def people_xlsx_modified(self) -> datetime | None:
if self.people_xlsx:
return self.filestorage.getdetails('people.xlsx').modified
return None
@property
def root_pdf(self) -> bytes | None:
result: bytes | None = None
if self.filestorage.exists('root.pdf'):
with self.filestorage.open('root.pdf', 'rb') as file:
# FS bug with mode=rb
result = file.read() # type:ignore[assignment]
return result
# FIXME: asymmetric property
@root_pdf.setter
def root_pdf(self, value: SupportsRead[bytes] | bytes) -> None:
with self.filestorage.open('root.pdf', 'wb') as file:
if hasattr(value, 'read'):
value = value.read()
# FS bug with mode=wb
file.write(value) # type:ignore
@property
def people_xlsx(self) -> bytes | None:
result: bytes | None = None
if self.filestorage.exists('people.xlsx'):
with self.filestorage.open('people.xlsx', 'rb') as file:
# FS bug with mode=rb
result = file.read() # type:ignore[assignment]
return result
# FIXME: asymmetric property
@people_xlsx.setter
def people_xlsx(self, value: SupportsRead[bytes] | bytes) -> None:
with self.filestorage.open('people.xlsx', 'wb') as file:
if hasattr(value, 'read'):
value = value.read()
# FS bug with mode=wb
file.write(value) # type:ignore
@property
def pdf_class(self) -> type[AgencyPdfDefault]:
pdf_layout = self.org.meta.get('pdf_layout')
if pdf_layout == 'ar':
return AgencyPdfAr
if pdf_layout == 'zg':
return AgencyPdfZg
if pdf_layout == 'bs':
return AgencyPdfBs
if pdf_layout == 'lu':
return AgencyPdfLu
return AgencyPdfDefault
@property
def enable_yubikey(self) -> bool:
return self.org.meta.get('enable_yubikey', self._enable_yubikey)
@enable_yubikey.setter
def enable_yubikey(self, value: bool) -> None:
self._enable_yubikey = value
def configure_organisation(
self,
*,
enable_user_registration: bool = False,
enable_yubikey: bool = False,
disable_password_reset: bool = False,
**cfg: Any
) -> None:
super().configure_organisation(
enable_user_registration=enable_user_registration,
enable_yubikey=enable_yubikey,
disable_password_reset=disable_password_reset,
**cfg
)
@AgencyApp.setting(section='org', name='create_new_organisation')
def get_create_new_organisation_factory(
) -> Callable[[AgencyApp, str], Organisation]:
return create_new_organisation
@AgencyApp.template_directory()
def get_template_directory() -> str:
return 'templates'
@AgencyApp.template_variables()
def get_template_variables(request: AgencyRequest) -> RenderData:
return {
'global_tools': tuple(get_global_tools(request)),
'top_navigation': tuple(get_top_navigation(request)),
'modules': get_modules(request)
}
@AgencyApp.setting(section='core', name='theme')
def get_theme() -> AgencyTheme:
return AgencyTheme()
@AgencyApp.setting(section='org', name='usergroup_form_class')
def get_usergroup_form_class() -> type[UserGroupForm]:
return UserGroupForm
@AgencyApp.setting(section='i18n', name='localedirs')
def get_i18n_localedirs() -> list[str]:
mine = utils.module_path('onegov.agency', 'locale')
return [mine, *get_org_i18n_localedirs()]
@AgencyApp.setting(section='org', name='ticket_manager_roles')
def get_ticket_manager_roles() -> tuple[str, ...]:
return ('admin', 'editor', 'member')
@AgencyApp.setting(section='org', name='disabled_extensions')
def get_disabled_extensions() -> tuple[str, ...]:
return ('PersonLinkExtension', )
# NOTE: A citizen login doesn't make a ton of sense here, given the
# simplicity of the tickets processed in this system, but we
# can think about enabling it in the future.
@AgencyApp.setting(section='org', name='citizen_login_enabled')
def get_citizen_login_enabled() -> bool:
return False
@AgencyApp.webasset_output()
def get_webasset_output() -> str:
return 'assets/bundles'
@AgencyApp.webasset_path()
def get_js_path() -> str:
return 'assets/js'
@AgencyApp.webasset('people-select')
def get_people_select_asset() -> Iterator[str]:
yield 'people-select.js'
@AgencyApp.webasset('sortable-multi-checkbox')
def get_sortable_multi_checkbox_asset() -> Iterator[str]:
yield 'sortable.js'
yield 'sortable-multi-checkbox.js'
@AgencyApp.webasset('editor')
def get_editor_assets() -> Iterator[str]:
yield from editor_assets()
@AgencyApp.setting(section='api', name='endpoints')
def get_api_endpoints_handler(
) -> Callable[[AgencyRequest], Iterator[ApiEndpoint[Any, Any]]]:
def get_api_endpoints(
request: AgencyRequest,
page: int = 0,
extra_parameters: dict[str, Any] | None = None
) -> Iterator[ApiEndpoint[Any, Any]]:
yield AgencyApiEndpoint(request, extra_parameters, page)
yield PersonApiEndpoint(request, extra_parameters, page)
yield MembershipApiEndpoint(request, extra_parameters, page)
return get_api_endpoints