diff --git a/lms/djangoapps/mobile_api/course_info/utils.py b/lms/djangoapps/mobile_api/course_info/utils.py index b86361223275..141c32da4cf4 100644 --- a/lms/djangoapps/mobile_api/course_info/utils.py +++ b/lms/djangoapps/mobile_api/course_info/utils.py @@ -2,28 +2,42 @@ Common utils for the `course_info` API. """ +import logging +from typing import List, Optional, Union + from django.core.cache import cache from lms.djangoapps.courseware.access import has_access from lms.djangoapps.grades.api import CourseGradeFactory from openedx.core.djangoapps.content.block_structure.api import get_block_structure_manager +log = logging.getLogger(__name__) + -def calculate_progress(user, course_id, cache_timeout): +def calculate_progress( + user: 'User', # noqa: F821 + course_id: 'CourseLocator', # noqa: F821 + cache_timeout: int, +) -> Optional[List[Union['ReadSubsectionGrade', 'ZeroSubsectionGrade']]]: # noqa: F821 """ Calculate the progress of the user in the course. """ is_staff = bool(has_access(user, 'staff', course_id)) - cache_key = f'course_block_structure_{str(course_id)}_{user.id}' - collected_block_structure = cache.get(cache_key) - if not collected_block_structure: - collected_block_structure = get_block_structure_manager(course_id).get_collected() - cache.set(cache_key, collected_block_structure, cache_timeout) + try: + cache_key = f'course_block_structure_{str(course_id)}_{user.id}' + collected_block_structure = cache.get(cache_key) + if not collected_block_structure: + collected_block_structure = get_block_structure_manager(course_id).get_collected() + cache.set(cache_key, collected_block_structure, cache_timeout) + + course_grade = CourseGradeFactory().read(user, collected_block_structure=collected_block_structure) - course_grade = CourseGradeFactory().read(user, collected_block_structure=collected_block_structure) + # recalculate course grade from visible grades (stored grade was calculated over all grades, visible or not) + course_grade.update(visible_grades_only=True, has_staff_access=is_staff) + subsection_grades = list(course_grade.subsection_grades.values()) + except Exception as err: # pylint: disable=broad-except + log.warning(f'Could not get grades for the course: {course_id}, error: {err}') + return [] - # recalculate course grade from visible grades (stored grade was calculated over all grades, visible or not) - course_grade.update(visible_grades_only=True, has_staff_access=is_staff) - subsection_grades = list(course_grade.subsection_grades.values()) return subsection_grades diff --git a/lms/djangoapps/mobile_api/course_info/views.py b/lms/djangoapps/mobile_api/course_info/views.py index 352efca9b4aa..c6de108727d8 100644 --- a/lms/djangoapps/mobile_api/course_info/views.py +++ b/lms/djangoapps/mobile_api/course_info/views.py @@ -405,20 +405,12 @@ def _extend_sequential_info_with_assignment_progress( if block_info['type'] == 'sequential': grade = grades_with_locations.get(block_id) if grade: - points_earned = ( - grade.graded_total.earned - if grades_with_locations[block_id].graded - else 0 - ) - points_possible = ( - grade.graded_total.possible - if grades_with_locations[block_id].graded - else 0 - ) + graded_total = grade.graded_total if grade.graded else None + points_earned = graded_total.earned if graded_total else 0 + points_possible = graded_total.possible if graded_total else 0 assignment_type = grade.format else: - points_earned, points_possible = 0, 0 - assignment_type = None + points_earned, points_possible, assignment_type = 0, 0, None block_info.update( {