-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmutations.py
More file actions
500 lines (411 loc) · 18 KB
/
mutations.py
File metadata and controls
500 lines (411 loc) · 18 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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
from urllib.parse import urljoin
from django.conf import settings
from conferences.frontend import trigger_frontend_revalidate
from grants.tasks import get_name
from notifications.models import EmailTemplate, EmailTemplateIdentifier
from strawberry.scalars import JSON
from django.db import transaction
import math
import re
from typing import Annotated, Union, Optional
from privacy_policy.record import record_privacy_policy_acceptance
import strawberry
from strawberry import ID
from strawberry.types import Info
from api.permissions import IsAuthenticated
from api.types import BaseErrorType, MultiLingualInput
from conferences.models.conference import Conference
from i18n.strings import LazyI18nString
from languages.models import Language
from participants.models import Participant
from submissions.models import ProposalMaterial, Submission as SubmissionModel
from submissions.tasks import notify_new_cfp_submission
from .types import Submission, SubmissionMaterialInput
FACEBOOK_LINK_MATCH = re.compile(r"^http(s)?:\/\/(www\.)?facebook\.com\/")
LINKEDIN_LINK_MATCH = re.compile(r"^http(s)?:\/\/(www\.)?linkedin\.com\/")
MASTODON_HANDLE_MATCH = re.compile(
r"^(https?:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}\/@[a-zA-Z0-9_]+|@?[a-zA-Z0-9_]+@[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,})$"
)
@strawberry.type
class ProposalMaterialErrors:
file_id: list[str] = strawberry.field(default_factory=list)
url: list[str] = strawberry.field(default_factory=list)
id: list[str] = strawberry.field(default_factory=list)
@strawberry.type
class SendSubmissionErrors(BaseErrorType):
@strawberry.type
class _SendSubmissionErrors:
instance: list[str] = strawberry.field(default_factory=list)
title: list[str] = strawberry.field(default_factory=list)
abstract: list[str] = strawberry.field(default_factory=list)
topic: list[str] = strawberry.field(default_factory=list)
languages: list[str] = strawberry.field(default_factory=list)
conference: list[str] = strawberry.field(default_factory=list)
type: list[str] = strawberry.field(default_factory=list)
duration: list[str] = strawberry.field(default_factory=list)
elevator_pitch: list[str] = strawberry.field(default_factory=list)
notes: list[str] = strawberry.field(default_factory=list)
audience_level: list[str] = strawberry.field(default_factory=list)
tags: list[str] = strawberry.field(default_factory=list)
short_social_summary: list[str] = strawberry.field(default_factory=list)
materials: list[ProposalMaterialErrors] = strawberry.field(default_factory=list)
speaker_bio: list[str] = strawberry.field(default_factory=list)
speaker_photo: list[str] = strawberry.field(default_factory=list)
speaker_website: list[str] = strawberry.field(default_factory=list)
speaker_level: list[str] = strawberry.field(default_factory=list)
previous_talk_video: list[str] = strawberry.field(default_factory=list)
speaker_twitter_handle: list[str] = strawberry.field(default_factory=list)
speaker_instagram_handle: list[str] = strawberry.field(default_factory=list)
speaker_linkedin_url: list[str] = strawberry.field(default_factory=list)
speaker_facebook_url: list[str] = strawberry.field(default_factory=list)
speaker_mastodon_handle: list[str] = strawberry.field(default_factory=list)
non_field_errors: list[str] = strawberry.field(default_factory=list)
errors: _SendSubmissionErrors = None
class BaseSubmissionInput:
def clean(self):
self.title = self.title.clean(self.languages)
self.elevator_pitch = self.elevator_pitch.clean(self.languages)
self.abstract = self.abstract.clean(self.languages)
def multi_lingual_validation(
self, errors: SendSubmissionErrors, conference: Conference
):
multi_lingual_fields = (
"title",
"abstract",
"elevator_pitch",
)
multi_lingual_max_lengths = {
"title": 100,
"elevator_pitch": 300,
"abstract": 5000,
}
to_text = {"it": "Italian", "en": "English"}
allowed_languages = conference.languages.values_list("code", flat=True)
for language in self.languages:
if language not in allowed_languages:
errors.add_error("languages", f"Language ({language}) is not allowed")
continue
for field in multi_lingual_fields:
value = getattr(getattr(self, field), language)
max_length = multi_lingual_max_lengths.get(field, math.inf)
if not value:
errors.add_error(field, f"{to_text[language]}: Cannot be empty")
continue
if len(value) > max_length:
errors.add_error(
field,
f"{to_text[language]}: Cannot be more than {max_length} chars",
)
def validate(self, conference: Conference):
errors = SendSubmissionErrors()
if not self.tags:
errors.add_error("tags", "You need to add at least one tag")
elif len(self.tags) > 3:
errors.add_error("tags", "You can only add up to 3 tags")
if not self.speaker_level:
errors.add_error(
"speaker_level", "You need to specify what is your speaker experience"
)
elif self.speaker_level not in SubmissionModel.SPEAKER_LEVELS:
errors.add_error("speaker_level", "Select a valid choice")
if not self.languages:
errors.add_error("languages", "You need to add at least one language")
self.multi_lingual_validation(errors, conference)
max_lengths = {
"speaker_bio": 2048,
"notes": 1000,
"short_social_summary": 128,
"previous_talk_video": 2048,
"speaker_website": 2048,
"speaker_twitter_handle": 15,
"speaker_instagram_handle": 30,
"speaker_mastodon_handle": 2048,
"speaker_linkedin_url": 2048,
"speaker_facebook_url": 2048,
}
for field_name, max_length in max_lengths.items():
field = getattr(self, field_name)
if len(field) > max_length:
errors.add_error(
field_name,
f"Cannot be more than {max_length} chars",
)
duration = conference.durations.filter(id=self.duration).first()
if not conference.submission_types.filter(id=self.type).exists():
errors.add_error("type", "Not allowed submission type")
if self.topic and not conference.topics.filter(id=self.topic).exists():
errors.add_error("topic", "Not a valid topic")
if not duration:
errors.add_error(
"duration",
(
"Select a valid choice. "
"That choice is not one of the available choices."
),
)
elif not duration.allowed_submission_types.filter(id=self.type).exists():
errors.add_error(
"duration", "Duration is not an allowed for the submission type"
)
if not conference.audience_levels.filter(id=self.audience_level).exists():
errors.add_error("audience_level", "Not a valid audience level")
if not self.speaker_photo:
errors.add_error("speaker_photo", "This is required")
if self.speaker_linkedin_url and not LINKEDIN_LINK_MATCH.match(
self.speaker_linkedin_url
):
errors.add_error(
"speaker_linkedin_url", "Linkedin URL should be a linkedin.com link"
)
if self.speaker_facebook_url and not FACEBOOK_LINK_MATCH.match(
self.speaker_facebook_url
):
errors.add_error(
"speaker_facebook_url", "Facebook URL should be a facebook.com link"
)
if self.speaker_mastodon_handle and not MASTODON_HANDLE_MATCH.match(
self.speaker_mastodon_handle
):
errors.add_error(
"speaker_mastodon_handle",
"Mastodon handle should be in format: [email protected] or @[email protected] or https://instance.social/@username",
)
return errors
@strawberry.input
class SendSubmissionInput(BaseSubmissionInput):
conference: ID
title: MultiLingualInput
abstract: MultiLingualInput
languages: list[ID]
type: ID
duration: ID
elevator_pitch: MultiLingualInput
notes: str
audience_level: ID
short_social_summary: str
speaker_bio: str
speaker_photo: str
speaker_website: str
speaker_level: str
previous_talk_video: str
speaker_twitter_handle: str
speaker_instagram_handle: str
speaker_linkedin_url: str
speaker_facebook_url: str
speaker_mastodon_handle: str
speaker_availabilities: JSON
topic: Optional[ID] = strawberry.field(default=None)
tags: list[ID] = strawberry.field(default_factory=list)
@strawberry.input
class UpdateSubmissionInput(BaseSubmissionInput):
instance: ID
title: MultiLingualInput
abstract: MultiLingualInput
languages: list[ID]
type: ID
duration: ID
elevator_pitch: MultiLingualInput
notes: str
audience_level: ID
short_social_summary: str
speaker_bio: str
speaker_photo: str
speaker_website: str
speaker_level: str
previous_talk_video: str
speaker_twitter_handle: str
speaker_instagram_handle: str
speaker_linkedin_url: str
speaker_facebook_url: str
speaker_mastodon_handle: str
speaker_availabilities: JSON
topic: Optional[ID] = strawberry.field(default=None)
tags: list[ID] = strawberry.field(default_factory=list)
materials: list[SubmissionMaterialInput] = strawberry.field(default_factory=list)
def validate(self, conference: Conference, submission: SubmissionModel):
errors = super().validate(conference)
if self.materials:
if len(self.materials) > 3:
errors.add_error(
"non_field_errors", "You can only add up to 3 materials"
)
else:
for index, material in enumerate(self.materials):
with errors.with_prefix("materials", index):
material.validate(errors, submission)
return errors
SendSubmissionOutput = Annotated[
Union[Submission, SendSubmissionErrors],
strawberry.union(name="SendSubmissionOutput"),
]
UpdateSubmissionOutput = Annotated[
Union[Submission, SendSubmissionErrors],
strawberry.union(name="UpdateSubmissionOutput"),
]
@strawberry.type
class SubmissionsMutations:
@strawberry.mutation(permission_classes=[IsAuthenticated])
def update_submission(
self, info: Info, input: UpdateSubmissionInput
) -> UpdateSubmissionOutput:
request = info.context.request
instance = SubmissionModel.objects.get_by_hashid(input.instance)
if not instance.can_edit(info.context.request):
return SendSubmissionErrors.with_error(
"non_field_errors", "You cannot edit this submission"
)
conference = instance.conference
errors = input.validate(conference=conference, submission=instance)
if errors.has_errors:
return errors
input.clean()
instance.title = LazyI18nString(input.title.to_dict())
instance.abstract = LazyI18nString(input.abstract.to_dict())
instance.topic_id = input.topic
instance.type_id = input.type
instance.duration_id = input.duration
instance.elevator_pitch = LazyI18nString(input.elevator_pitch.to_dict())
instance.notes = input.notes
instance.audience_level_id = input.audience_level
instance.speaker_level = input.speaker_level
instance.previous_talk_video = input.previous_talk_video
instance.short_social_summary = input.short_social_summary
languages = Language.objects.filter(code__in=input.languages).all()
instance.languages.set(languages)
instance.tags.set(input.tags)
instance.save()
materials_to_create = []
materials_to_update = []
existing_materials = {
existing_material.id: existing_material
for existing_material in instance.materials.all()
}
for material in input.materials:
existing_material = (
existing_materials.get(int(material.id)) if material.id else None
)
if existing_material:
existing_material.name = material.name
existing_material.url = material.url
existing_material.file_id = material.file_id
materials_to_update.append(existing_material)
else:
materials_to_create.append(
ProposalMaterial(
proposal=instance,
name=material.name,
url=material.url,
file_id=material.file_id,
)
)
if to_delete := [
m.id for m in existing_materials.values() if m not in materials_to_update
]:
ProposalMaterial.objects.filter(
proposal=instance,
id__in=to_delete,
).delete()
if materials_to_create:
ProposalMaterial.objects.bulk_create(materials_to_create)
if materials_to_update:
ProposalMaterial.objects.bulk_update(
materials_to_update, fields=["name", "url", "file_id"]
)
Participant.objects.update_or_create(
user_id=request.user.id,
conference=conference,
defaults={
"bio": input.speaker_bio,
"photo_file_id": input.speaker_photo,
"website": input.speaker_website,
"speaker_level": input.speaker_level,
"previous_talk_video": input.previous_talk_video,
"twitter_handle": input.speaker_twitter_handle,
"instagram_handle": input.speaker_instagram_handle,
"linkedin_url": input.speaker_linkedin_url,
"facebook_url": input.speaker_facebook_url,
"mastodon_handle": input.speaker_mastodon_handle,
"speaker_availabilities": input.speaker_availabilities,
},
)
trigger_frontend_revalidate(conference, instance)
instance.__strawberry_definition__ = Submission.__strawberry_definition__
return instance
@strawberry.mutation(permission_classes=[IsAuthenticated])
@transaction.atomic
def send_submission(
self, info: Info, input: SendSubmissionInput
) -> SendSubmissionOutput:
request = info.context.request
conference = Conference.objects.filter(code=input.conference).first()
if not conference:
return SendSubmissionErrors.with_error("conference", "Invalid conference")
errors = input.validate(conference=conference)
if not conference.is_cfp_open:
errors.add_error("non_field_errors", "The call for paper is not open!")
if errors.has_errors:
return errors
input.clean()
instance = SubmissionModel.objects.create(
speaker_id=request.user.id,
conference=conference,
title=LazyI18nString(input.title.to_dict()),
abstract=LazyI18nString(input.abstract.to_dict()),
topic_id=input.topic,
type_id=input.type,
duration_id=input.duration,
elevator_pitch=LazyI18nString(input.elevator_pitch.to_dict()),
notes=input.notes,
audience_level_id=input.audience_level,
short_social_summary=input.short_social_summary,
)
languages = Language.objects.filter(code__in=input.languages).all()
instance.languages.set(languages)
instance.tags.set(input.tags)
Participant.objects.update_or_create(
user_id=request.user.id,
conference=conference,
defaults={
"bio": input.speaker_bio,
"photo_file_id": input.speaker_photo,
"website": input.speaker_website,
"speaker_level": input.speaker_level,
"previous_talk_video": input.previous_talk_video,
"twitter_handle": input.speaker_twitter_handle,
"instagram_handle": input.speaker_instagram_handle,
"linkedin_url": input.speaker_linkedin_url,
"facebook_url": input.speaker_facebook_url,
"mastodon_handle": input.speaker_mastodon_handle,
"speaker_availabilities": input.speaker_availabilities,
},
)
record_privacy_policy_acceptance(
info.context.request,
conference,
"cfp",
)
email_template = EmailTemplate.objects.for_conference(
conference
).get_by_identifier(EmailTemplateIdentifier.proposal_received_confirmation)
proposal_url = urljoin(
settings.FRONTEND_URL,
f"/submission/{instance.hashid}",
)
email_template.send_email(
recipient=request.user,
placeholders={
"user_name": get_name(request.user, "there"),
"proposal_title": instance.title.localize("en"),
"proposal_url": proposal_url,
},
)
def _notify_new_submission():
notify_new_cfp_submission.delay(
submission_id=instance.id,
conference_id=instance.conference_id,
admin_url=request.build_absolute_uri(instance.get_admin_url()),
)
transaction.on_commit(_notify_new_submission)
# hack because we return django models
instance.__strawberry_definition__ = Submission.__strawberry_definition__
return instance