Commit e3179c37 by Sarina Canelake

Clean up Email section of legacy dashboard

parent d4a06794
"""
Unit tests for email feature flag in legacy instructor dashboard.
Additionally tests that bulk email is always disabled for non-Mongo
backed courses, regardless of email feature flag, and that the
view is conditionally available when Course Auth is turned on.
"""
from django.test.utils import override_settings
from django.conf import settings
from django.core.urlresolvers import reverse
from mock import patch
from xmodule.modulestore.tests.django_utils import TEST_DATA_MOCK_MODULESTORE
from student.tests.factories import AdminFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory
from xmodule.modulestore import ModuleStoreEnum
from bulk_email.models import CourseAuthorization
@override_settings(MODULESTORE=TEST_DATA_MOCK_MODULESTORE)
class TestInstructorDashboardEmailView(ModuleStoreTestCase):
"""
Check for email view displayed with flag
"""
def setUp(self):
self.course = CourseFactory.create()
# Create instructor account
instructor = AdminFactory.create()
self.client.login(username=instructor.username, password="test")
# URL for instructor dash
self.url = reverse('instructor_dashboard_legacy', kwargs={'course_id': self.course.id.to_deprecated_string()})
# URL for email view
self.email_link = '<a href="#" onclick="goto(\'Email\')" class="None">Email</a>'
def tearDown(self):
"""
Undo all patches.
"""
patch.stopall()
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False})
def test_email_flag_true(self):
# Assert that the URL for the email view is in the response
response = self.client.get(self.url)
self.assertTrue(self.email_link in response.content)
# Select the Email view of the instructor dash
session = self.client.session
session[u'idash_mode:{0}'.format(self.course.location.course_key.to_deprecated_string())] = 'Email'
session.save()
response = self.client.get(self.url)
# Ensure we've selected the view properly and that the send_to field is present.
selected_email_link = '<a href="#" onclick="goto(\'Email\')" class="selectedmode">Email</a>'
self.assertTrue(selected_email_link in response.content)
send_to_label = '<label for="id_to">Send to:</label>'
self.assertTrue(send_to_label in response.content)
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': True})
def test_email_flag_unauthorized(self):
# Assert that the URL for the email view is not in the response
# email is enabled, but this course is not authorized to send email
response = self.client.get(self.url)
self.assertFalse(self.email_link in response.content)
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': True})
def test_email_flag_authorized(self):
# Assert that the URL for the email view is in the response
# email is enabled, and this course is authorized to send email
# Assert that instructor email is not enabled for this course
self.assertFalse(CourseAuthorization.instructor_email_enabled(self.course.id))
response = self.client.get(self.url)
self.assertFalse(self.email_link in response.content)
# Authorize the course to use email
cauth = CourseAuthorization(course_id=self.course.id, email_enabled=True)
cauth.save()
# Assert that instructor email is enabled for this course
self.assertTrue(CourseAuthorization.instructor_email_enabled(self.course.id))
response = self.client.get(self.url)
self.assertTrue(self.email_link in response.content)
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': False})
def test_email_flag_false(self):
# Assert that the URL for the email view is not in the response
response = self.client.get(self.url)
self.assertFalse(self.email_link in response.content)
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True})
def test_email_flag_true_xml_store(self):
# If the enable email setting is enabled, but this is an XML backed course,
# the email view shouldn't be available on the instructor dashboard.
# The course factory uses a MongoModuleStore backing, so patch the
# `get_modulestore_type` method to pretend to be XML-backed.
# This is OK; we're simply testing that the `is_mongo_modulestore_type` flag
# in `instructor/views/legacy.py` is doing the correct thing.
with patch('xmodule.modulestore.mongo.base.MongoModuleStore.get_modulestore_type') as mock_modulestore:
mock_modulestore.return_value = ModuleStoreEnum.Type.xml
# Assert that the URL for the email view is not in the response
response = self.client.get(self.url)
self.assertFalse(self.email_link in response.content)
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True})
def test_send_mail_unauthorized(self):
""" Test 'Send email' action returns an error if course is not authorized to send email. """
response = self.client.post(
self.url, {
'action': 'Send email',
'to_option': 'all',
'subject': "Welcome to the course!",
'message': "Lets start with an introduction!"
}
)
self.assertContains(response, "Email is not enabled for this course.")
@patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True})
def test_send_mail_authorized(self):
""" Test 'Send email' action when course is authorized to send email. """
course_authorization = CourseAuthorization(course_id=self.course.id, email_enabled=True)
course_authorization.save()
session = self.client.session
session[u'idash_mode:{0}'.format(self.course.location.course_key.to_deprecated_string())] = 'Email'
session.save()
response = self.client.post(
self.url, {
'action': 'Send email',
'to_option': 'all',
'subject': 'Welcome to the course!',
'message': 'Lets start with an introduction!',
}
)
self.assertContains(response, "Your email was successfully queued for sending.")
......@@ -58,8 +58,7 @@ from instructor_task.api import (
get_instructor_task_history,
submit_rescore_problem_for_all_students,
submit_rescore_problem_for_student,
submit_reset_problem_attempts_for_all_students,
submit_bulk_course_email
submit_reset_problem_attempts_for_all_students
)
from instructor_task.views import get_task_completion_info
from edxmako.shortcuts import render_to_response, render_to_string
......@@ -446,55 +445,6 @@ def instructor_dashboard(request, course_id):
datatable = ret['datatable']
#----------------------------------------
# email
elif action == 'Send email':
email_to_option = request.POST.get("to_option")
email_subject = request.POST.get("subject")
html_message = request.POST.get("message")
if bulk_email_is_enabled_for_course(course_key):
try:
# Create the CourseEmail object. This is saved immediately, so that
# any transaction that has been pending up to this point will also be
# committed.
email = CourseEmail.create(
course_key.to_deprecated_string(), request.user, email_to_option, email_subject, html_message
)
# Submit the task, so that the correct InstructorTask object gets created (for monitoring purposes)
submit_bulk_course_email(request, course_key, email.id) # pylint: disable=no-member
except Exception as err: # pylint: disable=broad-except
# Catch any errors and deliver a message to the user
error_msg = "Failed to send email! ({0})".format(err)
msg += "<font color='red'>" + error_msg + "</font>"
log.exception(error_msg)
else:
# If sending the task succeeds, deliver a success message to the user.
if email_to_option == "all":
text = _(
"Your email was successfully queued for sending. "
"Please note that for large classes, it may take up to an hour "
"(or more, if other courses are simultaneously sending email) "
"to send all emails."
)
else:
text = _('Your email was successfully queued for sending.')
email_msg = '<div class="msg msg-confirm"><p class="copy">{text}</p></div>'.format(text=text)
else:
msg += "<font color='red'>Email is not enabled for this course.</font>"
elif "Show Background Email Task History" in action:
message, datatable = get_background_task_table(course_key, task_type='bulk_course_email')
msg += message
elif "Show Background Email Task History" in action:
message, datatable = get_background_task_table(course_key, task_type='bulk_course_email')
msg += message
#----------------------------------------
# psychometrics
elif action == 'Generate Histogram and IRT Plot':
......@@ -578,27 +528,6 @@ def instructor_dashboard(request, course_id):
if is_studio_course:
studio_url = get_cms_course_link(course)
email_editor = None
# HTML editor for email
if idash_mode == 'Email' and is_studio_course:
html_module = HtmlDescriptor(
course.system,
DictFieldData({'data': html_message}),
ScopeIds(None, None, None, course_key.make_usage_key('html', 'dummy'))
)
fragment = html_module.render('studio_view')
fragment = wrap_xblock(
'LmsRuntime', html_module, 'studio_view', fragment, None,
extra_data={"course-id": course_key.to_deprecated_string()},
usage_id_serializer=lambda usage_id: quote_slashes(usage_id.to_deprecated_string()),
request_token=request_token(request),
)
email_editor = fragment.content
# Enable instructor email only if the following conditions are met:
# 1. Feature flag is on
# 2. We have explicitly enabled email for the given course via django-admin
# 3. It is NOT an XML course
if bulk_email_is_enabled_for_course(course_key):
show_email_tab = True
......@@ -628,10 +557,6 @@ def instructor_dashboard(request, course_id):
'modeflag': {idash_mode: 'selectedmode'},
'studio_url': studio_url,
'to_option': email_to_option, # email
'subject': email_subject, # email
'editor': email_editor, # email
'email_msg': email_msg, # email
'show_email_tab': show_email_tab, # email
'problems': problems, # psychometrics
......
......@@ -407,78 +407,7 @@ function goto( mode)
##-----------------------------------------------------------------------------
%if modeflag.get('Email'):
%if email_msg:
<p></p><p>${email_msg}</p>
%endif
<ul class="list-fields">
<li class="field">
<label for="id_to">${_("Send to:")}</label>
<select id="id_to" name="to_option">
<option value="myself">${_("Myself")}</option>
%if to_option == "staff":
<option value="staff" selected="selected">${_("Staff and instructors")}</option>
%else:
<option value="staff">${_("Staff and instructors")}</option>
%endif
%if to_option == "all":
<option value="all" selected="selected">${_("All (students, staff and instructors)")}</option>
%else:
<option value="all">${_("All (students, staff and instructors)")}</option>
%endif
</select>
</li>
<li class="field">
<label for="id_subject">${_("Subject: ")}</label>
%if subject:
<input type="text" id="id_subject" name="subject" maxlength="128" size="75" value="${subject}">
%else:
<input type="text" id="id_subject" name="subject" maxlength="128" size="75">
%endif
<span class="tip">${_("(Max 128 characters)")}</span>
</li>
<li class="field">
<label>Message:</label>
<div class="email-editor">
${editor}
</div>
<input type="hidden" name="message" value="">
</li>
</ul>
<div class="submit-email-action">
<p class="copy">${_("Please try not to email students more than once per week. Important things to consider before sending:")}</p>
<ul class="list-advice">
<li class="item">${_("Have you read over the email to make sure it says everything you want to say?")}</li>
<li class="item">${_("Have you sent the email to yourself first to make sure you're happy with how it's displayed, and that embedded links and images work properly?")}</li>
</ul>
<div class="submit-email-warning">
<p class="copy"><span style="color: red;"><b>${_("CAUTION!")}</b></span>
${_("Once the 'Send Email' button is clicked, your email will be queued for sending.")}
<b>${_("A queued email CANNOT be cancelled.")}</b></p>
</div>
<br />
<input type="submit" name="action" value="Send email">
</div>
<script type="text/javascript">
$(document).ready(function(){
var emailEditor = XBlock.initializeBlock($('.xblock-studio_view'));
document.idashform.onsubmit = function() {
this.message.value = emailEditor.save()['data'];
return true;
}
});
</script>
<br />
<p>These email actions run in the background, and status for active email tasks will appear in a table below.
To see status for all bulk email tasks submitted for this course, click on this button:
</p>
<p>
<input type="submit" name="action" value="Show Background Email Task History">
</p>
<p class="deprecated">${_("To send email, please visit the 'Email' section of the instructor dashboard.")}</p>
%endif
</form>
......
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