Skip to content

Commit af40ac0

Browse files
feat: update preference config when version is changed (#36518)
1 parent 336fb01 commit af40ac0

3 files changed

Lines changed: 74 additions & 2 deletions

File tree

openedx/core/djangoapps/notifications/models.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,35 @@ def get_user_course_preference(user_id, course_id):
172172
log.error(f'Unable to update notification preference to new config. {e}')
173173
return preferences
174174

175+
@staticmethod
176+
def get_user_notification_preferences(user):
177+
"""
178+
Checks if all user preferences have updated versions and returns the user preferences.
179+
Updates any preferences that need to be updated to the latest config version.
180+
"""
181+
preferences = CourseNotificationPreference.objects.filter(user=user, is_active=True)
182+
email_opt_out = UserPreference.objects.filter(user_id=user.id, key=ONE_CLICK_EMAIL_UNSUB_KEY).exists()
183+
current_config_version = get_course_notification_preference_config_version()
184+
preferences_to_update = []
185+
186+
try:
187+
for preference in preferences:
188+
if preference.config_version != current_config_version:
189+
current_prefs = preference.notification_preference_config
190+
new_prefs = NotificationPreferenceSyncManager.update_preferences(current_prefs, email_opt_out)
191+
preference.config_version = current_config_version
192+
preference.notification_preference_config = new_prefs
193+
preferences_to_update.append(preference)
194+
if preferences_to_update:
195+
CourseNotificationPreference.objects.bulk_update(
196+
preferences_to_update,
197+
['config_version', 'notification_preference_config']
198+
)
199+
except Exception as e: # pylint: disable=broad-exception-caught
200+
log.error(f'Unable to update notification preference to new config: {str(e)}')
201+
202+
return preferences
203+
175204
@staticmethod
176205
def get_updated_user_course_preferences(user, course_id):
177206
return CourseNotificationPreference.get_user_course_preference(user.id, course_id)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Test the notification app models
3+
"""
4+
import unittest
5+
from unittest import mock
6+
7+
import pytest
8+
9+
from common.djangoapps.student.tests.factories import UserFactory
10+
from openedx.core.djangoapps.notifications.base_notification import NotificationAppManager
11+
from openedx.core.djangoapps.notifications.models import CourseNotificationPreference, \
12+
COURSE_NOTIFICATION_CONFIG_VERSION
13+
14+
15+
@pytest.mark.django_db
16+
class TestPreferenceModel(unittest.TestCase):
17+
"""
18+
Test the CourseNotificationPreference model.
19+
"""
20+
21+
def test_get_user_notification_preferences_method(self):
22+
"""
23+
Test the get_user_notification_preferences method. and check if version is updated properly.
24+
"""
25+
# Create a mock user and notification preference
26+
user = UserFactory()
27+
CourseNotificationPreference.objects.create(
28+
user_id=user.id,
29+
course_id='course-v1:edX+DemoX+Demo_Course',
30+
is_active=True,
31+
notification_preference_config=NotificationAppManager().get_notification_app_preferences(True)
32+
)
33+
# Check if the notification preference is created
34+
preference = CourseNotificationPreference.objects.get(user_id=user.id)
35+
self.assertIsNotNone(preference)
36+
self.assertTrue(preference.is_active)
37+
38+
with mock.patch(
39+
'openedx.core.djangoapps.notifications.models.COURSE_NOTIFICATION_CONFIG_VERSION',
40+
COURSE_NOTIFICATION_CONFIG_VERSION + 1
41+
):
42+
updated_preferences = preference.get_user_notification_preferences(user)
43+
for updated_preference in updated_preferences:
44+
assert updated_preference.config_version == COURSE_NOTIFICATION_CONFIG_VERSION + 1

openedx/core/djangoapps/notifications/views.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,7 @@ def get(self, request):
591591
"""
592592
API view for getting the aggregate notification preferences for the current user.
593593
"""
594-
notification_preferences = CourseNotificationPreference.objects.filter(user=request.user, is_active=True)
595-
594+
notification_preferences = CourseNotificationPreference.get_user_notification_preferences(request.user)
596595
if not notification_preferences.exists():
597596
return Response({
598597
'status': 'error',

0 commit comments

Comments
 (0)