From b3b4b26929213c16d0994f954c0ee36fcc724137 Mon Sep 17 00:00:00 2001 From: danialmalik Date: Mon, 7 Sep 2020 13:24:08 +0500 Subject: [PATCH 1/2] add to bulk email context in celery task --- lms/djangoapps/bulk_email/tasks.py | 21 +++++++++++++++++---- lms/djangoapps/instructor_task/api.py | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lms/djangoapps/bulk_email/tasks.py b/lms/djangoapps/bulk_email/tasks.py index aad51207bfd8..ca17489d13f3 100644 --- a/lms/djangoapps/bulk_email/tasks.py +++ b/lms/djangoapps/bulk_email/tasks.py @@ -31,6 +31,7 @@ from celery.states import FAILURE, RETRY, SUCCESS from django.conf import settings from django.contrib.auth.models import User +from django.contrib.sites.models import Site from django.core.mail import EmailMultiAlternatives, get_connection from django.core.mail.message import forbid_multi_line_headers from django.urls import reverse @@ -49,6 +50,7 @@ update_subtask_status ) from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers +from openedx.core.lib.celery.task_utils import emulate_http_request from openedx.core.lib.courses import course_image_url from util.date_utils import get_default_time_display @@ -109,16 +111,17 @@ def _get_course_email_context(course): course_root ) lms_url = settings.LMS_ROOT_URL - logo_url = get_logo_url() + logo_url = get_logo_url(is_secure=(not settings.DEBUG)) image_url = u'{}{}'.format(settings.LMS_ROOT_URL, course_image_url(course)) + email_context = { 'course_title': course_title, 'course_root': course_root, 'course_language': course.language, 'course_url': course_url, 'course_image_url': image_url, - 'lms_url': lms_url, - 'logo_url': logo_url, + 'lms_url': lms_url, + 'logo_url': logo_url, 'course_end_date': course_end_date, 'account_settings_url': '{}{}'.format(settings.LMS_ROOT_URL, reverse('account_settings')), 'email_settings_url': '{}{}'.format(settings.LMS_ROOT_URL, reverse('dashboard')), @@ -137,6 +140,7 @@ def perform_delegate_email_batches(entry_id, course_id, task_input, action_name) # Get inputs to use in this task from the entry. user_id = entry.requester.id task_id = entry.task_id + site_id = task_input.get('site', None) # Perfunctory check, since expansion is made for convenience of other task # code that doesn't need the entry_id. @@ -178,7 +182,16 @@ def perform_delegate_email_batches(entry_id, course_id, task_input, action_name) # Get arguments that will be passed to every subtask. targets = email_obj.targets.all() - global_email_context = _get_course_email_context(course) + + # [UCSD_CUSTOM] + # If `site_id` was passed in the task_input, get the site and derive + # context as per the provided site, otherwise simply get the email + # context. + try: + with emulate_http_request(site=Site.objects.get(id=site_id)): + global_email_context = _get_course_email_context(course) + except Site.DoesNotExist: + global_email_context = _get_course_email_context(course) recipient_qsets = [ target.get_users(course_id, user_id) diff --git a/lms/djangoapps/instructor_task/api.py b/lms/djangoapps/instructor_task/api.py index 9cd38750d273..5db76e4b7bf1 100644 --- a/lms/djangoapps/instructor_task/api.py +++ b/lms/djangoapps/instructor_task/api.py @@ -314,7 +314,7 @@ def submit_bulk_course_email(request, course_key, email_id): task_type = 'bulk_course_email' task_class = send_bulk_course_email - task_input = {'email_id': email_id, 'to_option': targets} + task_input = {'email_id': email_id, 'to_option': targets, 'site': request.site.id} task_key_stub = str(email_id) # create the key value by using MD5 hash: task_key = hashlib.md5(task_key_stub).hexdigest() From 4bebc403b86980ce4f6817894fa757b0df95a713 Mon Sep 17 00:00:00 2001 From: danialmalik Date: Tue, 8 Sep 2020 14:15:52 +0500 Subject: [PATCH 2/2] Fix tests --- lms/djangoapps/bulk_email/tasks.py | 2 +- lms/djangoapps/instructor_task/tests/test_api.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lms/djangoapps/bulk_email/tasks.py b/lms/djangoapps/bulk_email/tasks.py index ca17489d13f3..3e0f6f18111d 100644 --- a/lms/djangoapps/bulk_email/tasks.py +++ b/lms/djangoapps/bulk_email/tasks.py @@ -188,7 +188,7 @@ def perform_delegate_email_batches(entry_id, course_id, task_input, action_name) # context as per the provided site, otherwise simply get the email # context. try: - with emulate_http_request(site=Site.objects.get(id=site_id)): + with emulate_http_request(site=Site.objects.get(id=site_id), user=entry.requester): global_email_context = _get_course_email_context(course) except Site.DoesNotExist: global_email_context = _get_course_email_context(course) diff --git a/lms/djangoapps/instructor_task/tests/test_api.py b/lms/djangoapps/instructor_task/tests/test_api.py index 01ee8b94dc99..47a86f118c04 100644 --- a/lms/djangoapps/instructor_task/tests/test_api.py +++ b/lms/djangoapps/instructor_task/tests/test_api.py @@ -249,9 +249,11 @@ def _test_resubmission(self, api_call): api_call() def test_submit_bulk_email_all(self): + request = self.create_task_request(self.instructor) + request.site.id = None email_id = self._define_course_email() api_call = lambda: submit_bulk_course_email( - self.create_task_request(self.instructor), + request, self.course.id, email_id )