-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnewsletter.py
More file actions
450 lines (377 loc) · 13.7 KB
/
Copy pathnewsletter.py
File metadata and controls
450 lines (377 loc) · 13.7 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
from __future__ import annotations
from collections import OrderedDict
from datetime import timedelta
import transaction
from wtforms.validators import DataRequired
from onegov.core.csv import convert_excel_to_csv, CSVFile
from onegov.form.fields import UploadField
from onegov.org.forms.fields import HtmlField
from onegov.org.utils import extract_categories_and_subcategories
from onegov.form.validators import FileSizeLimit
from onegov.form.validators import WhitelistedMimeType
from wtforms.fields import BooleanField
from onegov.file.utils import name_without_extension
from onegov.form import Form
from onegov.form.fields import ChosenSelectField
from onegov.form.fields import DateTimeLocalField
from onegov.form.fields import MultiCheckboxField
from onegov.newsletter import Recipient, RecipientCollection
from onegov.org import _
from sedate import replace_timezone, to_timezone, utcnow
from wtforms.fields import RadioField
from wtforms.fields import StringField
from wtforms.fields import TextAreaField
from wtforms.validators import InputRequired
from wtforms.validators import ValidationError
from markupsafe import Markup
from typing import Any, TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Iterable, Callable
from onegov.core.csv import DefaultRow
from onegov.file import File
from onegov.event.models import Occurrence
from onegov.org.models import News
from onegov.org.request import OrgRequest
from onegov.newsletter.models import Newsletter
from typing import Self
from wtforms.fields.choices import _Choice
class NewsletterForm(Form):
title = StringField(
label=_('Title'),
description=_('Used in the overview and the e-mail subject'),
validators=[InputRequired()])
lead = TextAreaField(
label=_('Editorial'),
description=_('A few words about this edition of the newsletter'),
render_kw={'rows': 6})
closing_remark = HtmlField(
label=_('Closing remark'),
description=_('Closing remark at the end of the newsletter'),
render_kw={'rows': 6},
)
# FIXME: Why are we passing the request in? It should alread be stored on
# the form itself.
def update_model(self, model: Newsletter, request: OrgRequest) -> None:
assert self.title.data is not None
model.title = self.title.data
model.lead = self.lead.data
model.html = self.get_html(request)
model.closing_remark = self.closing_remark.data
def apply_model(self, model: Newsletter) -> None:
self.title.data = model.title
self.lead.data = model.lead
self.closing_remark.data = model.closing_remark
# FIXME: same here
def get_html(self, request: OrgRequest) -> str:
return ''
@classmethod
def with_news(
cls,
request: OrgRequest,
news: Iterable[News]
) -> type[Self]:
choices = tuple(
(
str(item.id),
Markup(
'<div class="title">{}</div>'
'<div class="date">{}</div>'
).format(
item.title,
request.format_date(item.created, 'relative'),
)
)
for item in news
)
if not choices:
return cls
class NewsletterWithNewsForm(cls): # type:ignore
news = MultiCheckboxField(
label=_('Latest news'),
choices=choices,
render_kw={
'prefix_label': False,
'class_': 'recommended'
}
)
show_news_as_tiles = BooleanField(
label=_('Show news as tiles'),
description=_(
'If checked, news are displayed as tiles. Otherwise, '
'news are listed in full length.'),
default=True
)
def update_model(
self,
model: Newsletter,
request: OrgRequest # FIXME: same here
) -> None:
super().update_model(model, request)
model.content['news'] = self.news.data
model.content['show_news_as_tiles'] = (
self.show_news_as_tiles.data)
def apply_model(self, model: Newsletter) -> None:
super().apply_model(model)
self.news.data = model.content.get('news')
self.show_news_as_tiles.data = model.content.get(
'show_news_as_tiles', True)
return NewsletterWithNewsForm
@classmethod
def with_occurrences(
cls,
request: OrgRequest,
occurrences: Iterable[Occurrence]
) -> type[Self]:
choices = tuple(
(
str(item.id),
Markup(
'<div class="title">{}</div>'
'<div class="date">{}</div>'
).format(
item.title,
request.format_date(
item.localized_start, 'dd.MM.yyyy HH:mm')
)
)
for item in occurrences
)
if not choices:
return cls
class NewsletterWithOccurrencesForm(cls): # type:ignore
occurrences = MultiCheckboxField(
label=_('Events'),
choices=choices,
render_kw={
'prefix_label': False,
'class_': 'recommended'
}
)
def update_model(
self,
model: Newsletter,
request: OrgRequest # FIXME: same here
) -> None:
super().update_model(model, request)
model.content['occurrences'] = self.occurrences.data
def apply_model(self, model: Newsletter) -> None:
super().apply_model(model)
self.occurrences.data = model.content.get('occurrences')
return NewsletterWithOccurrencesForm
@classmethod
def with_publications(
cls,
request: OrgRequest,
publications: Iterable[File]
) -> type[Self]:
choices = tuple(
(
str(item.id),
Markup(
'<div class="title">{}</div>'
'<div class="date">{}</div>'
).format(
name_without_extension(item.name),
request.format_date(item.created, 'dd.MM.yyyy')
)
)
for item in publications
)
if not choices:
return cls
class NewsletterWithPublicationsForm(cls): # type:ignore
publications = MultiCheckboxField(
label=_('Publications'),
choices=choices,
render_kw={
'prefix_label': False,
'class_': 'recommended'
}
)
def update_model(
self,
model: Newsletter,
request: OrgRequest # FIXME: same here
) -> None:
super().update_model(model, request)
model.content['publications'] = self.publications.data
def apply_model(self, model: Newsletter) -> None:
super().apply_model(model)
self.publications.data = model.content.get('publications')
return NewsletterWithPublicationsForm
class NewsletterSendForm(Form):
if TYPE_CHECKING:
request: OrgRequest
categories = MultiCheckboxField(
label=_('Categories'),
description=_('Select categories the newsletter reports on. The '
'users will receive the newsletter only if it '
'reports on at least one of the categories the user '
'subscribed to.'),
choices=[]
)
send = RadioField(
_('Send'),
choices=(
('now', _('Now')),
('specify', _('At a specified time'))
),
default='now'
)
time = DateTimeLocalField(
label=_('Time'),
validators=[InputRequired()],
depends_on=('send', 'specify')
)
def validate_time(self, field: DateTimeLocalField) -> None:
if not field.data:
return
# FIXME: We should probably store the timezone on the request
# and make the attribute on Layout a pure forwarding
# of the one on the request
from onegov.org.layout import DefaultLayout # XXX circular import
layout = DefaultLayout(self.model, self.request)
if self.send.data == 'specify':
time = replace_timezone(field.data, layout.timezone)
time = to_timezone(time, 'UTC')
if time < (utcnow() + timedelta(seconds=60 * 5)):
raise ValidationError(_(
'Scheduled time must be at least 5 minutes in the future'
))
if time.minute != 0:
raise ValidationError(_(
'Newsletters can only be sent on the hour '
'(10:00, 11:00, etc.)'
))
self.time.data = time
def on_request(self) -> None:
choices: list[_Choice] = []
categories, subcategories = extract_categories_and_subcategories(
self.request.app.org.newsletter_categories)
for cat, sub in zip(categories, subcategories):
choices.append((cat, cat))
choices.extend((s, f'\xa0\xa0\xa0{s}') for s in sub)
if not choices:
self.delete_field('categories')
return
self.categories.choices = choices
class NewsletterTestForm(Form):
selected_recipient = ChosenSelectField(
label=_('Recipient'),
choices=[],
)
@property
def recipient(self) -> Recipient:
return (
self.request.session.query(Recipient)
.filter_by(id=self.selected_recipient.data)
.one()
)
def on_request(self) -> None:
recipients = (
self.request.session.query(Recipient)
.with_entities(Recipient.id, Recipient.address)
.filter_by(confirmed=True)
)
self.selected_recipient.choices = [
(r.id.hex, r.address)
for r in recipients
]
class NewsletterSubscriberImportExportForm(Form):
dry_run = BooleanField(
label=_('Dry Run'),
description=_('Do not actually import the newsletter subscribers'),
default=False
)
file = UploadField(
label=_('Import'),
validators=[
DataRequired(),
WhitelistedMimeType({
'application/excel',
'application/vnd.ms-excel',
(
'application/'
'vnd.openxmlformats-officedocument.spreadsheetml.sheet'
),
'application/vnd.ms-office',
'application/octet-stream',
'application/zip',
'text/csv',
'text/plain',
}),
FileSizeLimit(10 * 1024 * 1024)
],
render_kw={'force_simple': True}
)
@property
def headers(self) -> dict[str, str]:
return {
'address': self.request.translate(_('Address')),
'created': self.request.translate(_(
'Subscribed on')),
}
def run_export(self,
formatter: Callable[[object], Any]
) -> list[OrderedDict[str, Any]]:
recipients = RecipientCollection(
self.request.session).ordered_by_status_address().filter_by(
confirmed=True
)
headers = self.headers
data = []
for recipient in recipients:
r = OrderedDict()
for k, v in headers.items():
r[v] = formatter(getattr(recipient, k))
data.append(r)
return data
def run_import(self) -> tuple[int, list[str]]:
headers = self.headers
headers.pop('created')
session = self.request.session
recipients = RecipientCollection(session)
try:
assert self.file.file is not None
csvfile = convert_excel_to_csv(self.file.file)
except Exception:
return 0, ['Error converting file']
try:
# dialect needs to be set, else error
csv = CSVFile(csvfile, dialect='excel')
except Exception:
return 0, ['Error reading CSV file']
lines = list(csv.lines)
columns = {
key: csv.as_valid_identifier(value)
for key, value in headers.items()
}
def get(line: DefaultRow, column: str) -> Any:
return getattr(line, column)
count = 0
skipped = 0
errors = []
for number, line in enumerate(lines, start=1):
try:
kwargs = {
attribute: get(line, column)
for attribute, column in columns.items()
}
kwargs['confirmed'] = True
address = next(iter(kwargs.values()))
if recipients.by_address(address):
# silently skip duplicates
skipped += 1
continue
recipients.add(**kwargs)
count += 1
except Exception as e:
error_msg = f'Error on line {number}: {e!s}'
errors.append(error_msg)
if self.dry_run.data:
transaction.abort()
summary = [f'Imported: {count}, Skipped duplicates: {skipped}']
if errors:
summary.append(f'Errors: {len(errors)}')
summary.extend(errors)
return count, summary