diff --git a/lms/services/course.py b/lms/services/course.py index 8976ea1ff3..129a8135fb 100644 --- a/lms/services/course.py +++ b/lms/services/course.py @@ -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 ) } } @@ -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, @@ -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"], @@ -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( @@ -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( diff --git a/tests/unit/lms/services/course_test.py b/tests/unit/lms/services/course_test.py index fab386687e..aec5afe527 100644 --- a/tests/unit/lms/services/course_test.py +++ b/tests/unit/lms/services/course_test.py @@ -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, @@ -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, @@ -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"], @@ -227,6 +234,7 @@ def test_upsert_course( "lti_context_memberships_url", "starts_at", "ends_at", + "lms_api_course_id", ], ), call().one(),