-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserializers.py
More file actions
92 lines (80 loc) · 3.03 KB
/
serializers.py
File metadata and controls
92 lines (80 loc) · 3.03 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
from django.apps import apps
from rest_framework import serializers
from invites.models import Invite
from projects.models import Collaborator
from projects.serializers import ProjectListSerializer
from users.serializers import UserDetailSerializer
class InviteListSerializer(serializers.ModelSerializer[Invite]):
class Meta:
model = Invite
fields = [
"id",
"project",
"user",
"motivational_letter",
"role",
"specialization",
"is_accepted",
]
def validate(self, attrs):
project = attrs["project"]
user = attrs["user"]
if project.leader_id == user.id:
raise serializers.ValidationError(
{"user": "Пользователь уже является лидером проекта."}
)
if Collaborator.objects.filter(project=project, user=user).exists():
raise serializers.ValidationError(
{"user": "Пользователь уже состоит в проекте."}
)
if Invite.objects.filter(
project=project, user=user, is_accepted__isnull=True
).exists():
raise serializers.ValidationError(
{"user": "У пользователя уже есть активное приглашение в этот проект."}
)
link = project.program_links.select_related("partner_program").first()
if link:
PartnerProgramUserProfile = apps.get_model(
"partner_programs", "PartnerProgramUserProfile"
)
is_participant = PartnerProgramUserProfile.objects.filter(
user_id=user.id,
partner_program_id=link.partner_program_id,
).exists()
if not is_participant:
raise serializers.ValidationError(
{
"user": (
"Нельзя пригласить пользователя: проект относится к программе, "
"а пользователь не является её участником."
)
}
)
return attrs
class InviteDetailSerializer(serializers.ModelSerializer[Invite]):
user = UserDetailSerializer(many=False, read_only=True)
project = ProjectListSerializer(many=False, read_only=True)
specialization = serializers.CharField(
required=False, allow_null=True, allow_blank=True
)
class Meta:
model = Invite
fields = [
"id",
"project",
"user",
"motivational_letter",
"role",
"specialization",
"is_accepted",
"datetime_created",
"datetime_updated",
]
read_only_fields = [
"project",
"user",
"is_accepted",
"datetime_created",
"datetime_updated",
]