Commit 5444b932 by Awais Committed by Awais Qureshi

Send email on course-creation.

ECOM-6801
parent fef082e1
......@@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
def send_email_for_change_state(course_run):
""" Send the emails for a comment.
""" Send the emails for a course run change state event.
Arguments:
course_run (Object): CourseRun object
......@@ -95,3 +95,52 @@ def send_email_for_studio_instance_created(course_run):
email_msg.send()
except Exception: # pylint: disable=broad-except
logger.exception('Failed to send email notifications for course_run [%s]', course_run.id)
def send_email_for_course_creation(course, course_run):
""" Send the emails for a course creation.
Arguments:
course (Course): Course object
course_run (CourseRun): CourseRun object
"""
try:
txt_template = 'publisher/email/course_created.txt'
html_template = 'publisher/email/course_created.html'
to_addresses = course.get_course_users_emails()
from_address = settings.PUBLISHER_FROM_EMAIL
course_user_roles = course_run.course.course_user_roles.all()
course_team = course_user_roles.filter(role=PublisherUserRole.CourseTeam).first()
partner_coordinator = course_user_roles.filter(role=PublisherUserRole.PartnerCoordinator).first()
context = {
'course_title': course_run.course.title,
'date': course_run.created.strftime("%B %d, %Y"),
'time': course_run.created.strftime("%H:%M:%S"),
'course_team_name': course_team.user.full_name if course_team else '',
'partner_coordinator_name': partner_coordinator.user.full_name if partner_coordinator else '',
'dashboard_url': 'https://{host}{path}'.format(
host=Site.objects.get_current().domain.strip('/'), path=reverse('publisher:publisher_dashboard')
),
'from_address': from_address,
'contact_us_email': partner_coordinator.user.email if partner_coordinator else ''
}
template = get_template(txt_template)
plain_content = template.render(context)
template = get_template(html_template)
html_content = template.render(context)
subject = _('New Studio instance request for {title}').format(title=course.title) # pylint: disable=no-member
email_msg = EmailMultiAlternatives(
subject, plain_content, from_address, to=[settings.PUBLISHER_FROM_EMAIL], bcc=to_addresses
)
email_msg.attach_alternative(html_content, 'text/html')
email_msg.send()
except Exception: # pylint: disable=broad-except
logger.exception(
'Failed to send email notifications for course creation course run id [%s]', course_run.course.id
)
......@@ -139,10 +139,6 @@ class StudioInstanceCreatedEmailTests(TestCase):
def setUp(self):
super(StudioInstanceCreatedEmailTests, self).setUp()
self.user = UserFactory()
self.group = factories.GroupFactory()
self.user.groups.add(self.group)
self.course_run = factories.CourseRunFactory()
# add user in course-user-role table
......@@ -177,7 +173,6 @@ class StudioInstanceCreatedEmailTests(TestCase):
""" Verify that emails sent successfully for studio instance created."""
emails.send_email_for_studio_instance_created(self.course_run)
# assert email sent
self.assert_email_sent(
reverse('publisher:publisher_course_run_detail', kwargs={'pk': self.course_run.id}),
'Studio instance created',
......@@ -204,3 +199,62 @@ class StudioInstanceCreatedEmailTests(TestCase):
self.assertIn(
'For questions or comments, contact {}.'.format(self.user.email), body
)
class CourseCreatedEmailTests(TestCase):
""" Tests for the email functionality for new course created. """
def setUp(self):
super(CourseCreatedEmailTests, self).setUp()
self.user = UserFactory()
self.course_run = factories.CourseRunFactory()
# add user in course-user-role table
factories.CourseUserRoleFactory(
course=self.course_run.course, role=PublisherUserRole.PartnerCoordinator, user=self.user
)
self.course_team = UserFactory()
factories.CourseUserRoleFactory(
course=self.course_run.course, role=PublisherUserRole.CourseTeam, user=self.course_team
)
UserAttributeFactory(user=self.user, enable_email_notification=True)
toggle_switch('enable_publisher_email_notifications', True)
@mock.patch('django.core.mail.message.EmailMessage.send', mock.Mock(side_effect=TypeError))
def test_email_with_error(self):
""" Verify that emails failure log message."""
with LogCapture(emails.logger.name) as l:
emails.send_email_for_course_creation(self.course_run.course, self.course_run)
l.check(
(
emails.logger.name,
'ERROR',
'Failed to send email notifications for course creation course run id [{}]'.format(
self.course_run.id
)
)
)
def test_email_sent_successfully(self):
""" Verify that emails send as course creation notifications."""
emails.send_email_for_course_creation(self.course_run.course, self.course_run)
subject = 'New Studio instance request for {title}'.format(title=self.course_run.course.title)
self.assert_email_sent(subject)
def assert_email_sent(self, subject):
""" Verify the email data for tests cases."""
self.assertEqual(len(mail.outbox), 1)
self.assertEqual([settings.PUBLISHER_FROM_EMAIL], mail.outbox[0].to)
self.assertEqual([self.user.email, self.course_team.email], mail.outbox[0].bcc)
self.assertEqual(str(mail.outbox[0].subject), subject)
body = mail.outbox[0].body.strip()
self.assertIn('{name} created the'.format(name=self.course_team.full_name), body)
self.assertIn('{dashboard_url}'.format(dashboard_url=reverse('publisher:publisher_dashboard')), body)
self.assertIn('Please create a Studio instance for this course', body)
self.assertIn('Thanks', body)
......@@ -11,6 +11,7 @@ from django.conf import settings
from django.contrib.auth.models import Group
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.core import mail
from django.forms import model_to_dict
from django.test import TestCase
from guardian.shortcuts import assign_perm
......@@ -388,6 +389,10 @@ class CreateUpdateCourseViewTests(TestCase):
self.assertEqual(course.organizations.first(), self.organization_extension.organization)
self.assertTrue(len(course.course_user_roles.all()), 2)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(
str(mail.outbox[0].subject), 'New Studio instance request for {title}'.format(title=course.title)
)
class CreateUpdateCourseRunViewTests(TestCase):
......
......@@ -18,6 +18,7 @@ from guardian.shortcuts import get_objects_for_user
from course_discovery.apps.core.models import User
from course_discovery.apps.publisher.choices import PublisherUserRole
from course_discovery.apps.publisher.emails import send_email_for_course_creation
from course_discovery.apps.publisher.forms import (
CourseForm, CourseRunForm, SeatForm, CustomCourseForm, CustomCourseRunForm,
CustomSeatForm, UpdateCourseForm
......@@ -247,6 +248,9 @@ class CreateCourseView(mixins.LoginRequiredMixin, mixins.PublisherUserRequiredMi
'EdX will create a Studio instance for this course. You will receive a notification message at '
'{email} when the Studio instance has been created.').format(email=request.user.email))
# sending email for notifying new course is created.
send_email_for_course_creation(course, run_course)
return HttpResponseRedirect(self.get_success_url(run_course.id))
except Exception as e: # pylint: disable=broad-except
# pylint: disable=no-member
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-01-13 15:33-0500\n"
"POT-Creation-Date: 2017-01-16 14:06+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -436,6 +436,11 @@ msgstr ""
msgid "Studio instance created"
msgstr ""
#: apps/publisher/emails.py
#, python-brace-format
msgid "New Studio instance request for {title}"
msgstr ""
#: apps/publisher/forms.py
msgid "Organization Name"
msgstr ""
......@@ -2005,6 +2010,8 @@ msgstr ""
#: templates/publisher/email/change_state.txt
#: templates/publisher/email/comment.html
#: templates/publisher/email/comment.txt
#: templates/publisher/email/course_created.html
#: templates/publisher/email/course_created.txt
msgid "The edX team"
msgstr ""
......@@ -2028,6 +2035,48 @@ msgstr ""
msgid "View comment: "
msgstr ""
#. Translators: partner_coordinator_name is a member name.
#: templates/publisher/email/course_created.html
#, python-format
msgid "Dear %(partner_coordinator_name)s,"
msgstr ""
#: templates/publisher/email/course_created.html
#, python-format
msgid ""
"%(course_team_name)s created the "
"%(link_start)s%(dashboard_url)s%(link_middle)s %(course_title)s %(link_end)s"
" course in Publisher on %(date)s at %(time)s."
msgstr ""
#: templates/publisher/email/course_created.html
#: templates/publisher/email/course_created.txt
msgid "Please create a Studio instance for this course."
msgstr ""
#. Translators: It's closing of mail.
#: templates/publisher/email/course_created.html
#: templates/publisher/email/course_created.txt
#: templates/publisher/email/studio_instance_created.html
#: templates/publisher/email/studio_instance_created.txt
#: templates/publisher/email/studio_instance_needed.html
#: templates/publisher/email/studio_instance_needed.txt
msgid "Thanks,"
msgstr ""
#: templates/publisher/email/course_created.txt
#: templates/publisher/email/studio_instance_needed.html
#: templates/publisher/email/studio_instance_needed.txt
msgid "Dear"
msgstr ""
#: templates/publisher/email/course_created.txt
#, python-format
msgid ""
"%(course_team_name)s created the %(course_title)s : %(dashboard_url)s course"
" in Publisher on %(date)s at %(time)s."
msgstr ""
#. Translators: course_team_name is course team member name.
#: templates/publisher/email/studio_instance_created.html
#: templates/publisher/email/studio_instance_created.txt
......@@ -2091,14 +2140,6 @@ msgid ""
"build your course. We recommend that you use the list early!"
msgstr ""
#. Translators: It's closing of mail.
#: templates/publisher/email/studio_instance_created.html
#: templates/publisher/email/studio_instance_created.txt
#: templates/publisher/email/studio_instance_needed.html
#: templates/publisher/email/studio_instance_needed.txt
msgid "Thanks,"
msgstr ""
#: templates/publisher/email/studio_instance_created.html
#, python-format
msgid ""
......@@ -2122,11 +2163,6 @@ msgstr ""
#: templates/publisher/email/studio_instance_needed.html
#: templates/publisher/email/studio_instance_needed.txt
msgid "Dear"
msgstr ""
#: templates/publisher/email/studio_instance_needed.html
#: templates/publisher/email/studio_instance_needed.txt
msgid "Please create a Studio instance for the following course."
msgstr ""
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-01-13 15:33-0500\n"
"POT-Creation-Date: 2017-01-16 14:06+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-01-13 15:33-0500\n"
"POT-Creation-Date: 2017-01-16 14:06+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -544,6 +544,13 @@ msgstr ""
msgid "Studio instance created"
msgstr "Stüdïö ïnstänçé çréätéd Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σ#"
#: apps/publisher/emails.py
#, python-brace-format
msgid "New Studio instance request for {title}"
msgstr ""
"Néw Stüdïö ïnstänçé réqüést för {title} Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, "
"¢σηѕє¢тєт#"
#: apps/publisher/forms.py
msgid "Organization Name"
msgstr "Örgänïzätïön Nämé Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмє#"
......@@ -2370,6 +2377,8 @@ msgstr "Vïéw Çöürsé Ⱡ'σяєм ιρѕυм ∂σłσя #"
#: templates/publisher/email/change_state.txt
#: templates/publisher/email/comment.html
#: templates/publisher/email/comment.txt
#: templates/publisher/email/course_created.html
#: templates/publisher/email/course_created.txt
msgid "The edX team"
msgstr "Thé édX téäm Ⱡ'σяєм ιρѕυм ∂σłσя ѕ#"
......@@ -2395,6 +2404,57 @@ msgstr "Vïéw çömmént Ⱡ'σяєм ιρѕυм ∂σłσя ѕ#"
msgid "View comment: "
msgstr "Vïéw çömmént: Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт#"
#. Translators: partner_coordinator_name is a member name.
#: templates/publisher/email/course_created.html
#, python-format
msgid "Dear %(partner_coordinator_name)s,"
msgstr "Déär %(partner_coordinator_name)s, Ⱡ'σяєм ιρѕυм ∂σł#"
#: templates/publisher/email/course_created.html
#, python-format
msgid ""
"%(course_team_name)s created the "
"%(link_start)s%(dashboard_url)s%(link_middle)s %(course_title)s %(link_end)s"
" course in Publisher on %(date)s at %(time)s."
msgstr ""
"%(course_team_name)s çréätéd thé "
"%(link_start)s%(dashboard_url)s%(link_middle)s %(course_title)s %(link_end)s"
" çöürsé ïn Püßlïshér ön %(date)s ät %(time)s. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, "
"¢σηѕє¢тєтυя #"
#: templates/publisher/email/course_created.html
#: templates/publisher/email/course_created.txt
msgid "Please create a Studio instance for this course."
msgstr ""
"Pléäsé çréäté ä Stüdïö ïnstänçé för thïs çöürsé. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт "
"αмєт, ¢σηѕє¢тєтυя α#"
#. Translators: It's closing of mail.
#: templates/publisher/email/course_created.html
#: templates/publisher/email/course_created.txt
#: templates/publisher/email/studio_instance_created.html
#: templates/publisher/email/studio_instance_created.txt
#: templates/publisher/email/studio_instance_needed.html
#: templates/publisher/email/studio_instance_needed.txt
msgid "Thanks,"
msgstr "Thänks, Ⱡ'σяєм ιρѕυм #"
#: templates/publisher/email/course_created.txt
#: templates/publisher/email/studio_instance_needed.html
#: templates/publisher/email/studio_instance_needed.txt
msgid "Dear"
msgstr "Déär Ⱡ'σяєм ι#"
#: templates/publisher/email/course_created.txt
#, python-format
msgid ""
"%(course_team_name)s created the %(course_title)s : %(dashboard_url)s course"
" in Publisher on %(date)s at %(time)s."
msgstr ""
"%(course_team_name)s çréätéd thé %(course_title)s : %(dashboard_url)s çöürsé"
" ïn Püßlïshér ön %(date)s ät %(time)s. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, "
"¢σηѕє¢тєтυя α#"
#. Translators: course_team_name is course team member name.
#: templates/publisher/email/studio_instance_created.html
#: templates/publisher/email/studio_instance_created.txt
......@@ -2486,14 +2546,6 @@ msgstr ""
"ησѕтяυ∂ єχєя¢ιтαтιση υłłαм¢σ łαвσяιѕ ηιѕι υт αłιqυιρ єχ єα ¢σммσ∂σ "
"¢σηѕєqυαт. ∂υιѕ αυтє ιяυяє ∂σłσя ιη яєρяєнєη∂єяιт ιη νσłυρтαтє νєł#"
#. Translators: It's closing of mail.
#: templates/publisher/email/studio_instance_created.html
#: templates/publisher/email/studio_instance_created.txt
#: templates/publisher/email/studio_instance_needed.html
#: templates/publisher/email/studio_instance_needed.txt
msgid "Thanks,"
msgstr "Thänks, Ⱡ'σяєм ιρѕυм #"
#: templates/publisher/email/studio_instance_created.html
#, python-format
msgid ""
......@@ -2524,11 +2576,6 @@ msgstr ""
#: templates/publisher/email/studio_instance_needed.html
#: templates/publisher/email/studio_instance_needed.txt
msgid "Dear"
msgstr "Déär Ⱡ'σяєм ι#"
#: templates/publisher/email/studio_instance_needed.html
#: templates/publisher/email/studio_instance_needed.txt
msgid "Please create a Studio instance for the following course."
msgstr ""
"Pléäsé çréäté ä Stüdïö ïnstänçé för thé föllöwïng çöürsé. Ⱡ'σяєм ιρѕυм ∂σłσя"
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-01-13 15:33-0500\n"
"POT-Creation-Date: 2017-01-16 14:06+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......
{% extends "publisher/email/email_base.html" %}
{% load i18n %}
{% block body %}
<!-- Message Body -->
<p>
{# Translators: partner_coordinator_name is a member name. #}
{% blocktrans trimmed %}
Dear {{ partner_coordinator_name }},
{% endblocktrans %}
<p>
<p>
{% blocktrans with link_start='<a href="' link_middle='">' link_end='</a>' trimmed %}
{{ course_team_name }} created the {{ link_start }}{{ dashboard_url }}{{ link_middle }} {{ course_title }} {{ link_end }} course in Publisher on {{ date }} at {{ time }}.
{% endblocktrans %}
</p>
<p>{% trans "Please create a Studio instance for this course." %}</p>
{# Translators: It's closing of mail. #}
<p>{% trans "Thanks," %}</p>
<p>{% trans "The edX team" %}</p>
<!-- End Message Body -->
{% endblock body %}
{% load i18n %}
{% trans "Dear" %} {{ partner_coordinator_name }},
{% blocktrans trimmed %}
{{ course_team_name }} created the {{ course_title }} : {{ dashboard_url }} course in Publisher on {{ date }} at {{ time }}.
{% endblocktrans %}
{% trans "Please create a Studio instance for this course." %}
{% trans "Thanks," %}
{% trans "The edX team" %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment