-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Ability to configure edx-ace with course emails #29900
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
ACE message types for bulk course emails. | ||
""" | ||
|
||
from openedx.core.djangoapps.ace_common.message import BaseMessageType | ||
|
||
|
||
class BulkEmail(BaseMessageType): | ||
""" | ||
Course message to list of recepient by instructors. | ||
""" | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.options['from_address'] = kwargs['context']['from_address'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
""" | ||
Module to define email message related classes and methods | ||
""" | ||
from abc import ABC, abstractmethod | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.core.mail import EmailMultiAlternatives | ||
from edx_ace import ace | ||
from edx_ace.recipient import Recipient | ||
|
||
from lms.djangoapps.bulk_email.message_types import BulkEmail | ||
from openedx.core.lib.celery.task_utils import emulate_http_request | ||
|
||
User = get_user_model() | ||
|
||
|
||
class CourseEmailMessage(ABC): | ||
""" | ||
Abstract base class for course email messages | ||
""" | ||
|
||
@abstractmethod | ||
def send(self): | ||
""" | ||
Triggers sending of email message | ||
""" | ||
|
||
|
||
class DjangoEmail(CourseEmailMessage): | ||
""" | ||
Email message class to send email directly using django mail API. | ||
""" | ||
def __init__(self, connection, course_email, email_context): | ||
""" | ||
Construct message content using course_email model and context | ||
""" | ||
self.connection = connection | ||
template_context = email_context.copy() | ||
# use the CourseEmailTemplate that was associated with the CourseEmail | ||
course_email_template = course_email.get_template() | ||
|
||
plaintext_msg = course_email_template.render_plaintext(course_email.text_message, template_context) | ||
html_msg = course_email_template.render_htmltext(course_email.html_message, template_context) | ||
|
||
# Create email: | ||
message = EmailMultiAlternatives( | ||
course_email.subject, | ||
plaintext_msg, | ||
email_context['from_address'], | ||
[email_context['email']] | ||
) | ||
message.attach_alternative(html_msg, 'text/html') | ||
self.message = message | ||
|
||
def send(self): | ||
""" | ||
send email using already opened connection | ||
""" | ||
self.connection.send_messages([self.message]) | ||
|
||
|
||
class ACEEmail(CourseEmailMessage): | ||
""" | ||
Email message class to send email using edx-ace. | ||
""" | ||
def __init__(self, site, email_context): | ||
""" | ||
Construct edx-ace message using email_context | ||
""" | ||
self.site = site | ||
self.user = User.objects.get(email=email_context['email']) | ||
message = BulkEmail(context=email_context).personalize( | ||
recipient=Recipient(email_context['user_id'], email_context['email']), | ||
language=email_context['course_language'], | ||
user_context={"name": email_context['name']}, | ||
) | ||
self.message = message | ||
|
||
def send(self): | ||
""" | ||
Send message by emulating request in the context of site and user | ||
""" | ||
with emulate_http_request(site=self.site, user=self.user): | ||
ace.send(self.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
lms/templates/bulk_email/edx_ace/bulkemail/email/body.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{% extends 'ace_common/edx_ace/common/base_body.html' %} | ||
|
||
{% load i18n %} | ||
{% load static %} | ||
{% block content %} | ||
<table width="100%" align="left" border="0" cellpadding="0" cellspacing="0" role="presentation"> | ||
<tr> | ||
<td> | ||
<p style="color: rgba(0,0,0,.75);"> | ||
{% autoescape off %} | ||
{{ course_email.html_message }} | ||
{% endautoescape %} | ||
<br/> | ||
</p> | ||
|
||
<p style="font-size: 11px;"> | ||
{% filter force_escape %} | ||
{% blocktrans %} | ||
This email was automatically sent from {{ platform_name }}. | ||
{% endblocktrans %} | ||
{% endfilter %} | ||
<br> | ||
{% filter force_escape %} | ||
{% blocktrans %} | ||
You are receiving this email at address {{ email }} because you are enrolled in | ||
{% endblocktrans %} | ||
{% endfilter %} <a href='{{ course_url }}'>{{ course_title }}</a>. | ||
<br> | ||
{% filter force_escape %} | ||
{% blocktrans %} | ||
To stop receiving email like this, update your course email settings | ||
{% endblocktrans %} | ||
{% endfilter %} <a href='{{ email_settings_url }}'>{% trans "here" as transhere %}{{transhere|force_escape}}</a>. | ||
<br><a href='{{ unsubscribe_link }}'>{% trans "unsubscribe" as transunsub %}{{transunsub|force_escape}}</a> | ||
</p> | ||
</td> | ||
</tr> | ||
</table> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% load i18n %} | ||
{% autoescape off %} | ||
{{ course_email.text_message }} | ||
{% endautoescape %} | ||
|
||
|
||
{% blocktrans %}This email was automatically sent from {{ platform_name }}.{% endblocktrans %} | ||
{% blocktrans %}You are receiving this email at address {{ email }} because you are enrolled in {{course_title}}.{% endblocktrans %} | ||
{% blocktrans %}To stop receiving email like this, update your course email settings here {{ email_settings_url }}{% endblocktrans %} | ||
{% blocktrans %}To unsubscribe click here {{ unsubscribe_link }}{% endblocktrans %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{ from_address }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% extends 'ace_common/edx_ace/common/base_head.html' %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{% load i18n %} | ||
{% autoescape off %} | ||
{{ course_email.subject }} | ||
{% endautoescape %} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm no expert on the use of
edx-ace
, but I am used to seeing ACE messages inheriting from a common base message class namedBaseMessageType
. Example:edx-platform/lms/djangoapps/verify_student/message_types.py
Line 7 in 6042972
It's not apparent to me why we used a different (abstract) base class. Why is that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it to have a common
send()
function between the DjangoEmail and ACE implementations?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes to achieve polymorphism or have single method to send both types of emails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BaseMessageType
is limited to defining message types of edx-ace emails whereasCourseEmailMessage
is base class for two types of course emails we have