-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmutations.py
More file actions
369 lines (307 loc) · 13 KB
/
mutations.py
File metadata and controls
369 lines (307 loc) · 13 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
from dataclasses import asdict
from enum import Enum
from typing import Annotated, Union, Optional
from participants.models import Participant
import re
from privacy_policy.record import record_privacy_policy_acceptance
import strawberry
from strawberry.types import Info
from django.db import transaction
from api.grants.types import (
AgeGroup,
Grant,
GrantType,
Occupation,
)
from api.permissions import IsAuthenticated
from api.types import BaseErrorType
from conferences.models.conference import Conference
from grants.tasks import (
notify_new_grant_reply_slack,
)
from grants.models import Grant as GrantModel
from users.models import User
from grants.tasks import get_name
from notifications.models import EmailTemplate, EmailTemplateIdentifier
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 GrantErrors(BaseErrorType):
@strawberry.type
class _GrantErrors:
instance: list[str] = strawberry.field(default_factory=list)
name: list[str] = strawberry.field(default_factory=list)
full_name: list[str] = strawberry.field(default_factory=list)
conference: list[str] = strawberry.field(default_factory=list)
age_group: list[str] = strawberry.field(default_factory=list)
gender: list[str] = strawberry.field(default_factory=list)
occupation: list[str] = strawberry.field(default_factory=list)
grant_type: list[str] = strawberry.field(default_factory=list)
python_usage: list[str] = strawberry.field(default_factory=list)
community_contribution: list[str] = strawberry.field(default_factory=list)
been_to_other_events: list[str] = strawberry.field(default_factory=list)
needs_funds_for_travel: list[str] = strawberry.field(default_factory=list)
need_visa: list[str] = strawberry.field(default_factory=list)
need_accommodation: list[str] = strawberry.field(default_factory=list)
why: list[str] = strawberry.field(default_factory=list)
notes: list[str] = strawberry.field(default_factory=list)
departure_country: list[str] = strawberry.field(default_factory=list)
nationality: list[str] = strawberry.field(default_factory=list)
departure_city: list[str] = strawberry.field(default_factory=list)
non_field_errors: list[str] = strawberry.field(default_factory=list)
participant_bio: list[str] = strawberry.field(default_factory=list)
participant_website: list[str] = strawberry.field(default_factory=list)
participant_twitter_handle: list[str] = strawberry.field(default_factory=list)
participant_instagram_handle: list[str] = strawberry.field(default_factory=list)
participant_linkedin_url: list[str] = strawberry.field(default_factory=list)
participant_facebook_url: list[str] = strawberry.field(default_factory=list)
participant_mastodon_handle: list[str] = strawberry.field(default_factory=list)
errors: _GrantErrors = None
class BaseGrantInput:
def validate(self, conference: Conference, user: User) -> GrantErrors:
errors = GrantErrors()
if not conference:
errors.add_error("conference", "Invalid conference")
if conference and not conference.is_grants_open:
errors.add_error("non_field_errors", "The grants form is not open!")
max_length_fields = {
"full_name": 300,
"name": 300,
"departure_country": 100,
"nationality": 100,
"departure_city": 100,
}
for field, max_length in max_length_fields.items():
value = getattr(self, field, "")
if value and len(value) > max_length:
errors.add_error(
field,
f"{field}: Cannot be more than {max_length} chars",
)
non_empty_fields = (
"full_name",
"python_usage",
"been_to_other_events",
"why",
"grant_type",
)
for field in non_empty_fields:
value = getattr(self, field, "")
if not value:
errors.add_error(field, f"{field}: Cannot be empty")
continue
# Validate social media fields
if self.participant_linkedin_url and not LINKEDIN_LINK_MATCH.match(
self.participant_linkedin_url
):
errors.add_error(
"participant_linkedin_url", "Linkedin URL should be a linkedin.com link"
)
if self.participant_facebook_url and not FACEBOOK_LINK_MATCH.match(
self.participant_facebook_url
):
errors.add_error(
"participant_facebook_url", "Facebook URL should be a facebook.com link"
)
if self.participant_mastodon_handle and not MASTODON_HANDLE_MATCH.match(
self.participant_mastodon_handle
):
errors.add_error(
"participant_mastodon_handle",
"Mastodon handle should be in format: username@instance.social or @username@instance.social or https://instance.social/@username",
)
return errors
@strawberry.input
class SendGrantInput(BaseGrantInput):
name: str
full_name: str
conference: strawberry.ID
age_group: AgeGroup
gender: str
occupation: Occupation
grant_type: list[GrantType]
python_usage: str
been_to_other_events: str
community_contribution: str
needs_funds_for_travel: bool
need_visa: bool
need_accommodation: bool
why: str
notes: str
departure_country: str | None = None
nationality: str
departure_city: str | None = None
participant_bio: str
participant_website: str
participant_twitter_handle: str
participant_instagram_handle: str
participant_linkedin_url: str
participant_facebook_url: str
participant_mastodon_handle: str
def validate(self, conference: Conference, user: User) -> GrantErrors | None:
errors = super().validate(conference=conference, user=user)
if GrantModel.objects.of_user(user).for_conference(conference).exists():
errors.add_error("non_field_errors", "Grant already submitted!")
return errors.if_has_errors
@strawberry.input
class UpdateGrantInput(BaseGrantInput):
instance: strawberry.ID
name: str
full_name: str
conference: strawberry.ID
age_group: AgeGroup
gender: str
occupation: Occupation
grant_type: list[GrantType]
python_usage: str
been_to_other_events: str
community_contribution: str
needs_funds_for_travel: bool
need_visa: bool
need_accommodation: bool
why: str
notes: str
departure_country: str | None = None
nationality: str
departure_city: str | None = None
participant_bio: str
participant_website: str
participant_twitter_handle: str
participant_instagram_handle: str
participant_linkedin_url: str
participant_facebook_url: str
participant_mastodon_handle: str
def validate(self, conference: Conference, user: User) -> GrantErrors | None:
return super().validate(conference=conference, user=user).if_has_errors
SendGrantResult = Annotated[
Union[Grant, GrantErrors], strawberry.union(name="SendGrantResult")
]
UpdateGrantResult = Annotated[
Union[Grant, GrantErrors], strawberry.union(name="UpdateGrantResult")
]
@strawberry.enum
class StatusOption(Enum):
confirmed = "confirmed"
refused = "refused"
def to_grant_status(self) -> GrantModel.Status:
return GrantModel.Status(self.name)
@strawberry.input
class SendGrantReplyInput:
instance: strawberry.ID
status: Optional[StatusOption]
@strawberry.type
class SendGrantReplyError:
message: str
SendGrantReplyResult = Annotated[
Union[Grant, SendGrantReplyError], strawberry.union(name="SendGrantReplyResult")
]
@strawberry.type
class GrantMutation:
@strawberry.mutation(permission_classes=[IsAuthenticated])
@transaction.atomic
def send_grant(self, info: Info, input: SendGrantInput) -> SendGrantResult:
request = info.context.request
conference = Conference.objects.filter(code=input.conference).first()
if errors := input.validate(conference=conference, user=request.user):
return errors
instance = GrantModel.objects.create(
**{
"user_id": request.user.id,
"conference": conference,
"name": input.name,
"full_name": input.full_name,
"age_group": input.age_group,
"gender": input.gender,
"occupation": input.occupation,
"grant_type": input.grant_type,
"python_usage": input.python_usage,
"been_to_other_events": input.been_to_other_events,
"community_contribution": input.community_contribution,
"needs_funds_for_travel": input.needs_funds_for_travel,
"need_visa": input.need_visa,
"need_accommodation": input.need_accommodation,
"why": input.why,
"notes": input.notes,
"departure_country": input.departure_country,
"nationality": input.nationality,
"departure_city": input.departure_city,
}
)
record_privacy_policy_acceptance(
info.context.request,
conference,
"grant",
)
Participant.objects.update_or_create(
user_id=request.user.id,
conference=instance.conference,
defaults={
"bio": input.participant_bio,
"website": input.participant_website,
"twitter_handle": input.participant_twitter_handle,
"instagram_handle": input.participant_instagram_handle,
"linkedin_url": input.participant_linkedin_url,
"facebook_url": input.participant_facebook_url,
"mastodon_handle": input.participant_mastodon_handle,
},
)
email_template = EmailTemplate.objects.for_conference(
conference
).get_by_identifier(EmailTemplateIdentifier.grant_application_confirmation)
email_template.send_email(
recipient=request.user,
placeholders={
"user_name": get_name(request.user, "there"),
},
)
# hack because we return django models
instance.__strawberry_definition__ = Grant.__strawberry_definition__
return instance
@strawberry.mutation(permission_classes=[IsAuthenticated])
def update_grant(self, info: Info, input: UpdateGrantInput) -> UpdateGrantResult:
request = info.context.request
instance = GrantModel.objects.get(id=input.instance)
if not instance.can_edit(request.user):
return GrantErrors.with_error(
"non_field_errors", "You cannot edit this grant"
)
input.conference = instance.conference
if errors := input.validate(conference=input.conference, user=request.user):
return errors
for attr, value in asdict(input).items():
setattr(instance, attr, value)
instance.save()
Participant.objects.update_or_create(
user_id=request.user.id,
conference=instance.conference,
defaults={
"bio": input.participant_bio,
"website": input.participant_website,
"twitter_handle": input.participant_twitter_handle,
"instagram_handle": input.participant_instagram_handle,
"linkedin_url": input.participant_linkedin_url,
"facebook_url": input.participant_facebook_url,
"mastodon_handle": input.participant_mastodon_handle,
},
)
instance.__strawberry_definition__ = Grant.__strawberry_definition__
return instance
@strawberry.mutation(permission_classes=[IsAuthenticated])
def send_grant_reply(
self, info: Info, input: SendGrantReplyInput
) -> SendGrantReplyResult:
request = info.context.request
grant = GrantModel.objects.get(id=input.instance)
if not grant.can_edit(request.user):
return SendGrantReplyError(message="You cannot reply to this grant")
# Can't modify the status if the grant is still pending or was already rejected
if grant.status in (GrantModel.Status.pending, GrantModel.Status.rejected):
return SendGrantReplyError(message="You cannot reply to this grant")
grant.status = input.status.to_grant_status()
grant.save()
admin_url = request.build_absolute_uri(grant.get_admin_url())
notify_new_grant_reply_slack.delay(grant_id=grant.id, admin_url=admin_url)
return Grant.from_model(grant)