-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplications.py
More file actions
103 lines (93 loc) · 2.92 KB
/
Copy pathapplications.py
File metadata and controls
103 lines (93 loc) · 2.92 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
from rest_framework import serializers
from partner_programs.models import Application
from projects.models import Project
class ApplicationSerializer(serializers.ModelSerializer):
project = serializers.PrimaryKeyRelatedField(
queryset=Project.objects.all(),
allow_null=True,
required=False,
)
project_id = serializers.PrimaryKeyRelatedField(
source="project",
queryset=Project.objects.all(),
allow_null=True,
required=False,
write_only=True,
)
immutable_input_fields = frozenset(
{
"id",
"program",
"user",
"created_by",
"status",
"submitted_at",
"approved_at",
"rejected_at",
"withdrawn_at",
"created_at",
"updated_at",
}
)
class Meta:
model = Application
fields = (
"id",
"program",
"user",
"created_by",
"status",
"form_data",
"project",
"project_id",
"submitted_at",
"approved_at",
"rejected_at",
"withdrawn_at",
"created_at",
"updated_at",
)
read_only_fields = (
"id",
"program",
"user",
"created_by",
"status",
"submitted_at",
"approved_at",
"rejected_at",
"withdrawn_at",
"created_at",
"updated_at",
)
extra_kwargs = {
"form_data": {"required": False},
}
def validate(self, attrs):
supplied_immutable_fields = self.immutable_input_fields.intersection(
self.initial_data
)
if supplied_immutable_fields:
raise serializers.ValidationError(
{
field: "This field is read-only."
for field in sorted(supplied_immutable_fields)
}
)
if "project" in self.initial_data and "project_id" in self.initial_data:
raise serializers.ValidationError(
{"project": "Use either project or project_id, not both."}
)
if self.instance and self.instance.status != Application.STATUS_DRAFT:
raise serializers.ValidationError(
{"status": "Only draft applications can be updated."}
)
project = attrs.get("project", serializers.empty)
if project is not serializers.empty and project is not None:
request = self.context.get("request")
user = getattr(request, "user", None)
if not user or not user.is_authenticated or project.leader_id != user.pk:
raise serializers.ValidationError(
{"project": "You can only use a project that you lead."}
)
return attrs