-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: default stale start_dates to today in braze assignment task
- Loading branch information
1 parent
3fc0229
commit 93e1e5e
Showing
4 changed files
with
75 additions
and
7 deletions.
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
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
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,55 @@ | ||
""" | ||
Utils for content_assignments | ||
""" | ||
from datetime import datetime, timedelta | ||
|
||
from dateutil import parser | ||
from pytz import UTC | ||
|
||
from enterprise_access.apps.content_assignments.constants import ( | ||
BRAZE_ACTION_REQUIRED_BY_TIMESTAMP_FORMAT, | ||
START_DATE_DEFAULT_TO_TODAY_THRESHOLD_DAYS | ||
) | ||
|
||
|
||
def is_within_minimum_start_date_threshold(today, start_date): | ||
""" | ||
Checks if today's date were set to a certain number of days in the past, | ||
offset_date_from_today, is the start_date before offset_date_from_today. | ||
""" | ||
start_date_datetime = parser.parse(start_date) | ||
offset_date_from_today = today - timedelta(days=START_DATE_DEFAULT_TO_TODAY_THRESHOLD_DAYS) | ||
return start_date_datetime < offset_date_from_today.replace(tzinfo=UTC) | ||
|
||
|
||
def has_time_to_complete(today, end_date, weeks_to_complete): | ||
""" | ||
Checks if today's date were set to a certain number of weeks_to_complete in the future, | ||
offset_now_by_weeks_to_complete, is offset_now_by_weeks_to_complete date before the end_date | ||
""" | ||
end_date_datetime = parser.parse(end_date) | ||
offset_now_by_weeks_to_complete = today + timedelta(weeks=weeks_to_complete) | ||
return offset_now_by_weeks_to_complete.replace(tzinfo=UTC) <= end_date_datetime | ||
|
||
|
||
def get_self_paced_normalized_start_date(start_date, end_date, course_metadata): | ||
""" | ||
Normalizes courses start_date far in the past based on a heuristic for the purpose of displaying a | ||
reasonable start_date in content assignment related emails. | ||
Heuristic: | ||
For self-paced courses with a weeks_to_complete field too close to the end date to complete the course | ||
or a start_date that is before today offset by the START_DATE_DEFAULT_TO_TODAY_THRESHOLD_DAYS should | ||
default to today's date. | ||
Otherwise, return the current start_date | ||
""" | ||
today = datetime.now() | ||
pacing_type = course_metadata.get('pacing_type') or None | ||
weeks_to_complete = course_metadata.get('weeks_to_complete') or None | ||
if not (start_date and end_date and pacing_type and weeks_to_complete): | ||
return today.strftime(BRAZE_ACTION_REQUIRED_BY_TIMESTAMP_FORMAT) | ||
if pacing_type == "self_paced": | ||
if has_time_to_complete(today, end_date, weeks_to_complete) or \ | ||
is_within_minimum_start_date_threshold(today, start_date): | ||
return today.strftime(BRAZE_ACTION_REQUIRED_BY_TIMESTAMP_FORMAT) | ||
return start_date |