-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathuser.py
124 lines (106 loc) · 3.3 KB
/
user.py
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
"""
© Ocado Group
Created on 19/01/2024 at 11:06:00(+00:00).
"""
import typing as t
from rest_framework import serializers
from ...serializers import ModelSerializer
from ..models import (
AnyUser,
Student,
Teacher,
User,
user_first_name_validators,
user_last_name_validators,
)
from .student import StudentSerializer
from .teacher import TeacherSerializer
RequestUser = User
# pylint: disable=missing-class-docstring
# pylint: disable=too-many-ancestors
class BaseUserSerializer(
ModelSerializer[RequestUser, AnyUser], t.Generic[AnyUser]
):
# TODO: add to model validators in new schema.
first_name = serializers.CharField(
validators=user_first_name_validators,
max_length=150,
read_only=True,
)
# TODO: add to model validators in new schema.
last_name = serializers.CharField(
validators=user_last_name_validators,
max_length=150,
read_only=True,
)
requesting_to_join_class = serializers.CharField(
source="new_student.pending_class_request",
read_only=True,
)
class Meta:
model = User
fields = [
"id",
"first_name",
"last_name",
"email",
"is_active",
"date_joined",
"requesting_to_join_class",
]
extra_kwargs = {
"id": {"read_only": True},
"first_name": {"read_only": True},
"last_name": {"read_only": True},
"email": {"read_only": True},
"is_active": {"read_only": True},
"date_joined": {"read_only": True},
}
class UserSerializer(BaseUserSerializer[AnyUser], t.Generic[AnyUser]):
student = StudentSerializer(
source="new_student",
read_only=True,
)
teacher = TeacherSerializer[Teacher](
source="new_teacher",
read_only=True,
)
class Meta(BaseUserSerializer.Meta):
fields = [*BaseUserSerializer.Meta.fields, "student", "teacher"]
def to_representation(self, instance):
try:
student = (
dict(StudentSerializer(instance.new_student).data)
if instance.new_student and instance.new_student.class_field
else None
)
except Student.DoesNotExist:
student = None
try:
requesting_to_join_class = (
instance.new_student.pending_class_request.access_code
if instance.new_student
and instance.new_student.pending_class_request
else None
)
except Student.DoesNotExist:
requesting_to_join_class = None
try:
teacher = (
dict(TeacherSerializer[Teacher](instance.new_teacher).data)
if instance.new_teacher
else None
)
except Teacher.DoesNotExist:
teacher = None
return {
"id": instance.id,
"first_name": instance.first_name,
"last_name": instance.last_name,
"email": instance.email,
"is_active": instance.is_active,
"date_joined": instance.date_joined,
"requesting_to_join_class": requesting_to_join_class,
"student": student,
"teacher": teacher,
}