-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserializers.py
195 lines (157 loc) · 6.12 KB
/
serializers.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
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
from textwrap import dedent
from django.db.models import Q, Subquery
from rest_framework import serializers
from courses.models import Course
from courses.util import get_current_semester
from degree.models import (
Degree,
DegreePlan,
DockedCourse,
DoubleCountRestriction,
Fulfillment,
Rule,
)
class DegreeListSerializer(serializers.ModelSerializer):
class Meta:
model = Degree
fields = "__all__"
class SimpleCourseSerializer(serializers.ModelSerializer):
id = serializers.ReadOnlyField(
source="full_code",
help_text=dedent(
"""
The full code of the course, in the form '{dept code}-{course code}'
dash-joined department and code of the course, e.g. `CIS-120` for CIS-120."""
),
)
course_quality = serializers.DecimalField(
max_digits=4, decimal_places=3, read_only=True, help_text="course_quality_help"
)
difficulty = serializers.DecimalField(
max_digits=4, decimal_places=3, read_only=True, help_text="difficulty_help"
)
instructor_quality = serializers.DecimalField(
max_digits=4,
decimal_places=3,
read_only=True,
help_text="instructor_quality_help",
)
work_required = serializers.DecimalField(
max_digits=4, decimal_places=3, read_only=True, help_text="work_required_help"
)
class Meta:
model = Course
fields = [
"id",
"title",
"credits",
"semester",
"course_quality",
"instructor_quality",
"difficulty",
"work_required",
]
read_only_fields = fields
class RuleSerializer(serializers.ModelSerializer):
q_json = serializers.ReadOnlyField(help_text="JSON representation of the q object")
class Meta:
model = Rule
fields = "__all__"
# Allow recursive serialization of rules
RuleSerializer._declared_fields["rules"] = RuleSerializer(
many=True, read_only=True, source="children"
)
class DoubleCountRestrictionSerializer(serializers.ModelSerializer):
class Meta:
model = DoubleCountRestriction
fields = "__all__"
class DegreeDetailSerializer(serializers.ModelSerializer):
rules = RuleSerializer(many=True, read_only=True)
double_count_restrictions = DoubleCountRestrictionSerializer(many=True, read_only=True)
class Meta:
model = Degree
fields = "__all__"
class FulfillmentSerializer(serializers.ModelSerializer):
course = serializers.SerializerMethodField()
def get_course(self, obj):
course = (
Course.with_reviews.filter(
full_code=obj.full_code, semester__lte=get_current_semester()
)
.order_by("-semester")
.first()
)
if course is not None:
return SimpleCourseSerializer(course).data
return None
rules = serializers.PrimaryKeyRelatedField(
many=True, queryset=Rule.objects.all(), required=False
)
def to_internal_value(self, data):
"""
Allow for this route to be nested under the degreeplan viewset.
"""
data = data.copy()
data["degree_plan"] = self.context["view"].get_degree_plan_id()
return super().to_internal_value(data)
class Meta:
model = Fulfillment
fields = ["id", "degree_plan", "full_code", "course", "semester", "rules"]
def validate(self, data):
data = super().validate(data)
rules = data.get("rules") # for patch requests without a rules field
full_code = data.get("full_code")
degree_plan = data.get("degree_plan")
if rules is None and full_code is None and degree_plan is None:
return data # Nothing to validate
if rules is None and self.instance is not None:
rules = self.instance.rules.all()
elif rules is None:
rules = []
if full_code is None:
full_code = self.instance.full_code
if degree_plan is None:
degree_plan = self.instance.degree_plan
# TODO: check that rules belong to this degree plan
for rule in rules:
# NOTE: we don't do any validation if the course doesn't exist in DB. In future,
# it may be better to prompt user for manual override
if Course.objects.filter(full_code=full_code).exists():
satisfying_courses = Course.objects.filter(rule.get_q_object())
if not (
Course.objects.filter(
full_code=full_code,
topic_id__in=Subquery(satisfying_courses.values("topic_id")),
).exists()
):
raise serializers.ValidationError(
f"Course {full_code} does not satisfy rule {rule.id}"
)
# Check for double count restrictions
double_count_restrictions = DoubleCountRestriction.objects.filter(
Q(rule__in=rules) | Q(other_rule__in=rules)
)
for restriction in double_count_restrictions:
if restriction.is_double_count_violated(degree_plan):
raise serializers.ValidationError(
f"Double count restriction {restriction.id} violated"
)
return data
class DegreePlanListSerializer(serializers.ModelSerializer):
class Meta:
model = DegreePlan
fields = ["id", "name", "created_at", "updated_at"]
class DegreePlanDetailSerializer(serializers.ModelSerializer):
degrees = DegreeDetailSerializer(
many=True, help_text="The degrees belonging to this degree plan"
)
person = serializers.HiddenField(default=serializers.CurrentUserDefault())
class Meta:
model = DegreePlan
fields = ["id", "name", "degrees", "person", "created_at", "updated_at"]
class DockedCourseSerializer(serializers.ModelSerializer):
id = serializers.ReadOnlyField(help_text="The id of the docked course")
person = serializers.HiddenField(default=serializers.CurrentUserDefault())
class Meta:
model = DockedCourse
fields = ["full_code", "id", "person"]