-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializers.py
More file actions
425 lines (351 loc) · 13.7 KB
/
Copy pathserializers.py
File metadata and controls
425 lines (351 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.contrib.auth.password_validation import validate_password
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer
from .models import (
Position,
Role,
Member,
Section,
StudyProgram,
Application,
Reference,
)
from .send_email import send_verification_email
def get_language_from_request(request):
"""Get language from cookie defaulting to 'en'"""
return request.COOKIES.get("language", "en")
class SectionSerializer(ModelSerializer):
class Meta:
model = Section
fields = ["id", "abbreviation", "section_en", "section_sv"]
read_only_fields = ["id"]
class StudyProgramSerializer(ModelSerializer):
section = SectionSerializer(read_only=True)
class Meta:
model = StudyProgram
fields = ["id", "name_en", "name_sv", "section"]
read_only_fields = ["id"]
class SectionWithProgramsSerializer(ModelSerializer):
"""Section with nested study programs"""
programs = StudyProgramSerializer(
source="study_programs", many=True, read_only=True
)
class Meta:
model = Section
fields = ["id", "abbreviation", "section_en", "section_sv", "programs"]
read_only_fields = ["id"]
class MemberSerializer(ModelSerializer):
"""
Serializer for Member model.
This serializer handles the creation of new members and includes
functionality for email verification.
Methods
-------
create(validated_data)
Creates a new member instance, sets the password, and sends a verification email.
Also invalidates any previous verification tokens for the user.
Returns the created member instance.
"""
study_program = StudyProgramSerializer(read_only=True)
study_program_id = serializers.PrimaryKeyRelatedField(
queryset=StudyProgram.objects.all(),
source="study_program",
write_only=True,
required=False,
allow_null=True,
)
section_id = serializers.PrimaryKeyRelatedField(
queryset=Section.objects.all(),
write_only=True,
required=False,
allow_null=True,
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance is not None:
self.fields["email"].read_only = True
class Meta:
model = Member
fields = (
"name",
"phone_number",
"study_program",
"study_program_id",
"section_id",
"registration_year",
"status",
"ssn",
"email",
"password",
"is_active",
"is_staff",
"verified_email",
)
extra_kwargs = {
"password": {"write_only": True},
# Debateable if we want to expose these fields
"is_active": {"read_only": True},
"is_staff": {"read_only": True},
"verified_email": {"read_only": True},
}
def validate_password(self, value):
try:
validate_password(value)
except ValidationError as err:
raise serializers.ValidationError(err.messages)
return value
def validate(self, attrs):
section = attrs.get("section_id")
study_program = attrs.get("study_program")
if section and study_program and study_program.section_id != section.id:
raise serializers.ValidationError(
{"section_id": ["Section does not match selected program."]}
)
return attrs
def create(self, validated_data):
validated_data.pop("section_id", None)
password = validated_data.pop("password", None)
if password is None:
raise serializers.ValidationError({"password": ["Password must be set"]})
user = Member(**validated_data)
user.set_password(password)
user.save()
send_verification_email(user)
return user
class RoleDetailSerializer(ModelSerializer):
team_name = serializers.SerializerMethodField()
title = serializers.SerializerMethodField()
description = serializers.SerializerMethodField()
team_logo = serializers.ImageField(source="team.logo", read_only=True)
class Meta:
model = Role
fields = [
"team_name",
"team_logo",
"title",
"description",
"role_description_url",
"contact_email",
]
def get_team_name(self, obj):
lang = get_language_from_request(self.context.get("request"))
return obj.team.name_sv if lang == "sv" else obj.team.name_en
def get_title(self, obj):
lang = get_language_from_request(self.context.get("request"))
return obj.title_sv if lang == "sv" else obj.title_en
def get_description(self, obj):
lang = get_language_from_request(self.context.get("request"))
return obj.description_sv if lang == "sv" else obj.description_en
class PositionSerializer(ModelSerializer):
role = RoleDetailSerializer(read_only=True)
comment = serializers.SerializerMethodField()
user_app_status = serializers.SerializerMethodField()
class Meta:
model = Position
fields = [
"id",
"role",
"recruitment_start",
"recruitment_end",
"term_from",
"term_end",
"comment",
"user_app_status",
]
def get_comment(self, obj):
lang = get_language_from_request(self.context.get("request"))
return obj.comment_sv if lang == "sv" else obj.comment_eng
def get_user_app_status(self, obj):
request = self.context.get("request")
if not request or not getattr(request.user, "is_authenticated", False):
return ""
application = obj.applications.filter(member=request.user).first()
status = ""
if application:
status = (
_("In draft")
if application.status == Application.DRAFT
else _("Already applied")
)
return status
# TODO: Should be removed if we're considering django-admin for admin functionalities
class CreatePositionSerializer(ModelSerializer):
class Meta:
model = Position
fields = [
"role",
"recruitment_start",
"recruitment_end",
"appointed",
"term_from",
"term_end",
"comment_eng",
"comment_sv",
]
class ReferenceNestedSerializer(ModelSerializer):
"""Nested serializer for references in application creation"""
class Meta:
model = Reference
fields = ["name", "title", "phone_num", "email", "comment"]
extra_kwargs = {
"name": {"required": False, "allow_blank": True},
"title": {"required": False, "allow_blank": True},
"phone_num": {"required": False, "allow_blank": True},
"email": {"required": False, "allow_blank": True},
"comment": {"required": False, "allow_blank": True},
}
def validate(self, data):
"""Validate that if any field is filled, name and (email OR phone) are required"""
name = (data.get("name") or "").strip()
title = (data.get("title") or "").strip()
phone_num = (data.get("phone_num") or "").strip()
email = (data.get("email") or "").strip()
comment = (data.get("comment") or "").strip()
# Check if any field has data
has_any_field = any([name, title, phone_num, email, comment])
if has_any_field:
# name is required
if not name:
raise serializers.ValidationError(
{"name": "Name is required when providing reference information"}
)
# At least email OR phone is required
if not email and not phone_num:
raise serializers.ValidationError(
"Either email or phone number is required for a reference"
)
return data
class ListApplicationSerializer(ModelSerializer):
"""Serializer for Application model with nested position details (read-only)"""
position_details = PositionSerializer(source="position", read_only=True)
phone_number = serializers.CharField(source="member.phone_number", read_only=True)
email = serializers.CharField(source="member.email", read_only=True)
study_program = serializers.CharField(source="member.study_program", read_only=True)
references = serializers.SerializerMethodField()
class Meta:
model = Application
fields = [
"id",
"position_details",
"email",
"phone_number",
"study_program",
"status",
"cover_letter",
"qualifications",
"gdpr",
"decision_date",
"references",
]
read_only_fields = ["id", "status"]
def get_references(self, obj):
"""Get references for the application"""
references = Reference.objects.filter(application=obj)
return ReferenceNestedSerializer(references, many=True).data
class ListApplicationSerializer(ModelSerializer):
"""Serializer for Application model with nested position details (read-only)"""
position_details = PositionSerializer(source="position", read_only=True)
phone_number = serializers.CharField(source="member.phone_number", read_only=True)
email = serializers.CharField(source="member.email", read_only=True)
study_program = serializers.CharField(source="member.study_program", read_only=True)
references = serializers.SerializerMethodField()
class Meta:
model = Application
fields = [
"id",
"position_details",
"email",
"phone_number",
"study_program",
"status",
"cover_letter",
"qualifications",
"gdpr",
"decision_date",
"references",
]
read_only_fields = ["id", "status"]
def get_references(self, obj):
"""Get references for the application"""
references = Reference.objects.filter(application=obj)
return ReferenceNestedSerializer(references, many=True).data
class ApplicationSerializer(ModelSerializer):
"""Serializer for creating and updating applications with nested references"""
references = ReferenceNestedSerializer(many=True, required=False)
class Meta:
model = Application
fields = [
"id",
"position",
"cover_letter",
"qualifications",
"gdpr",
"status",
"references",
]
read_only_fields = ["id"]
def validate(self, data):
"""Validate application data"""
# check if user already has an application for this position
if not self.instance:
user = self.context["request"].user
position = data.get("position")
if Application.objects.filter(member=user, position=position).exists():
raise serializers.ValidationError(
"You have already applied for this position"
)
# prevent editing submitted applications
if self.instance and self.instance.status == Application.SUBMITTED:
raise serializers.ValidationError("Cannot edit a submitted application")
# Check if GDPR is accepted
gdpr_value = data.get("gdpr", self.instance.gdpr if self.instance else False)
if not gdpr_value:
raise serializers.ValidationError(
{"gdpr": "You must accept the GDPR policy to submit an application"}
)
# Limit references to 3 for now
references = data.get("references", [])
if references and len(references) > 3:
raise serializers.ValidationError(
{"references": "Maximum 3 references allowed"}
)
return data
def validate_status(self, value):
"""Validate status - only draft and submitted allowed"""
allowed_statuses = [Application.DRAFT, Application.SUBMITTED]
if value not in allowed_statuses:
raise serializers.ValidationError(
f"Invalid status. Allowed values: {', '.join(allowed_statuses)}"
)
return value
def create(self, validated_data):
"""Create application with current user as member and nested references"""
references_data = validated_data.pop("references", [])
validated_data["member"] = self.context["request"].user
application = super().create(validated_data)
references_to_create = [
Reference(application=application, **ref_data)
for ref_data in references_data
if ref_data.get("name")
]
if references_to_create:
Reference.objects.bulk_create(references_to_create)
return application
def update(self, instance, validated_data):
"""Update application and replace references"""
references_data = validated_data.pop("references", None)
for attr, value in validated_data.items():
setattr(instance, attr, value)
instance.save()
if references_data is not None:
Reference.objects.filter(application=instance).delete()
references_to_create = [
Reference(application=instance, **ref_data)
for ref_data in references_data
if ref_data.get("name")
]
if references_to_create:
Reference.objects.bulk_create(references_to_create)
return instance