-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserializers.py
More file actions
341 lines (282 loc) · 10.1 KB
/
serializers.py
File metadata and controls
341 lines (282 loc) · 10.1 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
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from rest_framework import serializers
from core.models import Skill, SkillToObject
from core.serializers import SkillToObjectSerializer
from core.services import get_views_count
from files.models import UserFile
from files.serializers import UserFileSerializer
from projects.models import Project
from projects.validators import validate_project
from users.serializers import UserDetailSerializer
from vacancy.constants import WorkExperience, WorkFormat, WorkSchedule
from vacancy.models import Vacancy, VacancyResponse
User = get_user_model()
class RequiredSkillsSerializerMixin(serializers.Serializer):
required_skills = SkillToObjectSerializer(many=True, read_only=True)
class RequiredSkillsWriteSerializerMixin(RequiredSkillsSerializerMixin):
required_skills_ids = serializers.ListField(
child=serializers.IntegerField(), write_only=True, required=False
)
class AbstractVacancyEnumFields(serializers.Serializer):
required_experience = serializers.CharField(allow_null=True)
work_schedule = serializers.CharField(allow_null=True)
work_format = serializers.CharField(allow_null=True)
def validate_required_experience(self, value):
try:
return WorkExperience.from_display(value)
except ValueError as e:
raise serializers.ValidationError(str(e))
def validate_work_schedule(self, value):
try:
return WorkSchedule.from_display(value)
except ValueError as e:
raise serializers.ValidationError(str(e))
def validate_work_format(self, value):
try:
return WorkFormat.from_display(value)
except ValueError as e:
raise serializers.ValidationError(str(e))
def to_representation(self, instance):
representation = super().to_representation(instance)
representation["required_experience"] = WorkExperience.to_display(
instance.required_experience
)
representation["work_schedule"] = WorkSchedule.to_display(
instance.work_schedule
)
representation["work_format"] = WorkFormat.to_display(instance.work_format)
return representation
class AbstractVacancyReadOnlyFields(serializers.Serializer):
"""Abstract read-only fields for Vacancy."""
datetime_closed = serializers.DateTimeField(read_only=True)
response_count = serializers.SerializerMethodField(read_only=True)
def get_response_count(self, obj):
"""Returns count non status responses."""
return obj.vacancy_requests.filter(is_approved=None).count()
class ProjectVacancyListSerializer(
serializers.ModelSerializer,
AbstractVacancyReadOnlyFields,
RequiredSkillsSerializerMixin[Vacancy],
):
class Meta:
model = Vacancy
fields = [
"id",
"role",
"required_skills",
"description",
"project",
"is_active",
"datetime_closed",
"response_count",
]
class ProjectForVacancySerializer(serializers.ModelSerializer[Project]):
class Meta:
model = Project
fields = [
"id",
"name",
"description",
"image_address",
"is_company",
]
class VacancyDetailSerializer(
serializers.ModelSerializer,
AbstractVacancyReadOnlyFields,
AbstractVacancyEnumFields,
RequiredSkillsWriteSerializerMixin[Vacancy],
):
project = ProjectForVacancySerializer(many=False, read_only=True)
class Meta:
model = Vacancy
fields = [
"id",
"role",
"specialization",
"required_skills",
"required_skills_ids",
"description",
"project",
"is_active",
"datetime_created",
"datetime_updated",
"datetime_closed",
"response_count",
"required_experience",
"work_schedule",
"work_format",
"salary",
]
read_only_fields = ["project"]
class VacancyListSerializer(
serializers.ModelSerializer,
RequiredSkillsSerializerMixin[Vacancy],
AbstractVacancyReadOnlyFields,
):
class Meta:
model = Vacancy
fields = [
"id",
"role",
"specialization",
"required_skills",
"description",
"is_active",
"datetime_closed",
"response_count",
]
read_only_fields = [
"project",
]
# TODO FIX This (Copied serializer from projects) - hotfix: rename
class ProjectListSerializer_TODO_FIX(serializers.ModelSerializer):
views_count = serializers.SerializerMethodField(method_name="count_views")
short_description = serializers.SerializerMethodField()
@classmethod
def count_views(cls, project):
return get_views_count(project)
@classmethod
def get_short_description(cls, project):
return project.get_short_description()
class Meta:
model = Project
fields = [
"id",
"name",
"leader",
"short_description",
"image_address",
"industry",
"views_count",
"is_company",
]
read_only_fields = ["leader", "views_count", "is_company"]
def is_valid(self, *, raise_exception=False):
return super().is_valid(raise_exception=raise_exception)
def validate(self, data):
super().validate(data)
return validate_project(data)
class ProjectVacancyCreateListSerializer(
serializers.ModelSerializer,
AbstractVacancyReadOnlyFields,
AbstractVacancyEnumFields,
RequiredSkillsWriteSerializerMixin[Vacancy],
):
def create(self, validated_data):
project = validated_data["project"]
if project.leader != self.context["request"].user:
raise serializers.ValidationError("You are not the leader of the project")
required_skills_ids = validated_data.pop("required_skills_ids")
if validated_data["project"].draft:
validated_data["is_active"] = False
else:
validated_data["is_active"] = True
vacancy = Vacancy.objects.create(**validated_data)
for skill_id in required_skills_ids:
try:
skill = Skill.objects.get(id=skill_id)
except Skill.DoesNotExist:
raise serializers.ValidationError(
f"Skill with id {skill_id} does not exist"
)
SkillToObject.objects.create(
skill=skill,
content_type=ContentType.objects.get_for_model(Vacancy),
object_id=vacancy.id,
)
return vacancy
def to_representation(self, instance):
ret = super().to_representation(instance)
ret["project"] = ProjectListSerializer_TODO_FIX(instance.project).data
return ret
class Meta:
model = Vacancy
fields = [
"id",
"role",
"specialization",
"required_skills",
"required_skills_ids",
"description",
"project",
"is_active",
"datetime_closed",
"response_count",
"required_experience",
"work_schedule",
"work_format",
"salary",
]
class VacancyResponseListSerializer(serializers.ModelSerializer):
is_approved = serializers.BooleanField(read_only=True)
user = UserDetailSerializer(read_only=True)
user_id = serializers.IntegerField(write_only=True)
accompanying_file = serializers.SlugRelatedField(
slug_field="link",
queryset=UserFile.objects.all(),
required=False,
allow_null=True,
)
vacancy_role = serializers.SerializerMethodField()
class Meta:
model = VacancyResponse
fields = [
"id",
"user",
"user_id",
"why_me",
"accompanying_file",
"is_approved",
"vacancy",
"vacancy_role",
]
extra_kwargs = {
"user_id": {"write_only": True},
}
def get_vacancy_role(self, obj: VacancyResponse) -> str:
return obj.vacancy.role
def validate(self, attrs):
vacancy = attrs["vacancy"]
user = self.validate_user_exists(attrs["user_id"])
if VacancyResponse.objects.filter(vacancy=vacancy, user=user).exists():
raise serializers.ValidationError("Vacancy response already exists")
return attrs
def validate_user_exists(self, value):
try:
user = User.objects.get(pk=value)
except User.DoesNotExist:
raise serializers.ValidationError("User does not exist")
return user
def create(self, validated_data):
user_id = validated_data.pop("user_id")
user = User.objects.get(pk=user_id)
vacancy_response = VacancyResponse.objects.create(user=user, **validated_data)
return vacancy_response
class VacancyResponseFullFileInfoListSerializer(VacancyResponseListSerializer):
"""Returns full file info."""
accompanying_file = UserFileSerializer(read_only=True)
class VacancyResponseDetailSerializer(serializers.ModelSerializer[VacancyResponse]):
user = UserDetailSerializer(many=False, read_only=True)
vacancy = VacancyListSerializer(many=False, read_only=True)
is_approved = serializers.BooleanField(read_only=True)
accompanying_file = serializers.SlugRelatedField(
slug_field="link",
queryset=UserFile.objects.all(),
required=False,
allow_null=True,
)
class Meta:
model = VacancyResponse
fields = [
"id",
"user",
"vacancy",
"why_me",
"accompanying_file",
"is_approved",
"datetime_created",
"datetime_updated",
]
class VacancyResponseAcceptSerializer(VacancyResponseDetailSerializer[VacancyResponse]):
is_approved = serializers.BooleanField(required=True, read_only=False)