forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [AXM-297] Add progress to assignments in BlocksInfoInCourseView…
… API (#2546) * feat: [AXM-297, AXM-310] Add progress to assignments and total course progress * feat: [AXM-297] Add progress to assignments * style: [AXM-297] Try to fix linters (add docstrings) * refactor: [AXM-297] Add typing, refactor methods
- Loading branch information
1 parent
ebcdde6
commit 18f5eb8
Showing
4 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
""" | ||
Common constants for the `course_info` API. | ||
""" | ||
|
||
BLOCK_STRUCTURE_CACHE_TIMEOUT = 60 * 60 # 1 hour |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
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: '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)) | ||
|
||
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) | ||
|
||
# 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 [] | ||
|
||
return subsection_grades |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters