-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtasks.py
More file actions
425 lines (347 loc) · 13.7 KB
/
tasks.py
File metadata and controls
425 lines (347 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
from django.db.models import Q
from conferences.tasks import send_conference_voucher_email
from conferences.vouchers import create_conference_voucher
from conferences.models.conference_voucher import ConferenceVoucher
from pycon.celery_utils import OnlyOneAtTimeTask
from google_api.exceptions import NoGoogleCloudQuotaLeftError
from googleapiclient.errors import HttpError
from google_api.sdk import youtube_videos_insert, youtube_videos_set_thumbnail
from integrations import plain
from pretix import user_has_admission_ticket
from django.utils import timezone
from grants.tasks import get_name
from notifications.models import EmailTemplate, EmailTemplateIdentifier
from urllib.parse import urljoin
from django.conf import settings
import logging
from integrations import slack
from pycon.celery import app
from schedule.models import ScheduleItemSentForVideoUpload
from schedule.video_upload import (
cleanup_local_files,
create_video_info,
download_video_file,
extract_video_thumbnail,
)
from users.models import User
from schedule.models import ScheduleItem
logger = logging.getLogger(__name__)
@app.task
def send_schedule_invitation_email(*, schedule_item_id, is_reminder):
schedule_item = ScheduleItem.objects.get(id=schedule_item_id)
submission = schedule_item.submission
language_code = schedule_item.language.code
conference = schedule_item.conference
invitation_url = urljoin(
settings.FRONTEND_URL, f"/schedule/invitation/{submission.hashid}"
)
speaker_id = submission.speaker_id
submission_title = submission.title.localize(language_code)
speaker = User.objects.get(id=speaker_id)
conference_name = conference.name
email_template = EmailTemplate.objects.for_conference(conference).get_by_identifier(
EmailTemplateIdentifier.proposal_scheduled
)
email_template.send_email(
recipient=speaker,
placeholders={
"proposal_title": submission_title,
"conference_name": conference_name,
"invitation_url": invitation_url,
"speaker_name": get_name(speaker, "there"),
"is_reminder": is_reminder,
},
)
schedule_item.speaker_invitation_sent_at = timezone.now()
schedule_item.save()
@app.task
def send_submission_time_slot_changed_email(*, schedule_item_id):
from schedule.models import ScheduleItem
schedule_item = ScheduleItem.objects.get(id=schedule_item_id)
submission = schedule_item.submission
speaker_id = submission.speaker_id
proposal_title = submission.title.localize(schedule_item.language.code)
invitation_url = urljoin(
settings.FRONTEND_URL, f"/schedule/invitation/{submission.hashid}"
)
proposal_speaker = User.objects.get(id=speaker_id)
conference = schedule_item.conference
conference_name = schedule_item.conference.name
email_template = EmailTemplate.objects.for_conference(conference).get_by_identifier(
EmailTemplateIdentifier.proposal_scheduled_time_changed
)
email_template.send_email(
recipient=proposal_speaker,
placeholders={
"proposal_title": proposal_title,
"invitation_url": invitation_url,
"conference_name": conference_name,
"speaker_name": get_name(proposal_speaker, "there"),
},
)
@app.task
def notify_new_schedule_invitation_answer_slack(
*, schedule_item_id, invitation_admin_url, schedule_item_admin_url
):
from schedule.models import ScheduleItem
schedule_item = ScheduleItem.objects.get(id=schedule_item_id)
conference = schedule_item.conference
speaker = schedule_item.submission.speaker
user_name = get_name(speaker)
speaker_notes = schedule_item.speaker_invitation_notes
slack.send_message(
[
{
"type": "section",
"text": {
"text": f"{schedule_item.title} - {user_name} answer:",
"type": "mrkdwn",
},
},
{
"type": "section",
"text": {
"text": _schedule_item_status_to_message(schedule_item.status),
"type": "mrkdwn",
},
},
{
"type": "section",
"text": {
"text": f"*Speaker notes*\n{speaker_notes}",
"type": "mrkdwn",
},
},
],
[
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*<{invitation_admin_url}|Open invitation>*",
},
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*<{schedule_item_admin_url}|Open schedule item>*",
},
},
],
},
],
oauth_token=conference.get_slack_oauth_token(),
channel_id=conference.slack_speaker_invitation_answer_channel_id,
)
@app.task
def send_speaker_communication_email(
*,
subject,
body,
user_id,
conference_id,
only_speakers_without_ticket,
):
from conferences.models import Conference
user = User.objects.get(id=user_id)
conference = Conference.objects.get(id=conference_id)
if only_speakers_without_ticket and user_has_admission_ticket(
email=user.email,
event_organizer=conference.pretix_organizer_id,
event_slug=conference.pretix_event_id,
):
return
email_template = EmailTemplate.objects.for_conference(conference).get_by_identifier(
EmailTemplateIdentifier.speaker_communication
)
email_template.send_email(
recipient=user,
placeholders={
"conference_name": conference.name,
"user_name": get_name(user, "there"),
"body": body.replace("\n", "<br />"),
"subject": subject,
},
)
def _schedule_item_status_to_message(status: str):
from schedule.models import ScheduleItem
if status == ScheduleItem.STATUS.confirmed:
return "I am happy with the time slot."
if status == ScheduleItem.STATUS.maybe:
return "I can make this time slot work if it is not possible to change"
if status == ScheduleItem.STATUS.rejected:
return "The time slot does not work for me"
if status == ScheduleItem.STATUS.cant_attend:
return "I can't attend the conference anymore"
return "Undefined"
@app.task
def send_schedule_invitation_plain_message(*, schedule_item_id, message):
from schedule.models import ScheduleItem
if not settings.PLAIN_API:
return
schedule_item = ScheduleItem.objects.get(id=schedule_item_id)
user = schedule_item.submission.speaker
existing_thread_id = schedule_item.plain_thread_id
name = get_name(user, "Speaker")
title = f"{name} Schedule Invitation time slot"
if existing_thread_id:
title = "Additional message"
thread_id = plain.send_message(
user, title=title, message=message, existing_thread_id=existing_thread_id
)
schedule_item.plain_thread_id = thread_id
schedule_item.save(update_fields=["plain_thread_id"])
def upload_schedule_item_video(*, sent_for_video_upload_state_id: int):
sent_for_video_upload = ScheduleItemSentForVideoUpload.objects.get(
id=sent_for_video_upload_state_id
)
if not sent_for_video_upload.is_pending:
logger.info(
"Schedule Item Sent for upload %s is not pending but %s, skipping",
sent_for_video_upload_state_id,
sent_for_video_upload.status,
)
return
sent_for_video_upload.status = ScheduleItemSentForVideoUpload.Status.processing
sent_for_video_upload.failed_reason = ""
sent_for_video_upload.attempts += 1
sent_for_video_upload.last_attempt_at = timezone.now()
sent_for_video_upload.save(
update_fields=["status", "attempts", "failed_reason", "last_attempt_at"]
)
schedule_item = sent_for_video_upload.schedule_item
remote_video_path = schedule_item.video_uploaded_path
video_id = None
if not sent_for_video_upload.video_uploaded:
logger.info("Uploading video for schedule_item_id=%s", schedule_item.id)
video_info = create_video_info(schedule_item)
logger.info("Downloading video file %s", remote_video_path)
local_video_path = download_video_file(schedule_item.id, remote_video_path)
for response in youtube_videos_insert(
title=video_info.title,
description=video_info.description,
tags=video_info.tags_as_str,
file_path=local_video_path,
):
logger.info(
"schedule_item_id=%s Video uploading: %s", schedule_item.id, response
)
sent_for_video_upload.video_uploaded = True
sent_for_video_upload.save(update_fields=["video_uploaded"])
video_id = response["id"]
schedule_item.youtube_video_id = video_id
schedule_item.save(update_fields=["youtube_video_id"])
else:
logger.info("Video already uploaded for schedule_item_id=%s", schedule_item.id)
if not sent_for_video_upload.thumbnail_uploaded:
video_id = video_id or schedule_item.youtube_video_id
assert video_id, "Video marked as uploaded but Video ID is missing"
logger.info("Extracting thumbnail for schedule_item_id=%s", schedule_item.id)
thumbnail_path = extract_video_thumbnail(
remote_video_path,
schedule_item.id,
)
# we don't need the video file anymore as we already extracted the thumbnail
cleanup_local_files(schedule_item.id, delete_thumbnail=False)
try:
youtube_videos_set_thumbnail(
video_id=video_id,
thumbnail_path=thumbnail_path,
)
except HttpError as e:
if e.status_code == 429:
# we reached the daily thumbnail limit
logger.warning(
"Reached the daily thumbnail limit! schedule_item_id=%s moved back to pending",
schedule_item.id,
)
sent_for_video_upload.status = (
ScheduleItemSentForVideoUpload.Status.pending
)
sent_for_video_upload.save(update_fields=["status"])
return
raise
sent_for_video_upload.thumbnail_uploaded = True
sent_for_video_upload.save(update_fields=["thumbnail_uploaded"])
cleanup_local_files(schedule_item.id)
logger.info("Video uploaded for schedule_item_id=%s", schedule_item.id)
sent_for_video_upload.status = ScheduleItemSentForVideoUpload.Status.completed
sent_for_video_upload.save(update_fields=["status"])
@app.task(base=OnlyOneAtTimeTask)
def process_schedule_items_videos_to_upload():
statuses = (
ScheduleItemSentForVideoUpload.objects.filter(
Q(last_attempt_at__isnull=True)
| Q(
last_attempt_at__lt=timezone.now() - timezone.timedelta(hours=1),
)
)
.to_upload()
.order_by("last_attempt_at")
)
for sent_for_video_upload_state in statuses.iterator():
try:
upload_schedule_item_video(
sent_for_video_upload_state_id=sent_for_video_upload_state.id
)
except NoGoogleCloudQuotaLeftError:
logger.info(
"No google cloud quota left to upload the schedule item %s. Moving back to pending and stopping processing.",
sent_for_video_upload_state.schedule_item.id,
)
sent_for_video_upload_state.status = (
ScheduleItemSentForVideoUpload.Status.pending
)
sent_for_video_upload_state.failed_reason = "No Google Cloud Quota Left"
sent_for_video_upload_state.save(update_fields=["status", "failed_reason"])
break
except Exception as e:
logger.exception(
"Error processing schedule item %s video upload: %s",
sent_for_video_upload_state.schedule_item.id,
e,
)
sent_for_video_upload_state.status = (
ScheduleItemSentForVideoUpload.Status.failed
)
sent_for_video_upload_state.failed_reason = str(e)
sent_for_video_upload_state.save(update_fields=["status", "failed_reason"])
@app.task
def create_and_send_voucher_to_speaker(schedule_item_id: int):
schedule_item = ScheduleItem.objects.get(id=schedule_item_id)
speakers = schedule_item.speakers
if not speakers:
return
speaker = speakers[0]
co_speaker = speakers[1] if len(speakers) > 1 else None
_send_conference_voucher(
speaker,
schedule_item.conference,
ConferenceVoucher.VoucherType.SPEAKER,
)
if co_speaker:
_send_conference_voucher(
co_speaker,
schedule_item.conference,
ConferenceVoucher.VoucherType.CO_SPEAKER,
)
def _send_conference_voucher(user, conference, voucher_type):
conference_voucher = (
ConferenceVoucher.objects.for_conference(conference).for_user(user).first()
)
if conference_voucher:
logger.info(
"User %s already has a voucher for conference %s, not creating a new one",
user.id,
conference.id,
)
return
conference_voucher = create_conference_voucher(
conference=conference,
user=user,
voucher_type=voucher_type,
)
send_conference_voucher_email.delay(conference_voucher_id=conference_voucher.id)