diff --git a/common/djangoapps/student/tests/test_create_account.py b/common/djangoapps/student/tests/test_create_account.py index e72dd971eb3da3f2cac0ddb2925ef3a81f3cb0e9..68f97357d112a8be6ea8bf5b608a66d0f4118814 100644 --- a/common/djangoapps/student/tests/test_create_account.py +++ b/common/djangoapps/student/tests/test_create_account.py @@ -14,6 +14,7 @@ import mock from openedx.core.djangoapps.user_api.models import UserPreference from lang_pref import LANGUAGE_KEY +from notification_prefs import NOTIFICATION_PREF_KEY from edxmako.tests import mako_middleware_process_request from external_auth.models import ExternalAuthMap @@ -50,8 +51,8 @@ class TestCreateAccount(TestCase): @ddt.data("en", "eo") def test_header_lang_pref_saved(self, lang): response = self.client.post(self.url, self.params, HTTP_ACCEPT_LANGUAGE=lang) - self.assertEqual(response.status_code, 200) user = User.objects.get(username=self.username) + self.assertEqual(response.status_code, 200) self.assertEqual(UserPreference.get_preference(user, LANGUAGE_KEY), lang) def base_extauth_bypass_sending_activation_email(self, bypass_activation_email_for_extauth_setting): @@ -98,6 +99,18 @@ class TestCreateAccount(TestCase): """ self.base_extauth_bypass_sending_activation_email(False) + @ddt.data(True, False) + def test_discussions_email_digest_pref(self, digest_enabled): + with mock.patch.dict("student.models.settings.FEATURES", {"ENABLE_DISCUSSION_EMAIL_DIGEST": digest_enabled}): + response = self.client.post(self.url, self.params) + self.assertEqual(response.status_code, 200) + user = User.objects.get(username=self.username) + preference = UserPreference.get_preference(user, NOTIFICATION_PREF_KEY) + if digest_enabled: + self.assertIsNotNone(preference) + else: + self.assertIsNone(preference) + @mock.patch.dict("student.models.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True}) @mock.patch("lms.lib.comment_client.User.base_url", TEST_CS_URL) diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index 02a1abf86c47e62e95e1e820944bb4c45b094450..25f32adffab0833d76b90b6149c39404a993530d 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -86,6 +86,7 @@ from bulk_email.models import Optout, CourseAuthorization import shoppingcart from openedx.core.djangoapps.user_api.models import UserPreference from lang_pref import LANGUAGE_KEY +from notification_prefs.views import enable_notifications import track.views @@ -1589,6 +1590,12 @@ def create_account(request, post_override=None): # pylint: disable-msg=too-many (user, profile, registration) = ret + if settings.FEATURES.get('ENABLE_DISCUSSION_EMAIL_DIGEST'): + try: + enable_notifications(user) + except Exception: + log.exception("Enable discussion notifications failed for user {id}.".format(id=user.id)) + dog_stats_api.increment("common.student.account_created") email = post_vars['email'] diff --git a/lms/djangoapps/notification_prefs/views.py b/lms/djangoapps/notification_prefs/views.py index 3cada57beb8033e54d6aa57b4aa52907edfdbb4f..af9e62c5d5f52bf9e6a63ab2821c01fc74e2340d 100644 --- a/lms/djangoapps/notification_prefs/views.py +++ b/lms/djangoapps/notification_prefs/views.py @@ -90,6 +90,31 @@ class UsernameCipher(object): return UsernameCipher._remove_padding(decrypted) +def enable_notifications(user): + """ + Enable notifications for a user. + Currently only used for daily forum digests. + """ + UserPreference.objects.get_or_create( + user=user, + key=NOTIFICATION_PREF_KEY, + defaults={ + "value": UsernameCipher.encrypt(user.username) + } + ) + + +def disable_notifications(user): + """ + Disable notifications for a user. + Currently only used for daily forum digests. + """ + UserPreference.objects.filter( + user=user, + key=NOTIFICATION_PREF_KEY + ).delete() + + @require_POST def ajax_enable(request): """ @@ -103,13 +128,7 @@ def ajax_enable(request): if not request.user.is_authenticated(): raise PermissionDenied - UserPreference.objects.get_or_create( - user=request.user, - key=NOTIFICATION_PREF_KEY, - defaults={ - "value": UsernameCipher.encrypt(request.user.username) - } - ) + enable_notifications(request.user) return HttpResponse(status=204) @@ -125,10 +144,7 @@ def ajax_disable(request): if not request.user.is_authenticated(): raise PermissionDenied - UserPreference.objects.filter( - user=request.user, - key=NOTIFICATION_PREF_KEY - ).delete() + disable_notifications(request.user) return HttpResponse(status=204) diff --git a/lms/envs/common.py b/lms/envs/common.py index df132589b2dc47bd1db949c82a38ef2750ecb8b1..58683767469e05c2fa874c2719cef98dc07b1242 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -103,6 +103,13 @@ FEATURES = { # this should remain off in production until digest notifications are online. 'ENABLE_DISCUSSION_HOME_PANEL': False, + # Set this to True if you want the discussion digest emails enabled automatically for new users. + # This will be set on all new account registrations. + # It is not recommended to enable this feature if ENABLE_DISCUSSION_HOME_PANEL is not enabled, since + # subscribers who receive digests in that case will only be able to unsubscribe via links embedded + # in their emails, and they will have no way to resubscribe. + 'ENABLE_DISCUSSION_EMAIL_DIGEST': False, + 'ENABLE_PSYCHOMETRICS': False, # real-time psychometrics (eg item response theory analysis in instructor dashboard) 'ENABLE_DJANGO_ADMIN_SITE': True, # set true to enable django's admin site, even on prod (e.g. for course ops)