Skip to content

Commit

Permalink
fix: ent catalog course key normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnagro committed Dec 13, 2023
1 parent 513235c commit ba4a4a5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
36 changes: 24 additions & 12 deletions enterprise_access/apps/content_assignments/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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')


Expand Down
11 changes: 11 additions & 0 deletions enterprise_access/apps/subsidy_access_policy/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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': [],
Expand Down

0 comments on commit ba4a4a5

Please sign in to comment.