Skip to content

Commit

Permalink
refactor: feature flag replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
mudassir-hafeez committed Jul 12, 2024
1 parent 6c57b5a commit a9754d1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
7 changes: 6 additions & 1 deletion courses/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections import namedtuple
from traceback import format_exc

from django.conf import settings
from django.core.exceptions import ValidationError
from requests.exceptions import ConnectionError as RequestsConnectionError
from requests.exceptions import HTTPError
Expand Down Expand Up @@ -113,6 +114,7 @@ def create_run_enrollments(
for all of the given course runs
"""
successful_enrollments = []
send_welcome_email = settings.FEATURES.get("ENROLLMENT_WELCOME_EMAIL", False)
try:
enroll_in_edx_course_runs(user, runs)
except (
Expand Down Expand Up @@ -155,7 +157,10 @@ def create_run_enrollments(
successful_enrollments.append(enrollment)
if enrollment.edx_enrolled:
mail_api.send_course_run_enrollment_email(enrollment)
mail_api.send_course_run_enrollment_welcome_email(enrollment)
if send_welcome_email:
mail_api.send_course_run_enrollment_welcome_email(enrollment)
else:
log.info("Feature ENROLLMENT_WELCOME_EMAIL is disabled.")
return successful_enrollments, edx_request_success


Expand Down
9 changes: 8 additions & 1 deletion courses/management/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Utility functions/classes for course management commands"""

import logging
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError

from courses.models import CourseRun, CourseRunEnrollment, Program, ProgramEnrollment
Expand All @@ -12,6 +14,8 @@
from ecommerce import mail_api
from mitxpro.utils import has_equal_properties

log = logging.getLogger(__name__)


def enrollment_summary(enrollment):
"""
Expand Down Expand Up @@ -216,7 +220,10 @@ 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_course_run_enrollment_welcome_email(run_enrollment)
if settings.FEATURES.get("ENROLLMENT_WELCOME_EMAIL", False):
mail_api.send_course_run_enrollment_welcome_email(run_enrollment)
else:
log.info("Feature ENROLLMENT_WELCOME_EMAIL is disabled.")
elif not keep_failed_enrollments:
if created:
run_enrollment.delete()
Expand Down
3 changes: 0 additions & 3 deletions ecommerce/mail_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,6 @@ def send_course_run_enrollment_welcome_email(enrollment):
Args:
enrollment (CourseRunEnrollment): the enrollment for which to send the welcome email
"""
if not settings.FEATURES.get("ENROLLMENT_WELCOME_EMAIL", False):
log.info("Feature ENROLLMENT_WELCOME_EMAIL is disabled.")
return
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 = (
Expand Down
44 changes: 18 additions & 26 deletions ecommerce/mail_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,8 @@ def test_send_course_run_enrollment_email_error(mocker):
)


@pytest.mark.parametrize("enabled", [True, False])
def test_send_course_run_enrollment_welcome_email(settings, mocker, enabled):
def test_send_course_run_enrollment_welcome_email(mocker):
"""send_course_run_enrollment_welcome_email should send a welcome email for the given enrollment"""
settings.FEATURES["ENROLLMENT_WELCOME_EMAIL"] = enabled
mock_log = mocker.patch("ecommerce.mail_api.log")
patched_mail_api = mocker.patch("ecommerce.mail_api.api")
enrollment = CourseRunEnrollmentFactory.create()

Expand All @@ -170,28 +167,23 @@ def test_send_course_run_enrollment_welcome_email(settings, mocker, enabled):

send_course_run_enrollment_welcome_email(enrollment)

if not enabled:
mock_log.info.assert_called_once_with(
"Feature ENROLLMENT_WELCOME_EMAIL is disabled."
)
else:
patched_mail_api.context_for_user.assert_called_once_with(
user=enrollment.user,
extra_context={
"enrollment": enrollment,
"run_start_date": run_start_date.strftime(EMAIL_DATE_FORMAT),
"run_start_time": run_start_time,
"run_date_range": date_range,
},
)
patched_mail_api.message_for_recipient.assert_called_once_with(
enrollment.user.email,
patched_mail_api.context_for_user.return_value,
EMAIL_WELCOME_COURSE_RUN_ENROLLMENT,
)
patched_mail_api.send_message.assert_called_once_with(
patched_mail_api.message_for_recipient.return_value
)
patched_mail_api.context_for_user.assert_called_once_with(
user=enrollment.user,
extra_context={
"enrollment": enrollment,
"run_start_date": run_start_date.strftime(EMAIL_DATE_FORMAT),
"run_start_time": run_start_time,
"run_date_range": date_range,
},
)
patched_mail_api.message_for_recipient.assert_called_once_with(
enrollment.user.email,
patched_mail_api.context_for_user.return_value,
EMAIL_WELCOME_COURSE_RUN_ENROLLMENT,
)
patched_mail_api.send_message.assert_called_once_with(
patched_mail_api.message_for_recipient.return_value
)


@pytest.mark.parametrize("has_discount", [True, False])
Expand Down

0 comments on commit a9754d1

Please sign in to comment.