Skip to content

Commit cea7923

Browse files
Opeyem1aclaude
andcommitted
fix: scope Canvas student import to the course
upsert_course_member looked members up by lms_id alone, which is only unique within a course. A Canvas user with records in two courses made import raise MultipleObjectsReturned (500); with one record elsewhere, import silently moved it into this course and cleared its linked user. Look up by (course, lms_id), and leave the user and role of existing members to the LTI launch. Also parse sortable_name without assuming exactly one comma. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 183a36c commit cea7923

3 files changed

Lines changed: 58 additions & 6 deletions

File tree

‎src/app/canvas/import_students.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ def import_students_from_canvas(request, course: Course):
5151
]
5252

5353
for student in students:
54-
last_name, first_name = student.user["sortable_name"].split(",")
54+
# sortable_name is "Last, First", but may have no comma (or several)
55+
last_name, _, first_name = student.user["sortable_name"].partition(",")
5556

5657
CourseMember.upsert_course_member(
5758
user_id=None,
58-
first_name=first_name,
59-
last_name=last_name,
59+
first_name=first_name.strip(),
60+
last_name=last_name.strip(),
6061
lms_id=student.user["id"],
6162
sis_user_id=(
6263
student.sis_user_id

‎src/app/models/course_member.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ def upsert_course_member(
106106
course_id: str,
107107
role: UserRole,
108108
):
109+
# lms_id is only unique within a course. The linked user and role are
110+
# owned by the LTI launch, so an existing member keeps them.
109111
course_member, created = cls.objects.update_or_create(
112+
course_id=course_id,
110113
lms_id=lms_id,
111114
defaults={
112-
"user_id": user_id,
113115
"first_name": first_name,
114116
"last_name": last_name,
115-
"course_id": course_id,
116-
"role": role,
117117
"sis_user_id": sis_user_id,
118118
},
119119
create_defaults={

‎src/app/tests.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from django.test import TestCase
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 UpsertCourseMemberTests(TestCase):
10+
def setUp(self):
11+
org = Organization.objects.create(name="UBC")
12+
self.course_a = Course.objects.create(name="A", organization=org)
13+
self.course_b = Course.objects.create(name="B", organization=org)
14+
15+
def upsert(self, course, lms_id="42", first_name="Ada"):
16+
return CourseMember.upsert_course_member(
17+
user_id=None,
18+
first_name=first_name,
19+
last_name="Lovelace",
20+
lms_id=lms_id,
21+
sis_user_id=None,
22+
course_id=course.pk,
23+
role=UserRole.STUDENT,
24+
)
25+
26+
def test_same_lms_id_in_two_courses_creates_separate_members(self):
27+
self.upsert(self.course_a)
28+
self.upsert(self.course_b)
29+
30+
self.assertEqual(self.course_a.course_members.count(), 1)
31+
self.assertEqual(self.course_b.course_members.count(), 1)
32+
33+
# Re-importing must not fail now that the lms_id exists twice
34+
_, created = self.upsert(self.course_b, first_name="Augusta")
35+
self.assertFalse(created)
36+
self.assertEqual(
37+
self.course_b.course_members.get().first_name, "Augusta"
38+
)
39+
self.assertEqual(self.course_a.course_members.get().first_name, "Ada")
40+
41+
def test_existing_member_keeps_user_and_role(self):
42+
user = MyUser.objects.create(username="42+canvas")
43+
CourseMember.objects.create(
44+
user=user, course=self.course_a, lms_id="42", role=UserRole.INSTRUCTOR
45+
)
46+
47+
self.upsert(self.course_a)
48+
49+
member = self.course_a.course_members.get()
50+
self.assertEqual(member.user, user)
51+
self.assertEqual(member.role, UserRole.INSTRUCTOR)

0 commit comments

Comments
 (0)