Skip to content

Commit

Permalink
fix: refactor assignment braze tasks, and so forth
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveagent57 committed Dec 1, 2023
1 parent 51c1b17 commit 87f6fe6
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 189 deletions.
55 changes: 55 additions & 0 deletions enterprise_access/apps/content_assignments/content_metadata_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
API file interacting with assignment metadata (created to avoid a circular
import error)
"""
from datetime import datetime

from enterprise_access.apps.content_metadata.api import get_and_cache_catalog_content_metadata

DATE_INPUT_PATTERNS = [
'%Y-%m-%dT%H:%M:%SZ',
'%Y-%m-%d %H:%M:%SZ',
]


def get_content_metadata_for_assignments(enterprise_catalog_uuid, assignments):
"""
Expand All @@ -25,3 +32,51 @@ def get_content_metadata_for_assignments(enterprise_catalog_uuid, assignments):
assignment.content_key: metadata_by_key.get(assignment.content_key)
for assignment in assignments
}


def get_card_image_url(content_metadata):
"""
Helper to get the appropriate course card image
from a content metadata dictionary.
"""
if first_choice := content_metadata.get('card_image_url'):
return first_choice
if second_choice := content_metadata.get('image_url'):
return second_choice
return None


def get_human_readable_date(datetime_string, output_pattern='%b %d, %Y'):
"""
Given a datetime string value from some content metadata record,
convert it to the provided pattern.
"""
if not datetime_string:
return None

last_exception = None
for input_pattern in DATE_INPUT_PATTERNS:
try:
datetime_obj = datetime.strptime(datetime_string, input_pattern)
return datetime_obj.strftime(output_pattern)
except ValueError as exc:
last_exception = exc

if last_exception is not None:
raise last_exception
return None


def get_course_partners(course_metadata):
"""
Returns a list of course partner data for subsidy requests given a course dictionary.
"""
owners = course_metadata.get('owners') or []
names = [owner.get('name') for owner in owners]
if len(names) < 1:
raise Exception('Course must have a partner')
if len(names) == 1:
return names[0]
if len(names) == 2:
return ' and '.join(names)
return ', '.join(names[:-1]) + ', and ' + names[-1]
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.core.paginator import Paginator
from django.utils.timezone import now, timedelta

from enterprise_access.apps.api_client.lms_client import LmsApiClient
from enterprise_access.apps.content_assignments.constants import (
NUM_DAYS_BEFORE_AUTO_CANCELLATION,
AssignmentAutomaticExpiredReason,
Expand Down Expand Up @@ -41,13 +40,6 @@ def add_arguments(self, parser):
help='Dry Run, print log messages without spawning the celery tasks.',
)

def fetch_enterprise_customer_admin_emails(self, enterprise_customer_uuid):
""" Fetch enterprise customer admin emails """
lms_client = LmsApiClient()
enterprise_customer_data = lms_client.get_enterprise_customer_data(enterprise_customer_uuid)
admin_emails = [user['email'] for user in enterprise_customer_data['admin_users']]
return admin_emails

def handle(self, *args, **options):
dry_run = options['dry_run']

Expand All @@ -69,10 +61,6 @@ def handle(self, *args, **options):
assignment_configuration.enterprise_customer_uuid
)

admin_emails = self.fetch_enterprise_customer_admin_emails(
assignment_configuration.enterprise_customer_uuid
)

allocated_assignments = assignment_configuration.assignments.filter(
state=LearnerContentAssignmentStateChoices.ALLOCATED
)
Expand Down Expand Up @@ -126,4 +114,4 @@ def handle(self, *args, **options):
if not dry_run:
assignment.state = LearnerContentAssignmentStateChoices.CANCELLED
assignment.save()
send_assignment_automatically_expired_email.delay(expired_assignment_uuid, admin_emails)
send_assignment_automatically_expired_email.delay(expired_assignment_uuid)
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def setUp(self):
state=LearnerContentAssignmentStateChoices.ALLOCATED,
)

@mock.patch(COMMAND_PATH + '.LmsApiClient', return_value=mock.MagicMock())
@mock.patch(COMMAND_PATH + '.get_content_metadata_for_assignments')
@mock.patch(COMMAND_PATH + '.send_assignment_automatically_expired_email.delay')
@mock.patch('enterprise_access.apps.subsidy_access_policy.models.SubsidyAccessPolicy.subsidy_client')
Expand All @@ -73,7 +72,6 @@ def test_command_dry_run(
mock_subsidy_client,
mock_send_assignment_automatically_expired_email_task,
mock_content_metadata_for_assignments,
mock_lms_api_client
):
"""
Verify that management command work as expected in dry run mode.
Expand Down Expand Up @@ -101,9 +99,6 @@ def test_command_dry_run(
},
},
}
mock_lms_api_client().get_enterprise_customer_data.return_value = {
'admin_users': [{'email': '[email protected]'}, {'email': '[email protected]'}],
}
mock_path = COMMAND_PATH + '.logger.info'

all_assignment = LearnerContentAssignment.objects.all()
Expand Down Expand Up @@ -168,7 +163,6 @@ def test_command_dry_run(
# verify that state has not changed for any assignment
assert all_assignment.count() == allocated_assignments.count()

@mock.patch(COMMAND_PATH + '.LmsApiClient', return_value=mock.MagicMock())
@mock.patch(COMMAND_PATH + '.get_content_metadata_for_assignments')
@mock.patch(COMMAND_PATH + '.send_assignment_automatically_expired_email.delay')
@mock.patch('enterprise_access.apps.subsidy_access_policy.models.SubsidyAccessPolicy.subsidy_client')
Expand All @@ -177,7 +171,6 @@ def test_command(
mock_subsidy_client,
mock_send_assignment_automatically_expired_email_task,
mock_content_metadata_for_assignments,
mock_lms_api_client
):
"""
Verify that management command work as expected in dry run mode.
Expand All @@ -200,9 +193,6 @@ def test_command(
},
},
}
mock_lms_api_client().get_enterprise_customer_data.return_value = {
'admin_users': [{'email': '[email protected]'}, {'email': '[email protected]'}],
}
mock_path = COMMAND_PATH + '.logger.info'

all_assignment = LearnerContentAssignment.objects.all()
Expand Down Expand Up @@ -260,8 +250,8 @@ def test_command(
)
mock_send_assignment_automatically_expired_email_task.assert_has_calls(
[
call(self.alice_assignment.uuid, ['[email protected]', '[email protected]']),
call(self.bob_assignment.uuid, ['[email protected]', '[email protected]'])
call(self.alice_assignment.uuid),
call(self.bob_assignment.uuid)
]
)

Expand Down
Loading

0 comments on commit 87f6fe6

Please sign in to comment.