From 0c0a5a93c048d14d6983f5d97fc7f1c4bae6b8ed Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Wed, 20 Sep 2017 16:22:22 -0400 Subject: [PATCH] Create func for common template context entries --- openedx/core/djangoapps/schedules/tasks.py | 41 ++---------------- .../djangoapps/schedules/template_context.py | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+), 37 deletions(-) create mode 100644 openedx/core/djangoapps/schedules/template_context.py diff --git a/openedx/core/djangoapps/schedules/tasks.py b/openedx/core/djangoapps/schedules/tasks.py index 4ef6301d9a..8de9b0ad5a 100644 --- a/openedx/core/djangoapps/schedules/tasks.py +++ b/openedx/core/djangoapps/schedules/tasks.py @@ -19,9 +19,9 @@ from edx_ace.recipient import Recipient from edx_ace.utils.date import deserialize from opaque_keys.edx.keys import CourseKey -from edxmako.shortcuts import marketing_link from openedx.core.djangoapps.schedules.message_type import ScheduleMessageType from openedx.core.djangoapps.schedules.models import Schedule, ScheduleConfig +from openedx.core.djangoapps.schedules.template_context import absolute_url, get_base_template_context LOG = logging.getLogger(__name__) @@ -128,11 +128,9 @@ def _recurring_nudge_schedules_for_hour(target_hour, org_list, exclude_orgs=Fals user_schedules = list(user_schedules) course_id_strs = [str(schedule.enrollment.course_id) for schedule in user_schedules] - def absolute_url(relative_path): - return u'{}{}'.format(settings.LMS_ROOT_URL, urlquote(relative_path)) - first_schedule = user_schedules[0] - template_context = { + template_context = get_base_template_context() + template_context.update({ 'student_name': user.profile.name, 'course_name': first_schedule.enrollment.course.display_name, @@ -140,36 +138,5 @@ def _recurring_nudge_schedules_for_hour(target_hour, org_list, exclude_orgs=Fals # This is used by the bulk email optout policy 'course_ids': course_id_strs, - - # Platform information - 'homepage_url': encode_url(marketing_link('ROOT')), - 'dashboard_url': absolute_url(dashboard_relative_url), - 'template_revision': settings.EDX_PLATFORM_REVISION, - 'platform_name': settings.PLATFORM_NAME, - 'contact_mailing_address': settings.CONTACT_MAILING_ADDRESS, - 'social_media_urls': encode_urls_in_dict(getattr(settings, 'SOCIAL_MEDIA_FOOTER_URLS', {})), - 'mobile_store_urls': encode_urls_in_dict(getattr(settings, 'MOBILE_STORE_URLS', {})), - } + }) yield (user, first_schedule.enrollment.course.language, template_context) - - -def encode_url(url): - # Sailthru has a bug where URLs that contain "+" characters in their path components are misinterpreted - # when GA instrumentation is enabled. We need to percent-encode the path segments of all URLs that are - # injected into our templates to work around this issue. - parsed_url = urlparse(url) - modified_url = parsed_url._replace(path=urlquote(parsed_url.path)) - return modified_url.geturl() - - -def absolute_url(relative_path): - root = settings.LMS_ROOT_URL.rstrip('/') - relative_path = relative_path.lstrip('/') - return encode_url(u'{root}/{path}'.format(root=root, path=relative_path)) - - -def encode_urls_in_dict(mapping): - urls = {} - for key, value in mapping.iteritems(): - urls[key] = encode_url(value) - return urls diff --git a/openedx/core/djangoapps/schedules/template_context.py b/openedx/core/djangoapps/schedules/template_context.py new file mode 100644 index 0000000000..f04b0dd9d9 --- /dev/null +++ b/openedx/core/djangoapps/schedules/template_context.py @@ -0,0 +1,43 @@ +from urlparse import urlparse + +from django.conf import settings +from django.core.urlresolvers import reverse +from django.utils.http import urlquote + +from edxmako.shortcuts import marketing_link + + +def get_base_template_context(): + """Dict with entries needed for all templates that use the base template""" + return { + # Platform information + 'homepage_url': encode_url(marketing_link('ROOT')), + 'dashboard_url': absolute_url(reverse('dashboard')), + 'template_revision': settings.EDX_PLATFORM_REVISION, + 'platform_name': settings.PLATFORM_NAME, + 'contact_mailing_address': settings.CONTACT_MAILING_ADDRESS, + 'social_media_urls': encode_urls_in_dict(getattr(settings, 'SOCIAL_MEDIA_FOOTER_URLS', {})), + 'mobile_store_urls': encode_urls_in_dict(getattr(settings, 'MOBILE_STORE_URLS', {})), + } + + +def encode_url(url): + # Sailthru has a bug where URLs that contain "+" characters in their path components are misinterpreted + # when GA instrumentation is enabled. We need to percent-encode the path segments of all URLs that are + # injected into our templates to work around this issue. + parsed_url = urlparse(url) + modified_url = parsed_url._replace(path=urlquote(parsed_url.path)) + return modified_url.geturl() + + +def absolute_url(relative_path): + root = settings.LMS_ROOT_URL.rstrip('/') + relative_path = relative_path.lstrip('/') + return encode_url(u'{root}/{path}'.format(root=root, path=relative_path)) + + +def encode_urls_in_dict(mapping): + urls = {} + for key, value in mapping.iteritems(): + urls[key] = encode_url(value) + return urls