Skip to content

Commit

Permalink
chore: code refactoring and a utility function for formating dates
Browse files Browse the repository at this point in the history
  • Loading branch information
mudassir-hafeez committed Jul 2, 2024
1 parent f790184 commit af424be
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion courses/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def create_run_enrollments(
successful_enrollments.append(enrollment)
if enrollment.edx_enrolled:
mail_api.send_course_run_enrollment_email(enrollment)
mail_api.send_welcome_course_run_enrollment_email(enrollment)
mail_api.send_course_run_enrollment_welcome_email(enrollment)
return successful_enrollments, edx_request_success


Expand Down
2 changes: 1 addition & 1 deletion courses/management/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def create_run_enrollment(
run_enrollment.edx_enrolled = True
run_enrollment.save_and_log(None)
mail_api.send_course_run_enrollment_email(run_enrollment)
mail_api.send_welcome_course_run_enrollment_email(run_enrollment)
mail_api.send_course_run_enrollment_welcome_email(run_enrollment)
elif not keep_failed_enrollments:
if created:
run_enrollment.delete()
Expand Down
24 changes: 8 additions & 16 deletions ecommerce/mail_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Ecommerce mail API"""

import datetime
import logging
from urllib.parse import urlencode, urljoin

Expand All @@ -11,7 +10,7 @@

from courses.models import CourseRun
from ecommerce.constants import BULK_ENROLLMENT_EMAIL_TAG, CYBERSOURCE_CARD_TYPES
from ecommerce.utils import make_checkout_url
from ecommerce.utils import format_run_date, make_checkout_url
from mail import api
from mail.constants import (
EMAIL_B2B_RECEIPT,
Expand Down Expand Up @@ -194,23 +193,16 @@ def send_course_run_unenrollment_email(enrollment):
log.exception("Error sending unenrollment success email: %s", exp) # noqa: TRY401


def send_welcome_course_run_enrollment_email(enrollment):
def send_course_run_enrollment_welcome_email(enrollment):
"""
Welcome the user of successful enrollment for a course run
Send welcome email to the user on successful enrollment
Args:
enrollment (CourseRunEnrollment): the enrollment for which to send the welcome email
"""
run_start_date = run_end_date = ""
run_start_time = ""
if enrollment.run.start_date:
run_start_date = enrollment.run.start_date.strftime(EMAIL_DATE_FORMAT)
run_start_time = enrollment.run.start_date.astimezone(
datetime.timezone.utc
).strftime(EMAIL_TIME_FORMAT)
if enrollment.run.end_date:
run_end_date = enrollment.run.end_date.strftime(EMAIL_DATE_FORMAT)
date_range = (
run_start_date, run_start_time = format_run_date(enrollment.run.start_date)
run_end_date, _ = format_run_date(enrollment.run.end_date)
run_duration = (
f"{run_start_date} - {run_end_date}" if run_start_date and run_end_date else ""
)
try:
Expand All @@ -224,14 +216,14 @@ def send_welcome_course_run_enrollment_email(enrollment):
"enrollment": enrollment,
"run_start_date": run_start_date,
"run_start_time": run_start_time,
"run_date_range": date_range,
"run_date_range": run_duration,
},
),
EMAIL_WELCOME_COURSE_RUN_ENROLLMENT,
)
)
except: # noqa: E722
log.exception("Error sending welcome success email")
log.exception("Error sending welcome email")


def send_b2b_receipt_email(order):
Expand Down
8 changes: 4 additions & 4 deletions ecommerce/mail_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
send_b2b_receipt_email,
send_bulk_enroll_emails,
send_course_run_enrollment_email,
send_course_run_enrollment_welcome_email,
send_ecommerce_order_receipt,
send_enrollment_failure_message,
send_welcome_course_run_enrollment_email,
)
from ecommerce.models import Order
from mail.api import EmailMetadata, UserMessageProps
Expand Down Expand Up @@ -150,8 +150,8 @@ def test_send_course_run_enrollment_email_error(mocker):
)


def test_send_welcome_course_run_enrollment_email(mocker):
"""send_welcome_course_run_enrollment_email should send a welcome email for the given enrollment"""
def test_send_course_run_enrollment_welcome_email(mocker):
"""send_course_run_enrollment_welcome_email should send a welcome email for the given enrollment"""
patched_mail_api = mocker.patch("ecommerce.mail_api.api")
enrollment = CourseRunEnrollmentFactory.create()

Expand All @@ -165,7 +165,7 @@ def test_send_welcome_course_run_enrollment_email(mocker):
f"{run_end_date.strftime(EMAIL_DATE_FORMAT)}"
)

send_welcome_course_run_enrollment_email(enrollment)
send_course_run_enrollment_welcome_email(enrollment)

patched_mail_api.context_for_user.assert_called_once_with(
user=enrollment.user,
Expand Down
22 changes: 22 additions & 0 deletions ecommerce/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Utility functions for ecommerce"""

import datetime
import logging
from urllib.parse import urlencode, urljoin

Expand All @@ -12,6 +13,7 @@
from ecommerce.exceptions import ParseException

log = logging.getLogger(__name__)
EMAIL_TIME_FORMAT = "%I:%M %p %Z"


def create_delete_rule(table_name):
Expand Down Expand Up @@ -154,3 +156,23 @@ def is_existing_coupon_code(value):
Coupon.objects.filter(coupon_code=value).exists()
or B2BCoupon.objects.filter(coupon_code=value).exists()
)


def format_run_date(run_date):
"""
Format run date to return both date and time strings.
Args:
run_date (datetime): The datetime to format.
Returns:
tuple: A tuple containing the formatted date and time strings.
"""
if run_date:
from ecommerce.mail_api import EMAIL_DATE_FORMAT

formatted_date_time = run_date.astimezone(datetime.timezone.utc).strftime(
f"{EMAIL_DATE_FORMAT}-{EMAIL_TIME_FORMAT}"
)
return tuple(formatted_date_time.split("-", 1))
return "", ""

0 comments on commit af424be

Please sign in to comment.