-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubmissions.py
More file actions
78 lines (71 loc) · 2.03 KB
/
Copy pathsubmissions.py
File metadata and controls
78 lines (71 loc) · 2.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
from rest_framework import serializers
from partner_programs.models import Submission
class SubmissionSerializer(serializers.ModelSerializer):
create_fields = frozenset(
{
"title",
"description",
"form_data",
"links",
"stage_key",
"version",
}
)
update_fields = frozenset(
{
"title",
"description",
"form_data",
"links",
}
)
class Meta:
model = Submission
fields = (
"id",
"application",
"program",
"submitted_by",
"title",
"description",
"form_data",
"links",
"status",
"stage_key",
"version",
"submitted_at",
"created_at",
"updated_at",
)
read_only_fields = (
"id",
"application",
"program",
"submitted_by",
"status",
"submitted_at",
"created_at",
"updated_at",
)
extra_kwargs = {
"description": {"required": False},
"form_data": {"required": False},
"links": {"required": False},
"stage_key": {"required": False},
"version": {"required": False, "min_value": 1},
}
def validate(self, attrs):
allowed_fields = self.update_fields if self.instance else self.create_fields
unsupported_fields = set(self.initial_data).difference(allowed_fields)
if unsupported_fields:
raise serializers.ValidationError(
{
field: "This field is read-only."
for field in sorted(unsupported_fields)
}
)
if self.instance and not self.instance.can_edit:
raise serializers.ValidationError(
{"status": "Only draft or returned submissions can be updated."}
)
return attrs