-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: [AXM-53] add assertions for primary course #2522
Changes from 2 commits
89acefe
bc2403e
7eb1ad7
9655f59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||||||||||||||||||
Serializer for user API | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
|
||||||||||||||||||||||
from datetime import datetime | ||||||||||||||||||||||
from typing import Dict, List, Optional, Tuple | ||||||||||||||||||||||
|
||||||||||||||||||||||
from django.core.cache import cache | ||||||||||||||||||||||
|
@@ -16,8 +17,10 @@ | |||||||||||||||||||||
from lms.djangoapps.certificates.api import certificate_downloadable_status | ||||||||||||||||||||||
from lms.djangoapps.courseware.access import has_access | ||||||||||||||||||||||
from lms.djangoapps.courseware.block_render import get_block_for_descriptor | ||||||||||||||||||||||
from lms.djangoapps.courseware.courses import get_current_child | ||||||||||||||||||||||
from lms.djangoapps.courseware.context_processor import get_user_timezone_or_last_seen_timezone_or_utc | ||||||||||||||||||||||
from lms.djangoapps.courseware.courses import get_course_assignment_date_blocks, get_current_child | ||||||||||||||||||||||
from lms.djangoapps.courseware.model_data import FieldDataCache | ||||||||||||||||||||||
from lms.djangoapps.course_home_api.dates.serializers import DateSummarySerializer | ||||||||||||||||||||||
from lms.djangoapps.grades.api import CourseGradeFactory | ||||||||||||||||||||||
from openedx.core.djangoapps.content.block_structure.api import get_block_structure_manager | ||||||||||||||||||||||
from openedx.features.course_duration_limits.access import get_user_course_expiration_date | ||||||||||||||||||||||
|
@@ -160,9 +163,14 @@ class CourseEnrollmentSerializerModifiedForPrimary(CourseEnrollmentSerializer): | |||||||||||||||||||||
|
||||||||||||||||||||||
course_status = serializers.SerializerMethodField() | ||||||||||||||||||||||
progress = serializers.SerializerMethodField() | ||||||||||||||||||||||
course_assignments = serializers.SerializerMethodField() | ||||||||||||||||||||||
|
||||||||||||||||||||||
BLOCK_STRUCTURE_CACHE_TIMEOUT = 60 * 60 # 1 hour | ||||||||||||||||||||||
|
||||||||||||||||||||||
def __init__(self, *args, **kwargs): | ||||||||||||||||||||||
super().__init__(*args, **kwargs) | ||||||||||||||||||||||
self.course = modulestore().get_course(self.instance.course.id) | ||||||||||||||||||||||
|
||||||||||||||||||||||
def get_course_status(self, model: CourseEnrollment) -> Optional[Dict[str, List[str]]]: | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
Gets course status for the given user's enrollments. | ||||||||||||||||||||||
|
@@ -186,8 +194,8 @@ def get_course_status(self, model: CourseEnrollment) -> Optional[Dict[str, List[ | |||||||||||||||||||||
'last_visited_unit_display_name': unit_name, | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
@staticmethod | ||||||||||||||||||||||
def _get_last_visited_block_path_and_unit_name( | ||||||||||||||||||||||
self, | ||||||||||||||||||||||
request: 'Request', # noqa: F821 | ||||||||||||||||||||||
model: CourseEnrollment, | ||||||||||||||||||||||
) -> Tuple[List[Optional['XBlock']], Optional[str]]: # noqa: F821 | ||||||||||||||||||||||
|
@@ -197,12 +205,10 @@ def _get_last_visited_block_path_and_unit_name( | |||||||||||||||||||||
If there is no such visit, the first item deep enough down the course | ||||||||||||||||||||||
tree is used. | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
course = modulestore().get_course(model.course.id) | ||||||||||||||||||||||
field_data_cache = FieldDataCache.cache_for_block_descendents( | ||||||||||||||||||||||
course.id, model.user, course, depth=3) | ||||||||||||||||||||||
field_data_cache = FieldDataCache.cache_for_block_descendents(self.course.id, model.user, self.course, depth=3) | ||||||||||||||||||||||
|
||||||||||||||||||||||
course_block = get_block_for_descriptor( | ||||||||||||||||||||||
model.user, request, course, field_data_cache, course.id, course=course | ||||||||||||||||||||||
model.user, request, self.course, field_data_cache, self.course.id, course=self.course | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
unit_name = '' | ||||||||||||||||||||||
|
@@ -243,6 +249,32 @@ def get_progress(self, model: CourseEnrollment) -> Dict[str, int]: | |||||||||||||||||||||
'num_points_possible': sum(map(lambda x: x.graded_total.possible if x.graded else 0, subsection_grades)), | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
def get_course_assignments(self, model: CourseEnrollment) -> Optional[Dict[str, List[Dict[str, str]]]]: | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
Returns the future assignment data and past assignments data for the user in the course. | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
assignments = get_course_assignment_date_blocks( | ||||||||||||||||||||||
self.course, | ||||||||||||||||||||||
model.user, | ||||||||||||||||||||||
self.context.get('request'), | ||||||||||||||||||||||
include_past_dates=True | ||||||||||||||||||||||
) | ||||||||||||||||||||||
next_assignment = {} | ||||||||||||||||||||||
past_assignment = [] | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
If we iterate over the same list twice the complexity will be O(2N) that is by O-notation roughly the same as a single iteration. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What benefits will this give us, since this way we will iterate by |
||||||||||||||||||||||
|
||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
create this variable and move it above the past assignments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this micro-optimization here if there is still only one cycle? |
||||||||||||||||||||||
timezone = get_user_timezone_or_last_seen_timezone_or_utc(model.user) | ||||||||||||||||||||||
for assignment in sorted(assignments, key=lambda x: x.date): | ||||||||||||||||||||||
if assignment.date < datetime.now(timezone): | ||||||||||||||||||||||
past_assignment.append(assignment) | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
if not next_assignment: | ||||||||||||||||||||||
next_assignment = DateSummarySerializer(assignment).data | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
return { | ||||||||||||||||||||||
'future_assignment': next_assignment, | ||||||||||||||||||||||
'past_assignments': DateSummarySerializer(past_assignment, many=True).data, | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
class Meta: | ||||||||||||||||||||||
model = CourseEnrollment | ||||||||||||||||||||||
fields = ( | ||||||||||||||||||||||
|
@@ -255,6 +287,7 @@ class Meta: | |||||||||||||||||||||
'course_modes', | ||||||||||||||||||||||
'course_status', | ||||||||||||||||||||||
'progress', | ||||||||||||||||||||||
'course_assignments', | ||||||||||||||||||||||
) | ||||||||||||||||||||||
lookup_field = 'username' | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we will declare it with
None
and return also returnNone
if no future assignment?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed