@@ -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 )
0 commit comments