Skip to content

Commit 2187cef

Browse files
Opeyem1aclaude
andcommitted
fix: serialize course students as course members
864a521 changed CourseViewSet.get_serializer_class to return CourseViewSerializer instead of deferring to super(), which overrode serializer_class on @action routes. The students endpoint then serialized each CourseMember with the Course serializer, returning only id, name and sections, so the Students table showed "undefined" names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 183a36c commit 2187cef

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

‎src/app/test_course_views.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from rest_framework.test import APITestCase
2+
3+
from accounts.models import MyUser
4+
from app.models.course import Course
5+
from app.models.course_member import CourseMember, UserRole
6+
from app.models.organization import Organization
7+
8+
9+
class CourseStudentsEndpointTests(APITestCase):
10+
def setUp(self):
11+
org = Organization.objects.create(name="UBC")
12+
self.course = Course.objects.create(name="BIOC 202", organization=org)
13+
instructor = MyUser.objects.create(username="instructor")
14+
CourseMember.objects.create(
15+
user=instructor, course=self.course, role=UserRole.INSTRUCTOR
16+
)
17+
CourseMember.objects.create(
18+
course=self.course,
19+
role=UserRole.STUDENT,
20+
first_name="Ada",
21+
last_name="Lovelace",
22+
lms_id="42",
23+
sis_user_id="1234",
24+
)
25+
self.client.force_authenticate(instructor)
26+
27+
def test_students_are_serialized_as_course_members(self):
28+
response = self.client.get(f"/api/v1/courses/{self.course.pk}/students/")
29+
30+
self.assertEqual(response.status_code, 200)
31+
student = response.json()["results"][0]
32+
self.assertEqual(student["first_name"], "Ada")
33+
self.assertEqual(student["last_name"], "Lovelace")
34+
self.assertEqual(student["sis_user_id"], "1234")

‎src/app/views/course.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,11 @@ class CourseViewSet(
8585
serializer_class = CourseViewSerializer
8686
permission_classes = [IsCourseInstructor]
8787

88-
def get_serializer_class(
89-
self,
90-
) -> Type[CourseUpdateSerializer | CourseViewSerializer]:
88+
def get_serializer_class(self) -> Type[serializers.BaseSerializer]:
9189
if self.action in ["update", "partial_update"]:
9290
return CourseUpdateSerializer
93-
return CourseViewSerializer
91+
# Defer to super() so @action(serializer_class=...) is respected
92+
return super().get_serializer_class()
9493

9594
@action(
9695
detail=True,

0 commit comments

Comments
 (0)