-
Notifications
You must be signed in to change notification settings - Fork 14
feat: [FC-7879] add signal handler to save assignment dates to Conten… #347
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
Open
kyrylo-kh
wants to merge
4
commits into
openedx:master
Choose a base branch
from
raccoongang:rg/axm-dates-listen-to-course-published-and-process
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+307
−9
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
906ef14
feat: [FC-7879] add signal handler to save assignment dates to Conten…
kyrylo-kh 9e50d46
Merge branch 'master' into rg/axm-dates-listen-to-course-published-an…
kyrylo-kh c875945
fix: address review on assignment due dates handler
kyrylo-kh 79d5ace
fix: [AXM-2300] address review on update_or_create_assignments_due_dates
kyrylo-kh 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 hidden or 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 |
|---|---|---|
|
|
@@ -3,7 +3,8 @@ | |
| """ | ||
|
|
||
| import logging | ||
| from datetime import timedelta | ||
| from dataclasses import dataclass | ||
| from datetime import datetime, timedelta | ||
|
|
||
| from django.core.exceptions import ValidationError | ||
| from django.db import transaction | ||
|
|
@@ -400,6 +401,19 @@ def get_overrides_for_course(course_id): | |
| return dates | ||
|
|
||
|
|
||
| def _set_content_date_policy(date_kwargs, content_date): | ||
| """ | ||
| Assign a DatePolicy matching ``date_kwargs`` to ``content_date``. | ||
|
|
||
| Reuses an existing DatePolicy with the same values when present, otherwise creates one. | ||
| """ | ||
| existing_policies = list(models.DatePolicy.objects.filter(**date_kwargs).order_by('id')) | ||
| if existing_policies: | ||
| content_date.policy = existing_policies[0] | ||
| else: | ||
| content_date.policy = models.DatePolicy.objects.create(**date_kwargs) | ||
|
|
||
|
|
||
| def set_date_for_block( | ||
| course_id, block_id, field, date_or_timedelta, | ||
| user=None, reason='', actor=None | ||
|
|
@@ -425,14 +439,6 @@ def set_date_for_block( | |
| else: | ||
| date_kwargs = {'abs_date': date_or_timedelta} | ||
|
|
||
| def _set_content_date_policy(date_kwargs, existing_content_date): | ||
| # Race conditions were creating multiple DatePolicies w/ the same values. Handle that case. | ||
| existing_policies = list(models.DatePolicy.objects.filter(**date_kwargs).order_by('id')) | ||
| if existing_policies: | ||
| existing_content_date.policy = existing_policies[0] | ||
| else: | ||
| existing_content_date.policy = models.DatePolicy.objects.create(**date_kwargs) | ||
|
|
||
| with transaction.atomic(savepoint=False): # this is frequently called in a loop, let's avoid the savepoints | ||
| try: | ||
| existing_date = models.ContentDate.objects.select_related('policy').get( | ||
|
|
@@ -548,6 +554,88 @@ def get_schedules_with_due_date(course_id, assignment_date): | |
| return schedules | ||
|
|
||
|
|
||
| @dataclass | ||
| class Assignment: | ||
|
Member
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. suggestion (optional): this dataclass can be extracted into data.py |
||
| """ | ||
| Represents an assignment with a title, due date, block key, and optional subsection name. | ||
| """ | ||
|
|
||
| title: str | ||
| date: datetime | None | ||
| block_key: UsageKey | ||
| subsection_name: str = '' | ||
|
|
||
| def __post_init__(self): | ||
| """ | ||
| Validate the assignment object. | ||
| """ | ||
| if self.date is not None and not isinstance(self.date, datetime): | ||
| raise TypeError("date must be a datetime object or None") | ||
| if not isinstance(self.block_key, UsageKey): | ||
| raise TypeError("block_key must be a UsageKey object") | ||
|
|
||
|
|
||
| def update_or_create_assignments_due_dates(course_key, assignments: list[Assignment]): | ||
| """ | ||
| Update or create due dates for a list of assignments in a course. | ||
|
|
||
| Each assignment's due date is written to ContentDate. If a ContentDate already | ||
| exists for (course_key, assignment.block_key, 'due'), it is updated; otherwise | ||
| a new one is created. All operations are performed inside a single database | ||
| transaction. | ||
|
|
||
| Row identity is (course_id, location, field='due'); ``block_type`` stores the | ||
| structural XBlock type (e.g. 'sequential') and is not part of the lookup. The | ||
| DatePolicy is resolved via `_set_content_date_policy` so concurrent calls | ||
| do not create duplicate DatePolicy rows. | ||
|
|
||
| Arguments: | ||
| course_key: CourseKey or string representation of the course. | ||
| assignments: List of Assignment instances. Assignments with missing date or | ||
| title are skipped (with a warning). Use Assignment.subsection_name for | ||
| the containing subsection when available; it is stored separately from | ||
| assignment_title. | ||
|
|
||
| Returns: | ||
| None | ||
| """ | ||
| course_key = _ensure_key(CourseKey, course_key) | ||
| with transaction.atomic(): | ||
|
Member
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. Will be good to have a test case for the atomic boundary in this method |
||
| for assignment in assignments: | ||
| log.info( | ||
| "Updating assignment '%s' with due date '%s' for course %s", | ||
| assignment.title, | ||
| assignment.date, | ||
| course_key | ||
| ) | ||
| if not all((assignment.date, assignment.title)): | ||
| log.warning( | ||
| "Skipping assignment '%s' for course %s because it has no date or title", | ||
| assignment, | ||
| course_key | ||
| ) | ||
| continue | ||
| try: | ||
| content_date = models.ContentDate.objects.select_related('policy').get( | ||
| course_id=course_key, | ||
| location=assignment.block_key, | ||
| field='due', | ||
| ) | ||
| except models.ContentDate.DoesNotExist: | ||
| content_date = models.ContentDate( | ||
| course_id=course_key, | ||
| location=assignment.block_key, | ||
| field='due', | ||
| ) | ||
| _set_content_date_policy({'abs_date': assignment.date}, content_date) | ||
| content_date.block_type = assignment.block_key.block_type | ||
| content_date.assignment_title = assignment.title | ||
| content_date.course_name = course_key.course | ||
| content_date.subsection_name = assignment.subsection_name | ||
| content_date.active = True | ||
| content_date.save() | ||
|
|
||
|
|
||
| class BaseWhenException(Exception): | ||
| pass | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.