Commit 985a492e by Christina Roberts

Merge pull request #4891 from aboudreault/discussion-digest-default

Add ENABLE_DISCUSSION_EMAIL_DIGEST feature
parents 18a916ba ac445d6e
...@@ -14,6 +14,7 @@ import mock ...@@ -14,6 +14,7 @@ import mock
from openedx.core.djangoapps.user_api.models import UserPreference from openedx.core.djangoapps.user_api.models import UserPreference
from lang_pref import LANGUAGE_KEY from lang_pref import LANGUAGE_KEY
from notification_prefs import NOTIFICATION_PREF_KEY
from edxmako.tests import mako_middleware_process_request from edxmako.tests import mako_middleware_process_request
from external_auth.models import ExternalAuthMap from external_auth.models import ExternalAuthMap
...@@ -50,8 +51,8 @@ class TestCreateAccount(TestCase): ...@@ -50,8 +51,8 @@ class TestCreateAccount(TestCase):
@ddt.data("en", "eo") @ddt.data("en", "eo")
def test_header_lang_pref_saved(self, lang): def test_header_lang_pref_saved(self, lang):
response = self.client.post(self.url, self.params, HTTP_ACCEPT_LANGUAGE=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) user = User.objects.get(username=self.username)
self.assertEqual(response.status_code, 200)
self.assertEqual(UserPreference.get_preference(user, LANGUAGE_KEY), lang) self.assertEqual(UserPreference.get_preference(user, LANGUAGE_KEY), lang)
def base_extauth_bypass_sending_activation_email(self, bypass_activation_email_for_extauth_setting): def base_extauth_bypass_sending_activation_email(self, bypass_activation_email_for_extauth_setting):
...@@ -98,6 +99,18 @@ class TestCreateAccount(TestCase): ...@@ -98,6 +99,18 @@ class TestCreateAccount(TestCase):
""" """
self.base_extauth_bypass_sending_activation_email(False) 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.dict("student.models.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True})
@mock.patch("lms.lib.comment_client.User.base_url", TEST_CS_URL) @mock.patch("lms.lib.comment_client.User.base_url", TEST_CS_URL)
......
...@@ -86,6 +86,7 @@ from bulk_email.models import Optout, CourseAuthorization ...@@ -86,6 +86,7 @@ from bulk_email.models import Optout, CourseAuthorization
import shoppingcart import shoppingcart
from openedx.core.djangoapps.user_api.models import UserPreference from openedx.core.djangoapps.user_api.models import UserPreference
from lang_pref import LANGUAGE_KEY from lang_pref import LANGUAGE_KEY
from notification_prefs.views import enable_notifications
import track.views import track.views
...@@ -1589,6 +1590,12 @@ def create_account(request, post_override=None): # pylint: disable-msg=too-many ...@@ -1589,6 +1590,12 @@ def create_account(request, post_override=None): # pylint: disable-msg=too-many
(user, profile, registration) = ret (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") dog_stats_api.increment("common.student.account_created")
email = post_vars['email'] email = post_vars['email']
......
...@@ -90,6 +90,31 @@ class UsernameCipher(object): ...@@ -90,6 +90,31 @@ class UsernameCipher(object):
return UsernameCipher._remove_padding(decrypted) 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 @require_POST
def ajax_enable(request): def ajax_enable(request):
""" """
...@@ -103,13 +128,7 @@ def ajax_enable(request): ...@@ -103,13 +128,7 @@ def ajax_enable(request):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
raise PermissionDenied raise PermissionDenied
UserPreference.objects.get_or_create( enable_notifications(request.user)
user=request.user,
key=NOTIFICATION_PREF_KEY,
defaults={
"value": UsernameCipher.encrypt(request.user.username)
}
)
return HttpResponse(status=204) return HttpResponse(status=204)
...@@ -125,10 +144,7 @@ def ajax_disable(request): ...@@ -125,10 +144,7 @@ def ajax_disable(request):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
raise PermissionDenied raise PermissionDenied
UserPreference.objects.filter( disable_notifications(request.user)
user=request.user,
key=NOTIFICATION_PREF_KEY
).delete()
return HttpResponse(status=204) return HttpResponse(status=204)
......
...@@ -103,6 +103,13 @@ FEATURES = { ...@@ -103,6 +103,13 @@ FEATURES = {
# this should remain off in production until digest notifications are online. # this should remain off in production until digest notifications are online.
'ENABLE_DISCUSSION_HOME_PANEL': False, '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_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) 'ENABLE_DJANGO_ADMIN_SITE': True, # set true to enable django's admin site, even on prod (e.g. for course ops)
......
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