diff --git a/enterprise_access/apps/content_assignments/api.py b/enterprise_access/apps/content_assignments/api.py index 419a94bc4..32dc17417 100644 --- a/enterprise_access/apps/content_assignments/api.py +++ b/enterprise_access/apps/content_assignments/api.py @@ -150,6 +150,9 @@ def _normalize_course_key(course_key_str: str) -> str: else: return course_key_str +def _normalize_course_key_from_metadata(assignment_configuration, content_key): + content_summary = _get_content_summary(assignment_configuration, content_key) + return content_summary.get('content_key') def get_assignment_for_learner( assignment_configuration, @@ -187,15 +190,18 @@ def get_assignment_for_learner( constraint across [assignment_configuration,lms_user_id,content_key]. BUT still technically possible if internal staff managed to create a duplicate assignment configuration for a single enterprise. """ - content_key_to_match = None - # Whatever the requested content_key is, normalize it to a course with no namespace prefix. - try: - requested_course_run_locator = CourseKey.from_string(content_key) - # No exception raised, content_key represents a course run, so convert it to a course. - content_key_to_match = _get_course_key_from_locator(requested_course_run_locator) - except InvalidKeyError: - # Either the key was already a course key (no problem), or it was something else (weird). - content_key_to_match = _normalize_course_key(content_key) + + # content_key_to_match = None + # # Whatever the requested content_key is, normalize it to a course with no namespace prefix. + # try: + # requested_course_run_locator = CourseKey.from_string(content_key) + # # No exception raised, content_key represents a course run, so convert it to a course. + # content_key_to_match = _get_course_key_from_locator(requested_course_run_locator) + # except InvalidKeyError: + # # Either the key was already a course key (no problem), or it was something else (weird). + # content_key_to_match = _normalize_course_key(content_key) + + content_key_to_match = _normalize_course_key_from_metadata(assignment_configuration, content_key) queryset = LearnerContentAssignment.objects.select_related('assignment_configuration') try: return queryset.get( @@ -370,15 +376,21 @@ def _update_and_refresh_assignments(assignment_records, fields_changed): ) ) - -def _get_content_title(assignment_configuration, content_key): +def _get_content_summary(assignment_configuration, content_key): """ - Helper to retrieve (from cache) the title of a content_key'ed content_metadata + Helper to retrieve (from cache) the content metadata summary """ content_metadata = get_and_cache_content_metadata( assignment_configuration.enterprise_customer_uuid, content_key, ) + return content_metadata + +def _get_content_title(assignment_configuration, content_key): + """ + Helper to retrieve (from cache) the title of a content_key'ed content_metadata + """ + content_metadata = _get_content_summary(assignment_configuration, content_key) return content_metadata.get('content_title') diff --git a/enterprise_access/apps/subsidy_access_policy/tests/test_models.py b/enterprise_access/apps/subsidy_access_policy/tests/test_models.py index 79ef568e9..f2702a1c6 100644 --- a/enterprise_access/apps/subsidy_access_policy/tests/test_models.py +++ b/enterprise_access/apps/subsidy_access_policy/tests/test_models.py @@ -764,7 +764,14 @@ def setUp(self): autospec=True, ) self.mock_assignments_api = self.assignments_api_patcher.start() + + assign_get_content_metadata_patcher = patch( + 'enterprise_access.apps.content_assignment.api.get_and_cache_content_metadata' + ) + self.mock_assign_get_content_metadata = assign_get_content_metadata_patcher.start() + self.addCleanup(self.assignments_api_patcher.stop) + self.addCleanup(assign_get_content_metadata_patcher.stop) @classmethod def setUpClass(cls): @@ -970,6 +977,10 @@ def test_redeem( self.mock_get_content_metadata.return_value = { 'content_price': 200, } + self.mock_assign_get_content_metadata.return_value = { + 'content_price': 200, + 'course_key': 'edX+DemoX', + } self.mock_subsidy_client.can_redeem.return_value = {'can_redeem': True, 'active': True} self.mock_transactions_cache_for_learner.return_value = { 'transactions': [],