Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cms/djangoapps/course_creators/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
from smtplib import SMTPException

from crum import get_current_request
from django.conf import settings
from django.contrib import admin
from django.core.mail import send_mail
Expand All @@ -14,6 +15,8 @@
from course_creators.models import CourseCreator, send_admin_notification, send_user_notification, update_creator_state
from course_creators.views import update_course_creator_group
from edxmako.shortcuts import render_to_string
from openedx.features.edly.constants import ROLE_ASSIGNED, ROLE_REVOKED
from openedx.features.edly.utils import is_config_enabled

log = logging.getLogger("studio.coursecreatoradmin")

Expand Down Expand Up @@ -104,10 +107,13 @@ def send_user_notification_callback(sender, **kwargs):
else:
# changed to unrequested or pending
message_template = 'emails/course_creator_revoked.txt'
message = render_to_string(message_template, context)

message = render_to_string(message_template, context)
config_key = ROLE_ASSIGNED if updated_state == CourseCreator.GRANTED else ROLE_REVOKED
try:
user.email_user(subject, message, studio_request_email)
site = get_current_request().site
if is_config_enabled(site, config_key):
user.email_user(subject, message, studio_request_email)
except:
log.warning(u"Unable to send course creator status e-mail to %s", user.email)

Expand Down
16 changes: 10 additions & 6 deletions lms/djangoapps/instructor/enrollment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import pytz
import six
from crum import get_current_request
from django.conf import settings
from django.contrib.auth.models import User
from django.core.mail import send_mail
Expand All @@ -21,6 +22,8 @@
from edx_ace.recipient import Recipient
from eventtracking import tracker
from six import text_type
from openedx.features.edly.constants import COURSE_ENROLLMENT
from openedx.features.edly.utils import is_config_enabled
from submissions import api as sub_api # installed from the edx-submissions repository
from submissions.models import score_set

Expand Down Expand Up @@ -124,7 +127,7 @@ def get_user_email_language(user):

def enroll_email(
course_id, student_email, auto_enroll=False, email_students=False,
email_params=None, language=None, context_vars=None
email_params=None, language=None, context_vars=None, site=None
):
"""
Enroll a student by email.
Expand Down Expand Up @@ -165,7 +168,7 @@ def enroll_email(
email_params['message_type'] = 'enrolled_enroll'
email_params['email_address'] = student_email
email_params['full_name'] = previous_state.full_name
send_mail_to_student(student_email, email_params, language=language, context_vars=context_vars)
send_mail_to_student(student_email, email_params, language=language, context_vars=context_vars, site=site)

elif not is_email_retired(student_email):
cea, _ = CourseEnrollmentAllowed.objects.get_or_create(course_id=course_id, email=student_email)
Expand All @@ -174,7 +177,7 @@ def enroll_email(
if email_students:
email_params['message_type'] = 'allowed_enroll'
email_params['email_address'] = student_email
send_mail_to_student(student_email, email_params, language=language, context_vars=context_vars)
send_mail_to_student(student_email, email_params, language=language, context_vars=context_vars, site=site)

after_state = EmailEnrollmentState(course_id, student_email)

Expand Down Expand Up @@ -425,7 +428,7 @@ def get_email_params(course, auto_enroll, secure=True, course_key=None, display_
return email_params


def send_mail_to_student(student, param_dict, language=None, context_vars=None):
def send_mail_to_student(student, param_dict, language=None, context_vars=None, site=None):
"""
Construct the email using templates and then send it.
`student` is the student's email address (a `str`),
Expand All @@ -450,7 +453,7 @@ def send_mail_to_student(student, param_dict, language=None, context_vars=None):

Returns a boolean indicating whether the email was sent successfully.
"""

site = site or get_current_request().site
# Add some helpers and microconfig subsitutions
if 'display_name' in param_dict:
param_dict['course_name'] = param_dict['display_name']
Expand Down Expand Up @@ -490,7 +493,8 @@ def send_mail_to_student(student, param_dict, language=None, context_vars=None):
if from_email:
message.options.update({'from_address': from_email})

ace.send(message)
if is_config_enabled(site, COURSE_ENROLLMENT):
ace.send(message)


def render_message_to_string(subject_template, message_template, param_dict, language=None):
Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/instructor_task/tasks_helper/enrollments.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def enroll_user_to_course(request_info, course_id, username_or_email, site_name=
email_params = get_email_params(course=course, auto_enroll=auto_enroll, site_name=site_name)
__ = enroll_email(
course_id, email, auto_enroll, email_students,
email_params, language=language, context_vars=context_vars,
email_params, language=language, context_vars=context_vars, site=thread_site
)
if user:
TASK_LOG.info(
Expand Down
7 changes: 7 additions & 0 deletions openedx/features/edly/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@
WP_ADMIN_USERS = 'wp_admin_users'
COURSE_AUTHORS = 'course_authors'
PANEL_ADMINS = 'panel_admins'

# EMAIL CONFIGS
ACCOUNT_STATUS = 'account_status'
COURSE_ENROLLMENT = 'course_enrollment'
ROLE_ASSIGNED = 'role_assigned'
ROLE_REVOKED = 'role_revoked'
SUBSCRIPTION_EXPIRE = 'subscription_expire'
12 changes: 12 additions & 0 deletions openedx/features/edly/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,15 @@ def send_cert_email_to_course_staff(student_email, course_key, site_id, context_

msg.options['from_address'] = from_address
send_certificate_generation_email(msg, instructor_user, site)


def is_config_enabled(site, conf_key):
"""
Returns if the given email is enabled or not.
"""
site_conf = getattr(site, "configuration", None)
if not site_conf:
return True

email_conf = site_conf.get_value('EMAILS_CONFIG', {})
return email_conf.get(conf_key, True)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
from openedx.core.djangoapps.theming.helpers import get_config_value_from_site_or_settings
from openedx.core.djangoapps.user_api.preferences.api import get_user_preference
from openedx.core.lib.celery.task_utils import emulate_http_request
from openedx.features.edly.constants import SUBSCRIPTION_EXPIRE
from openedx.features.edly.context_processor import Colour
from openedx.features.edly.utils import is_config_enabled
from openedx.features.subscriptions.message_types import ExpiredNotification, ImpendingExpiryNotification
from openedx.features.subscriptions.models import UserSubscription
from openedx.features.subscriptions.utils import get_subscription_renew_url
Expand Down Expand Up @@ -94,8 +96,9 @@ def _send_email_notifications(self, context_values, ace_message_class):
language=get_user_preference(user, LANGUAGE_KEY),
user_context={'full_name': user.profile.name}
)
ace.send(msg)
logger.info('Expiry notification email sent to user: %r', user.username)
if is_config_enabled(site, SUBSCRIPTION_EXPIRE):
ace.send(msg)
logger.info('Expiry notification email sent to user: %r', user.username)

except Exception: # pylint: disable=broad-except
logger.exception('Could not send email for subscription expiry notification to user %s', user.username)
Expand Down