-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add feature to send an e-mail to staff when a student enrolls #16669
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
Merged
iloveagent57
merged 1 commit into
openedx:master
from
open-craft:clemente/enrollment-email
Aug 2, 2018
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 hidden or 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,20 @@ | ||
| """ | ||
| Enrollment Application Configuration | ||
|
|
||
| Signal handlers are connected here. | ||
| """ | ||
|
|
||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class EnrollmentConfig(AppConfig): | ||
| """ | ||
| Application configuration for enrollments. | ||
| """ | ||
| name = u'enrollment' | ||
|
|
||
| def ready(self): | ||
| """ | ||
| Connect signal handlers. | ||
| """ | ||
| from . import handlers # pylint: disable=unused-variable |
This file contains hidden or 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,72 @@ | ||
| """ | ||
| Handlers and actions related to enrollment. | ||
| """ | ||
| import logging | ||
| from smtplib import SMTPException | ||
| from urlparse import urlunsplit | ||
|
|
||
| from django.conf import settings | ||
| from django.core.urlresolvers import reverse | ||
| from django.core.mail.message import EmailMessage | ||
| from django.dispatch import receiver | ||
| from edxmako.shortcuts import render_to_string | ||
| from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers | ||
|
|
||
| from student.models import ENROLL_STATUS_CHANGE, EnrollStatusChange | ||
|
|
||
| LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @receiver(ENROLL_STATUS_CHANGE) | ||
| def send_email_to_staff_on_student_enrollment(sender, event=None, user=None, **kwargs): # pylint: disable=unused-argument | ||
| """ | ||
| Sends an e-mail to staff after a new enrollment. | ||
| This feature can be enabled by setting the e-mail of the staff in ENROLLMENT_NOTIFICATION_EMAIL in lms.env.json, | ||
| or by using a SiteConfiguration variable of the same name (which will override the env one). | ||
| Disabled by default. | ||
| """ | ||
|
|
||
| if event == EnrollStatusChange.enroll: | ||
| to_email = configuration_helpers.get_value('ENROLLMENT_NOTIFICATION_EMAIL', | ||
| settings.ENROLLMENT_NOTIFICATION_EMAIL) | ||
|
|
||
| if not to_email: | ||
| # feature disabled | ||
| return | ||
|
|
||
| course_id = kwargs['course_id'] | ||
|
|
||
| site_protocol = 'https' if settings.HTTPS == 'on' else 'http' | ||
| site_domain = configuration_helpers.get_value('site_domain', settings.SITE_NAME) | ||
| context = { | ||
| 'user': user, | ||
| # This full_name is dependent on edx-platform's profile implementation | ||
| 'user_full_name': user.profile.name if hasattr(user, 'profile') else None, | ||
|
|
||
| 'course_url': urlunsplit(( | ||
| site_protocol, | ||
| site_domain, | ||
| reverse('about_course', args=[course_id.to_deprecated_string()]), | ||
| None, | ||
| None | ||
| )), | ||
| 'user_admin_url': urlunsplit(( | ||
| site_protocol, | ||
| site_domain, | ||
| reverse('admin:auth_user_change', args=[user.id]), | ||
| None, | ||
| None, | ||
| )), | ||
| } | ||
| subject = ''.join( | ||
| render_to_string('emails/new_enrollment_email_subject.txt', context).splitlines() | ||
| ) | ||
| message = render_to_string('emails/new_enrollment_email_body.txt', context) | ||
|
|
||
| email = EmailMessage(subject=subject, body=message, from_email=settings.DEFAULT_FROM_EMAIL, to=[to_email]) | ||
|
|
||
| try: | ||
| email.send() | ||
| except SMTPException as exception: | ||
| LOGGER.error("Failed sending e-mail about new enrollment to %s", to_email) | ||
| LOGGER.exception(exception) |
This file contains hidden or 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,82 @@ | ||
| """ | ||
| Tests for e-mail notifications related to user enrollment. | ||
| """ | ||
| import unittest | ||
|
|
||
| from django.conf import settings | ||
| from django.core import mail | ||
| from django.test.utils import override_settings | ||
| from nose.plugins.attrib import attr | ||
| from rest_framework.test import APITestCase | ||
| from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase | ||
| from xmodule.modulestore.tests.factories import CourseFactory | ||
|
|
||
| from course_modes.models import CourseMode | ||
| from course_modes.tests.factories import CourseModeFactory | ||
| from openedx.features.enterprise_support.tests.mixins.enterprise import EnterpriseServiceMockMixin | ||
| from student.tests.factories import UserFactory | ||
| from .test_views import EnrollmentTestMixin | ||
|
|
||
|
|
||
| @attr(shard=3) | ||
| @override_settings(EDX_API_KEY="i am a key") | ||
| @unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') | ||
| @override_settings(ENROLLMENT_NOTIFICATION_EMAIL="some_admins@example.com") | ||
| class EnrollmentEmailNotificationTest(EnrollmentTestMixin, | ||
| ModuleStoreTestCase, | ||
| APITestCase, | ||
| EnterpriseServiceMockMixin): | ||
| """ | ||
| Test e-mails sent to staff when a students enrolls to a course. | ||
| """ | ||
| USERNAME = "Bob" | ||
| EMAIL = "bob@example.com" | ||
| PASSWORD = "edx" | ||
|
|
||
| ENABLED_CACHES = ['default', 'mongo_metadata_inheritance', 'loc_cache'] | ||
| ENABLED_SIGNALS = ['course_published'] | ||
|
|
||
| def setUp(self): | ||
| """ Create a course and user, then log in. Also creates a course mode.""" | ||
| # This function is a simplified version of test_views.EnrollmentTest.setUp | ||
|
|
||
| super(EnrollmentEmailNotificationTest, self).setUp() | ||
|
|
||
| # Pass emit_signals when creating the course so it would be cached | ||
| # as a CourseOverview. | ||
| self.course = CourseFactory.create(emit_signals=True) | ||
|
|
||
| self.user = UserFactory.create( | ||
| username=self.USERNAME, | ||
| email=self.EMAIL, | ||
| password=self.PASSWORD, | ||
| ) | ||
| self.client.login(username=self.USERNAME, password=self.PASSWORD) | ||
|
|
||
| CourseModeFactory.create( | ||
| course_id=self.course.id, | ||
| mode_slug=CourseMode.DEFAULT_MODE_SLUG, | ||
| mode_display_name=CourseMode.DEFAULT_MODE_SLUG, | ||
| ) | ||
|
|
||
| def test_email_sent_to_staff_after_enrollment(self): | ||
| """ | ||
| Tests that an e-mail is sent on enrollment (but not on other events like unenrollment). | ||
| """ | ||
| assert len(mail.outbox) == 0 | ||
|
|
||
| # Create an enrollment and verify some data | ||
| self.assert_enrollment_status() | ||
|
|
||
| assert len(mail.outbox) == 1 | ||
|
|
||
| msg = mail.outbox[0] | ||
| assert msg.subject == "New student enrollment" | ||
| assert msg.to == ["some_admins@example.com"] | ||
|
|
||
| # unenroll and check that unenrollment doesn't send additional e-mails | ||
| self.assert_enrollment_status( | ||
| as_server=True, | ||
| is_active=False, | ||
| ) | ||
| assert len(mail.outbox) == 1 | ||
This file contains hidden or 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 @@ | ||
| <%! from django.utils.translation import ugettext as _ %>${_("Enrollment received for:")} | ||
|
|
||
| % if user_full_name: | ||
| ${_("Learner: {username} ({full_name})").format(username=user.username, full_name=user_full_name)} | ||
| % else: | ||
| ${_("Learner: {username}").format(username=user.username)} | ||
| % endif | ||
| ${_("Course: {url}").format(url=course_url)} | ||
|
|
||
| ${_("More learner data in admin: {url}").format(url=user_admin_url)} |
This file contains hidden or 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,2 @@ | ||
| <%! from django.utils.translation import ugettext as _ %> | ||
| ${_("New student enrollment")} |
This file contains hidden or 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 hidden or 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
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.
Any particular reason this isn't in
setUp?