Skip to content

Commit

Permalink
refactor: [AXM-297] Add typing, refactor methods
Browse files Browse the repository at this point in the history
  • Loading branch information
KyryloKireiev committed Apr 25, 2024
1 parent fa6349c commit 2f9e155
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
34 changes: 24 additions & 10 deletions lms/djangoapps/mobile_api/course_info/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 4 additions & 12 deletions lms/djangoapps/mobile_api/course_info/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down

0 comments on commit 2f9e155

Please sign in to comment.