forked from openedx/edx-platform
-
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
Merged
monteri
merged 4 commits into
mob-develop
from
Ivan_Niedielnitsev/AXM-53/feat/Dates-for=primary=course
Apr 2, 2024
+67
−15
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
89acefe
feat: [AXM-53] add assertions for primary course
NiedielnitsevIvan bc2403e
test: [AXM-53] fix tests
NiedielnitsevIvan 7eb1ad7
style: [AXM-53] change future_assignment default value to None
NiedielnitsevIvan 9655f59
refactor: [AXM-53] add some optimization for assignments collecting
NiedielnitsevIvan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||||
---|---|---|---|---|---|---|
|
@@ -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 = None | ||||||
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
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: | ||||||
next_assignment = DateSummarySerializer(assignment).data | ||||||
break | ||||||
|
||||||
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' | ||||||
|
||||||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The 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
sorted_assignments
2 times? In addition, we will also worsen the readability, as for me.