Skip to content

Commit

Permalink
fix: use course-run specific metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzawaleed01 committed Feb 7, 2025
1 parent f4d8c4b commit 4e6a658
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
11 changes: 6 additions & 5 deletions enterprise_access/apps/content_assignments/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from enterprise_access.tasks import LoggedTaskWithRetry
from enterprise_access.utils import (
get_automatic_expiration_date_and_reason,
get_course_run_metadata_for_assignment,
get_normalized_metadata_for_assignment,
localized_utcnow
)
Expand Down Expand Up @@ -215,16 +216,16 @@ def get_start_date(self):
Checks if the start_date is matches the criteria set by `get_self_paced_normalized_start_date`
for old start_dates, if so, return today's date, otherwise, return the start_date
"""
start_date = self.normalized_metadata.get('start_date')
end_date = self.normalized_metadata.get('end_date')
course_metadata = self.course_metadata
course_run_metadata = get_course_run_metadata_for_assignment(self.assignment, self.course_metadata)
start_date = course_run_metadata.get('start_date')
end_date = course_run_metadata.get('end_date')
logger.info(
f"[get_start_date] Assignment UUID: {self.assignment.uuid} - start_date: {start_date}, "
f"end_date: {end_date}, "
f"course_metadata: {course_metadata}"
f"course_run_metadata: {course_run_metadata}"
)
return get_human_readable_date(
get_self_paced_normalized_start_date(start_date, end_date, course_metadata)
get_self_paced_normalized_start_date(start_date, end_date, course_run_metadata)
)

def get_action_required_by_timestamp(self):
Expand Down
11 changes: 11 additions & 0 deletions enterprise_access/apps/content_assignments/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ def setUpTestData(cls):
}
cls.mock_content_metadata = {
'key': TEST_COURSE_KEY,
'course_runs':[
{
'key': TEST_COURSE_RUN_KEY,
'start': '2020-01-01T12:00:00Z',
'end': '2024-03-22T23:59:59Z',
'enrollment_start': None,
'enrollment_end': '2023-09-11T19:00:00Z',
'pacing_type': 'instructor_paced',
'weeks_to_complete': 8,
}
],
'normalized_metadata': {
'start_date': '2020-01-01T12:00:00Z',
'end_date': '2022-01-01 12:00:00Z',
Expand Down
24 changes: 24 additions & 0 deletions enterprise_access/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,27 @@ def _days_from_now(days_from_now=0, date_format=None):
if not date_format:
return date
return date.strftime(date_format)


def get_course_run_metadata_for_assignment(assignment, content_metadata):
"""
Retrieves metadata for a specific course run associated with an assignment. If the assignment has
a preferred course run, returns the metadata for that run. If the preferred run metadata is not
found, returns normalized_metadata.
Args:
assignment (dict): The assignment object.
content_metadata (dict): The content metadata object.
Returns:
dict: Course run metadata if available, otherwise normalized_metadata.
"""
if preferred_course_run_key := assignment.preferred_course_run_key:
course_runs = content_metadata.get('course_runs', [])
for course_run in course_runs:
if course_run.get('key') == preferred_course_run_key:
# manually applying existing key value mappings for consistent return structure
course_run['start_date'] = course_run.get('start')
course_run['end_date'] = course_run.get('end')
return course_run
return content_metadata.get('normalized_metadata', {})

0 comments on commit 4e6a658

Please sign in to comment.