Skip to content

Commit

Permalink
Store the API ID for canvas courses on LMSCourse
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospri committed Jan 13, 2025
1 parent f56c6b4 commit aa60cb0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lms/services/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def get_from_launch(self, product_family: Family, lti_params) -> Course:
if product_family == Family.CANVAS:
extra = {
"canvas": {
"custom_canvas_course_id": lti_params.get(
"custom_canvas_course_id"
"custom_canvas_course_id": self._get_api_id_from_launch(
lti_params
)
}
}
Expand Down Expand Up @@ -315,6 +315,7 @@ def _upsert_lms_course(self, course: Course, lti_params: LTIParams) -> LMSCourse
).get("context_memberships_url")

course_starts_at, course_ends_at = self._get_course_dates(lti_params)
lms_api_course_id = self._get_api_id_from_launch(lti_params)

lms_course = bulk_upsert(
self._db,
Expand All @@ -328,6 +329,7 @@ def _upsert_lms_course(self, course: Course, lti_params: LTIParams) -> LMSCourse
"lti_context_memberships_url": lti_context_membership_url,
"starts_at": course_starts_at,
"ends_at": course_ends_at,
"lms_api_course_id": lms_api_course_id,
}
],
index_elements=["h_authority_provided_id"],
Expand All @@ -337,6 +339,7 @@ def _upsert_lms_course(self, course: Course, lti_params: LTIParams) -> LMSCourse
"lti_context_memberships_url",
"starts_at",
"ends_at",
"lms_api_course_id",
],
).one()
bulk_upsert(
Expand Down Expand Up @@ -444,6 +447,15 @@ def _get_course_dates(

return course_starts_at, course_ends_at

def _get_api_id_from_launch(self, lti_params: LTIParams) -> str | None:
"""Get the API ID from the launch params.
In this context API ID applies to the LMS specific propietary API ID as opposed to any LTI ID.
Canvas only for now.
"""
return lti_params.get("custom_canvas_course_id")


def course_service_factory(_context, request):
return CourseService(
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/lms/services/course_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ def test_get_from_launch_when_new_and_historical_course_exists(
"custom_course_ends, course_ends_at",
[(None, None), ("2022-01-01T00:00:00Z", datetime(2022, 1, 1, tzinfo=UTC))],
)
@pytest.mark.parametrize(
"custom_canvas_api_id",
[None, "API ID"],
)
def test_upsert_course(
self,
svc,
Expand All @@ -180,9 +184,11 @@ def test_upsert_course(
course_starts_at,
custom_course_ends,
course_ends_at,
custom_canvas_api_id,
):
lti_params["custom_course_starts"] = custom_course_starts
lti_params["custom_course_ends"] = custom_course_ends
lti_params["custom_canvas_course_id"] = custom_canvas_api_id

course = svc.upsert_course(
lti_params=lti_params,
Expand Down Expand Up @@ -218,6 +224,7 @@ def test_upsert_course(
"lti_context_memberships_url": None,
"starts_at": course_starts_at,
"ends_at": course_ends_at,
"lms_api_course_id": custom_canvas_api_id,
}
],
index_elements=["h_authority_provided_id"],
Expand All @@ -227,6 +234,7 @@ def test_upsert_course(
"lti_context_memberships_url",
"starts_at",
"ends_at",
"lms_api_course_id",
],
),
call().one(),
Expand Down

0 comments on commit aa60cb0

Please sign in to comment.