Unit tests for "_add_upsell_button_to_email_template".

This commit is contained in:
sandroroux
2017-10-10 16:43:18 -04:00
committed by Calen Pennington
parent d571adfb99
commit 40d3f4f2fc
13 changed files with 365 additions and 142 deletions

View File

@@ -10,42 +10,38 @@ from django.conf import settings
from edx_ace.channel import ChannelType
from edx_ace.test_utils import StubPolicy, patch_channels, patch_policies
from edx_ace.utils.date import serialize
from edx_ace.message import Message
from mock import Mock, patch
from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locator import CourseLocator
from course_modes.models import CourseMode
from course_modes.tests.factories import CourseModeFactory
from courseware.models import DynamicUpgradeDeadlineConfiguration
from openedx.core.djangoapps.schedules import resolvers, tasks
from openedx.core.djangoapps.schedules.management.commands import send_recurring_nudge as nudge
from openedx.core.djangoapps.schedules.tests.factories import ScheduleConfigFactory, ScheduleFactory
from openedx.core.djangoapps.site_configuration.tests.factories import SiteConfigurationFactory, SiteFactory
from openedx.core.djangolib.testing.utils import CacheIsolationTestCase, skip_unless_lms
from openedx.core.djangoapps.waffle_utils.testutils import WAFFLE_TABLES
from openedx.core.djangolib.testing.utils import CacheIsolationTestCase, skip_unless_lms, FilteredQueryCountMixin
from student.tests.factories import UserFactory
# Populating recurring nudge emails requires three queries
# 1a) Find all users whose first enrollment during a day was in the specified hour window
# 1b) All schedules for all enrollments for that day for users in that window (with 1a as a subquery)
# 2) Check whether debugging is enabled
CONST_QUERIES = 2
# 1) Prefetch all course modes for those schedules
# 2) (When not cached) load the DynamicUpgradeDeadlineConfiguration
SCHEDULE_QUERIES = 2
# 1) Load the current django site
# 2) Load the ScheduleConfig
SEND_QUERIES = 2
# 2) Query the schedules to find all of the template context information
NUM_QUERIES_NO_MATCHING_SCHEDULES = 2
# 1) (When not cached) load the CourseDynamicUpgradeDeadlineConfiguration
# 2) Load the VERIFIED course mode for the course
PER_COURSE_QUERIES = 2
# 3) Query all course modes for all courses in returned schedules
NUM_QUERIES_WITH_MATCHES = NUM_QUERIES_NO_MATCHING_SCHEDULES + 1
NUM_COURSE_MODES_QUERIES = 1
@ddt.ddt
@skip_unless_lms
@skipUnless('openedx.core.djangoapps.schedules.apps.SchedulesConfig' in settings.INSTALLED_APPS,
"Can't test schedules if the app isn't installed")
class TestSendRecurringNudge(CacheIsolationTestCase):
class TestSendRecurringNudge(FilteredQueryCountMixin, CacheIsolationTestCase):
# pylint: disable=protected-access
ENABLED_CACHES = ['default']
@@ -61,6 +57,8 @@ class TestSendRecurringNudge(CacheIsolationTestCase):
self.site_config = SiteConfigurationFactory.create(site=site)
ScheduleConfigFactory.create(site=self.site_config.site)
DynamicUpgradeDeadlineConfiguration.objects.create(enabled=True)
@patch.object(nudge.Command, 'resolver_class')
def test_handle(self, mock_resolver):
test_time = datetime.datetime(2017, 8, 1, tzinfo=pytz.UTC)
@@ -94,16 +92,21 @@ class TestSendRecurringNudge(CacheIsolationTestCase):
schedules = [
ScheduleFactory.create(
start=datetime.datetime(2017, 8, 3, 18, 44, 30, tzinfo=pytz.UTC),
enrollment__user=UserFactory.create(),
enrollment__course__id=CourseLocator('edX', 'toy', 'Bin')
) for _ in range(schedule_count)
) for i in range(schedule_count)
]
bins_in_use = frozenset((s.enrollment.user.id % tasks.RECURRING_NUDGE_NUM_BINS) for s in schedules)
test_time = datetime.datetime(2017, 8, 3, 18, tzinfo=pytz.UTC)
test_time_str = serialize(test_time)
for b in range(tasks.RECURRING_NUDGE_NUM_BINS):
# waffle flag takes an extra query before it is cached
with self.assertNumQueries(3 if b == 0 else 2):
expected_queries = NUM_QUERIES_NO_MATCHING_SCHEDULES
if b in bins_in_use:
# to fetch course modes for valid schedules
expected_queries += NUM_COURSE_MODES_QUERIES
with self.assertNumQueries(expected_queries, table_blacklist=WAFFLE_TABLES):
tasks.recurring_nudge_schedule_bin(
self.site_config.site.id, target_day_str=test_time_str, day_offset=-3, bin_num=b,
org_list=[schedules[0].enrollment.course.org],
@@ -113,9 +116,9 @@ class TestSendRecurringNudge(CacheIsolationTestCase):
@patch.object(tasks, '_recurring_nudge_schedule_send')
def test_no_course_overview(self, mock_schedule_send):
schedule = ScheduleFactory.create(
start=datetime.datetime(2017, 8, 3, 20, 34, 30, tzinfo=pytz.UTC),
enrollment__user=UserFactory.create(),
)
schedule.enrollment.course_id = CourseKey.from_string('edX/toy/Not_2012_Fall')
schedule.enrollment.save()
@@ -123,8 +126,7 @@ class TestSendRecurringNudge(CacheIsolationTestCase):
test_time = datetime.datetime(2017, 8, 3, 20, tzinfo=pytz.UTC)
test_time_str = serialize(test_time)
for b in range(tasks.RECURRING_NUDGE_NUM_BINS):
# waffle flag takes an extra query before it is cached
with self.assertNumQueries(3 if b == 0 else 2):
with self.assertNumQueries(NUM_QUERIES_NO_MATCHING_SCHEDULES, table_blacklist=WAFFLE_TABLES):
tasks.recurring_nudge_schedule_bin(
self.site_config.site.id, target_day_str=test_time_str, day_offset=-3, bin_num=b,
org_list=[schedule.enrollment.course.org],
@@ -196,7 +198,7 @@ class TestSendRecurringNudge(CacheIsolationTestCase):
test_time = datetime.datetime(2017, 8, 3, 17, tzinfo=pytz.UTC)
test_time_str = serialize(test_time)
with self.assertNumQueries(3):
with self.assertNumQueries(NUM_QUERIES_WITH_MATCHES, table_blacklist=WAFFLE_TABLES):
tasks.recurring_nudge_schedule_bin(
limited_config.site.id, target_day_str=test_time_str, day_offset=-3, bin_num=0,
org_list=org_list, exclude_orgs=exclude_orgs,
@@ -220,7 +222,7 @@ class TestSendRecurringNudge(CacheIsolationTestCase):
test_time = datetime.datetime(2017, 8, 3, 19, 44, 30, tzinfo=pytz.UTC)
test_time_str = serialize(test_time)
with self.assertNumQueries(3):
with self.assertNumQueries(NUM_QUERIES_WITH_MATCHES, table_blacklist=WAFFLE_TABLES):
tasks.recurring_nudge_schedule_bin(
self.site_config.site.id, target_day_str=test_time_str, day_offset=-3,
bin_num=user.id % tasks.RECURRING_NUDGE_NUM_BINS,
@@ -255,21 +257,21 @@ class TestSendRecurringNudge(CacheIsolationTestCase):
sent_messages = []
templates_override = deepcopy(settings.TEMPLATES)
templates_override[0]['OPTIONS']['string_if_invalid'] = "TEMPLATE WARNING - MISSING VARIABLE [%s]"
with self.settings(TEMPLATES=templates_override):
with self.settings(TEMPLATES=self._get_template_overrides()):
with patch.object(tasks, '_recurring_nudge_schedule_send') as mock_schedule_send:
mock_schedule_send.apply_async = lambda args, *_a, **_kw: sent_messages.append(args)
with self.assertNumQueries(3):
with self.assertNumQueries(NUM_QUERIES_WITH_MATCHES, table_blacklist=WAFFLE_TABLES):
tasks.recurring_nudge_schedule_bin(
self.site_config.site.id, target_day_str=test_time_str, day_offset=day,
bin_num=user.id % tasks.RECURRING_NUDGE_NUM_BINS, org_list=[schedules[0].enrollment.course.org],
bin_num=self._calculate_bin_for_user(user), org_list=[schedules[0].enrollment.course.org],
)
self.assertEqual(len(sent_messages), 1)
with self.assertNumQueries(SEND_QUERIES):
# Load the site
# Check the schedule config
with self.assertNumQueries(2):
for args in sent_messages:
tasks._recurring_nudge_schedule_send(*args)
@@ -277,3 +279,142 @@ class TestSendRecurringNudge(CacheIsolationTestCase):
for (_name, (_msg, email), _kwargs) in mock_channel.deliver.mock_calls:
for template in attr.astuple(email):
self.assertNotIn("TEMPLATE WARNING", template)
def test_user_in_course_with_verified_coursemode_receives_upsell(self):
user = UserFactory.create()
course_id = CourseLocator('edX', 'toy', 'Course1')
first_day_of_schedule = datetime.datetime.now(pytz.UTC)
verification_deadline = first_day_of_schedule + datetime.timedelta(days=21)
target_day = first_day_of_schedule
target_hour_as_string = serialize(target_day)
nudge_day = 3
schedule = ScheduleFactory.create(start=first_day_of_schedule,
enrollment__user=user,
enrollment__course__id=course_id)
schedule.enrollment.course.self_paced = True
schedule.enrollment.course.save()
CourseModeFactory(
course_id=course_id,
mode_slug=CourseMode.VERIFIED,
expiration_datetime=verification_deadline
)
schedule.upgrade_deadline = verification_deadline
bin_task_parameters = [
target_hour_as_string,
nudge_day,
user,
schedule.enrollment.course.org
]
sent_messages = self._stub_sender_and_collect_sent_messages(bin_task=tasks.recurring_nudge_schedule_bin,
stubbed_send_task=patch.object(tasks, '_recurring_nudge_schedule_send'),
bin_task_params=bin_task_parameters)
self.assertEqual(len(sent_messages), 1)
message_attributes = sent_messages[0][1]
self.assertTrue(self._contains_upsell_attribute(message_attributes))
def test_no_upsell_button_when_DUDConfiguration_is_off(self):
DynamicUpgradeDeadlineConfiguration.objects.create(enabled=False)
user = UserFactory.create()
course_id = CourseLocator('edX', 'toy', 'Course1')
first_day_of_schedule = datetime.datetime.now(pytz.UTC)
target_day = first_day_of_schedule
target_hour_as_string = serialize(target_day)
nudge_day = 3
schedule = ScheduleFactory.create(start=first_day_of_schedule,
enrollment__user=user,
enrollment__course__id=course_id)
schedule.enrollment.course.self_paced = True
schedule.enrollment.course.save()
bin_task_parameters = [
target_hour_as_string,
nudge_day,
user,
schedule.enrollment.course.org
]
sent_messages = self._stub_sender_and_collect_sent_messages(bin_task=tasks.recurring_nudge_schedule_bin,
stubbed_send_task=patch.object(tasks, '_recurring_nudge_schedule_send'),
bin_task_params=bin_task_parameters)
self.assertEqual(len(sent_messages), 1)
message_attributes = sent_messages[0][1]
self.assertFalse(self._contains_upsell_attribute(message_attributes))
def test_user_with_no_upgrade_deadline_is_not_upsold(self):
user = UserFactory.create()
course_id = CourseLocator('edX', 'toy', 'Course1')
first_day_of_schedule = datetime.datetime.now(pytz.UTC)
target_day = first_day_of_schedule
target_hour_as_string = serialize(target_day)
nudge_day = 3
schedule = ScheduleFactory.create(start=first_day_of_schedule,
upgrade_deadline=None,
enrollment__user=user,
enrollment__course__id=course_id)
schedule.enrollment.course.self_paced = True
schedule.enrollment.course.save()
verification_deadline = first_day_of_schedule + datetime.timedelta(days=21)
CourseModeFactory(
course_id=course_id,
mode_slug=CourseMode.VERIFIED,
expiration_datetime=verification_deadline
)
schedule.upgrade_deadline = verification_deadline
bin_task_parameters = [
target_hour_as_string,
nudge_day,
user,
schedule.enrollment.course.org
]
sent_messages = self._stub_sender_and_collect_sent_messages(bin_task=tasks.recurring_nudge_schedule_bin,
stubbed_send_task=patch.object(tasks, '_recurring_nudge_schedule_send'),
bin_task_params=bin_task_parameters)
self.assertEqual(len(sent_messages), 1)
message_attributes = sent_messages[0][1]
self.assertFalse(self._contains_upsell_attribute(message_attributes))
def _stub_sender_and_collect_sent_messages(self, bin_task, stubbed_send_task, bin_task_params):
sent_messages = []
with self.settings(TEMPLATES=self._get_template_overrides()), stubbed_send_task as mock_schedule_send:
mock_schedule_send.apply_async = lambda args, *_a, **_kw: sent_messages.append(args)
bin_task(
self.site_config.site.id,
target_day_str=bin_task_params[0],
day_offset=bin_task_params[1],
bin_num=self._calculate_bin_for_user(bin_task_params[2]),
org_list=[bin_task_params[3]]
)
return sent_messages
def _get_template_overrides(self):
templates_override = deepcopy(settings.TEMPLATES)
templates_override[0]['OPTIONS']['string_if_invalid'] = "TEMPLATE WARNING - MISSING VARIABLE [%s]"
return templates_override
def _calculate_bin_for_user(self, user):
return user.id % tasks.RECURRING_NUDGE_NUM_BINS
def _contains_upsell_attribute(self, msg_attr):
msg = Message.from_string(msg_attr)
tmp = msg.context["show_upsell"]
return msg.context["show_upsell"]

View File

@@ -14,21 +14,41 @@ from mock import Mock, patch
from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locator import CourseLocator
from course_modes.models import CourseMode
from course_modes.tests.factories import CourseModeFactory
from courseware.models import DynamicUpgradeDeadlineConfiguration
from openedx.core.djangoapps.schedules import resolvers, tasks
from openedx.core.djangoapps.schedules.management.commands import send_upgrade_reminder as reminder
from openedx.core.djangoapps.schedules.tests.factories import ScheduleConfigFactory, ScheduleFactory
from openedx.core.djangoapps.site_configuration.tests.factories import SiteConfigurationFactory, SiteFactory
from openedx.core.djangolib.testing.utils import CacheIsolationTestCase, skip_unless_lms
from openedx.core.djangoapps.waffle_utils.testutils import WAFFLE_TABLES
from openedx.core.djangolib.testing.utils import CacheIsolationTestCase, skip_unless_lms, FilteredQueryCountMixin
from student.tests.factories import UserFactory
# 1) Load the current django site
# 2) Query the schedules to find all of the template context information
NUM_QUERIES_NO_MATCHING_SCHEDULES = 2
# 3) Query all course modes for all courses in returned schedules
NUM_QUERIES_WITH_MATCHES = NUM_QUERIES_NO_MATCHING_SCHEDULES + 1
# 1) Global dynamic deadline switch
# 2) E-commerce configuration
NUM_QUERIES_WITH_DEADLINE = 2
NUM_COURSE_MODES_QUERIES = 1
@ddt.ddt
@skip_unless_lms
@skipUnless('openedx.core.djangoapps.schedules.apps.SchedulesConfig' in settings.INSTALLED_APPS,
"Can't test schedules if the app isn't installed")
class TestUpgradeReminder(CacheIsolationTestCase):
class TestUpgradeReminder(FilteredQueryCountMixin, CacheIsolationTestCase):
# pylint: disable=protected-access
ENABLED_CACHES = ['default']
def setUp(self):
super(TestUpgradeReminder, self).setUp()
@@ -74,20 +94,26 @@ class TestUpgradeReminder(CacheIsolationTestCase):
schedules = [
ScheduleFactory.create(
upgrade_deadline=datetime.datetime(2017, 8, 3, 18, 44, 30, tzinfo=pytz.UTC),
enrollment__user=UserFactory.create(),
enrollment__course__id=CourseLocator('edX', 'toy', 'Bin')
) for _ in range(schedule_count)
) for i in range(schedule_count)
]
bins_in_use = frozenset((s.enrollment.user.id % tasks.UPGRADE_REMINDER_NUM_BINS) for s in schedules)
test_time = datetime.datetime(2017, 8, 3, 18, tzinfo=pytz.UTC)
test_time_str = serialize(test_time)
for b in range(tasks.UPGRADE_REMINDER_NUM_BINS):
# waffle flag takes an extra query before it is cached
with self.assertNumQueries(3 if b == 0 else 2):
expected_queries = NUM_QUERIES_NO_MATCHING_SCHEDULES
if b in bins_in_use:
# to fetch course modes for valid schedules
expected_queries += NUM_COURSE_MODES_QUERIES
with self.assertNumQueries(expected_queries, table_blacklist=WAFFLE_TABLES):
tasks.upgrade_reminder_schedule_bin(
self.site_config.site.id, target_day_str=test_time_str, day_offset=2, bin_num=b,
org_list=[schedules[0].enrollment.course.org],
)
self.assertEqual(mock_schedule_send.apply_async.call_count, schedule_count)
self.assertFalse(mock_ace.send.called)
@@ -103,8 +129,7 @@ class TestUpgradeReminder(CacheIsolationTestCase):
test_time = datetime.datetime(2017, 8, 3, 20, tzinfo=pytz.UTC)
test_time_str = serialize(test_time)
for b in range(tasks.UPGRADE_REMINDER_NUM_BINS):
# waffle flag takes an extra query before it is cached
with self.assertNumQueries(3 if b == 0 else 2):
with self.assertNumQueries(NUM_QUERIES_NO_MATCHING_SCHEDULES, table_blacklist=WAFFLE_TABLES):
tasks.upgrade_reminder_schedule_bin(
self.site_config.site.id, target_day_str=test_time_str, day_offset=2, bin_num=b,
org_list=[schedule.enrollment.course.org],
@@ -176,7 +201,7 @@ class TestUpgradeReminder(CacheIsolationTestCase):
test_time = datetime.datetime(2017, 8, 3, 17, tzinfo=pytz.UTC)
test_time_str = serialize(test_time)
with self.assertNumQueries(3):
with self.assertNumQueries(NUM_QUERIES_WITH_MATCHES, table_blacklist=WAFFLE_TABLES):
tasks.upgrade_reminder_schedule_bin(
limited_config.site.id, target_day_str=test_time_str, day_offset=2, bin_num=0,
org_list=org_list, exclude_orgs=exclude_orgs,
@@ -200,7 +225,7 @@ class TestUpgradeReminder(CacheIsolationTestCase):
test_time = datetime.datetime(2017, 8, 3, 19, 44, 30, tzinfo=pytz.UTC)
test_time_str = serialize(test_time)
with self.assertNumQueries(3):
with self.assertNumQueries(NUM_QUERIES_WITH_MATCHES, table_blacklist=WAFFLE_TABLES):
tasks.upgrade_reminder_schedule_bin(
self.site_config.site.id, target_day_str=test_time_str, day_offset=2,
bin_num=user.id % tasks.UPGRADE_REMINDER_NUM_BINS,
@@ -212,18 +237,31 @@ class TestUpgradeReminder(CacheIsolationTestCase):
@ddt.data(*itertools.product((1, 10, 100), (2, 10)))
@ddt.unpack
def test_templates(self, message_count, day):
DynamicUpgradeDeadlineConfiguration.objects.create(enabled=True)
now = datetime.datetime.now(pytz.UTC)
future_date = now + datetime.timedelta(days=21)
user = UserFactory.create()
schedules = [
ScheduleFactory.create(
upgrade_deadline=datetime.datetime(2017, 8, 3, 19, 44, 30, tzinfo=pytz.UTC),
upgrade_deadline=future_date,
enrollment__user=user,
enrollment__course__id=CourseLocator('edX', 'toy', 'Course{}'.format(course_num))
)
for course_num in range(message_count)
]
test_time = datetime.datetime(2017, 8, 3, 19, tzinfo=pytz.UTC)
for schedule in schedules:
schedule.enrollment.course.self_paced = True
schedule.enrollment.course.save()
CourseModeFactory(
course_id=schedule.enrollment.course.id,
mode_slug=CourseMode.VERIFIED,
expiration_datetime=future_date
)
test_time = future_date
test_time_str = serialize(test_time)
patch_policies(self, [StubPolicy([ChannelType.PUSH])])
@@ -241,7 +279,10 @@ class TestUpgradeReminder(CacheIsolationTestCase):
with patch.object(tasks, '_upgrade_reminder_schedule_send') as mock_schedule_send:
mock_schedule_send.apply_async = lambda args, *_a, **_kw: sent_messages.append(args)
with self.assertNumQueries(3):
# we execute one query per course to see if it's opted out of dynamic upgrade deadlines, however,
# since we create a new course for each schedule in this test, we expect there to be one per message
num_expected_queries = NUM_QUERIES_WITH_MATCHES + NUM_QUERIES_WITH_DEADLINE + message_count
with self.assertNumQueries(num_expected_queries, table_blacklist=WAFFLE_TABLES):
tasks.upgrade_reminder_schedule_bin(
self.site_config.site.id, target_day_str=test_time_str, day_offset=day,
bin_num=user.id % tasks.UPGRADE_REMINDER_NUM_BINS,

View File

@@ -112,7 +112,7 @@ def _get_upgrade_deadline_delta_setting(course_id):
# Check if the course has a deadline
course_config = CourseDynamicUpgradeDeadlineConfiguration.current(course_id)
if course_config.enabled:
if course_config.enabled and not course_config.opt_out:
delta = course_config.deadline_days
return delta

View File

@@ -12,13 +12,15 @@ from django.core.urlresolvers import reverse
from django.db.models import F, Min
from django.db.utils import DatabaseError
from django.utils.formats import dateformat, get_format
import pytz
from edx_ace import ace
from edx_ace.message import Message
from edx_ace.recipient import Recipient
from edx_ace.utils.date import deserialize
from opaque_keys.edx.keys import CourseKey
from lms.djangoapps.experiments.utils import check_and_get_upgrade_link_and_date
from courseware.date_summary import verified_upgrade_deadline_link, verified_upgrade_link_is_valid
from edxmako.shortcuts import marketing_link
from openedx.core.djangoapps.schedules.message_type import ScheduleMessageType
@@ -134,7 +136,7 @@ def _recurring_nudge_schedules_for_hour(site, target_hour, org_list, exclude_org
}
# Information for including upsell messaging in template.
_add_upsell_button_to_email_template(user, first_schedule, template_context)
_add_upsell_button_information_to_template_context(user, first_schedule, template_context)
yield (user, first_schedule.enrollment.course.language, template_context)
@@ -178,27 +180,6 @@ def _gather_users_and_schedules_for_target_hour(target_hour, org_list, exclude_o
return users, schedules
def _add_upsell_button_to_email_template(a_user, a_schedule, template_context):
# Check and upgrade link performs a query on CourseMode, which is triggering failures in
# test_send_recurring_nudge.py
upgrade_link, upgrade_date = check_and_get_upgrade_link_and_date(a_user, a_schedule.enrollment)
has_dynamic_deadline = a_schedule.upgrade_deadline is not None
has_upgrade_link = upgrade_link is not None
show_upsell = has_dynamic_deadline and has_upgrade_link
template_context['show_upsell'] = show_upsell
if show_upsell:
template_context['upsell_link'] = upgrade_link
template_context['user_schedule_upgrade_deadline_time'] = dateformat.format(
upgrade_date,
get_format(
'DATE_FORMAT',
lang=a_schedule.enrollment.course.language,
use_l10n=True
)
)
@task(ignore_result=True, routing_key=ROUTING_KEY)
def recurring_nudge_schedule_bin(
site_id, target_day_str, day_offset, bin_num, org_list, exclude_orgs=False, override_recipient_email=None,
@@ -254,7 +235,7 @@ def _recurring_nudge_schedules_for_bin(site, target_day, bin_num, org_list, excl
})
# Information for including upsell messaging in template.
_add_upsell_button_to_email_template(user, first_schedule, template_context)
_add_upsell_button_information_to_template_context(user, first_schedule, template_context)
yield (user, first_schedule.enrollment.course.language, template_context)
@@ -335,7 +316,7 @@ def _upgrade_reminder_schedules_for_bin(site, target_day, bin_num, org_list, exc
'cert_image': absolute_url(site, static('course_experience/images/verified-cert.png')),
})
_add_upsell_button_to_email_template(user, first_schedule, template_context)
_add_upsell_button_information_to_template_context(user, first_schedule, template_context)
yield (user, first_schedule.enrollment.course.language, template_context)
@@ -393,3 +374,32 @@ def get_schedules_with_target_date_by_bin_and_orgs(schedule_date_field, target_d
schedules = schedules.using("read_replica")
return schedules
def _add_upsell_button_information_to_template_context(user, schedule, template_context):
enrollment = schedule.enrollment
course = enrollment.course
verified_upgrade_link = _get_link_to_purchase_verified_certificate(user, schedule)
has_verified_upgrade_link = verified_upgrade_link is not None
if has_verified_upgrade_link:
template_context['upsell_link'] = verified_upgrade_link
template_context['user_schedule_upgrade_deadline_time'] = dateformat.format(
enrollment.dynamic_upgrade_deadline,
get_format(
'DATE_FORMAT',
lang=course.language,
use_l10n=True
)
)
template_context['show_upsell'] = has_verified_upgrade_link
def _get_link_to_purchase_verified_certificate(a_user, a_schedule):
enrollment = a_schedule.enrollment
if enrollment.dynamic_upgrade_deadline is None or not verified_upgrade_link_is_valid(enrollment):
return None
return verified_upgrade_deadline_link(a_user, enrollment.course)