Skip to content

Commit 1ffedf4

Browse files
refactor: deprecated v2 preferences api and its dependencies (#38072)
1 parent 0092458 commit 1ffedf4

6 files changed

Lines changed: 11 additions & 719 deletions

File tree

openedx/core/djangoapps/notifications/base_notification.py

Lines changed: 4 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class NotificationType(TypedDict):
2525
notification_app: str
2626
# Unique identifier for this notification type.
2727
name: str
28-
# Mark this as a core notification.
28+
# Whether this notification type uses the notification app's default settings.
2929
# When True, user preferences are taken from the notification app's configuration,
3030
# overriding the `web`, `email`, `push`, `email_cadence`, and `non_editable` attributes set here.
3131
use_app_defaults: bool
@@ -38,8 +38,7 @@ class NotificationType(TypedDict):
3838
content_context: dict[str, Any]
3939
filters: list[str]
4040

41-
# All fields below are required unless `is_core` is True.
42-
# Core notifications take this config from the associated notification app instead (and ignore anything set here).
41+
# All fields below are required unless `use_app_defaults` is True.
4342

4443
# Set to True to enable delivery on web.
4544
web: NotRequired[bool]
@@ -306,9 +305,7 @@ class NotificationApp(TypedDict):
306305
307306
Each notification type defined in COURSE_NOTIFICATION_TYPES also references an app.
308307
309-
Each notification type can also be optionally defined as a core notification.
310308
In this case, the delivery preferences for that notification are taken
311-
from the `core_*` fields of the associated notification app.
312309
"""
313310
# Set to True to enable this app and linked notification types.
314311
enabled: bool
@@ -366,106 +363,6 @@ class NotificationApp(TypedDict):
366363
COURSE_NOTIFICATION_APPS = get_notification_apps_config()
367364

368365

369-
class NotificationTypeManager:
370-
"""
371-
Manager for notification types
372-
"""
373-
374-
def __init__(self):
375-
self.notification_types = COURSE_NOTIFICATION_TYPES
376-
377-
def get_notification_types_by_app(self, notification_app: str):
378-
"""
379-
Returns notification types for the given notification app name.
380-
"""
381-
return [
382-
notification_type.copy() for _, notification_type in self.notification_types.items()
383-
if notification_type.get('notification_app', None) == notification_app
384-
]
385-
386-
def get_core_and_non_core_notification_types(
387-
self, notification_app: str
388-
) -> tuple[NotificationType, NotificationType]:
389-
"""
390-
Returns notification types for the given app name, split by core and non core.
391-
392-
Return type is a tuple of (core_notification_types, non_core_notification_types).
393-
"""
394-
notification_types = self.get_notification_types_by_app(notification_app)
395-
core_notification_types = []
396-
non_core_notification_types = []
397-
for notification_type in notification_types:
398-
if notification_type.get('use_app_defaults', None):
399-
core_notification_types.append(notification_type)
400-
else:
401-
non_core_notification_types.append(notification_type)
402-
return core_notification_types, non_core_notification_types
403-
404-
@staticmethod
405-
def get_non_core_notification_type_preferences(non_core_notification_types, email_opt_out=False):
406-
"""
407-
Returns non-core notification type preferences for the given notification types.
408-
"""
409-
non_core_notification_type_preferences = {}
410-
for notification_type in non_core_notification_types:
411-
non_core_notification_type_preferences[notification_type.get('name')] = {
412-
'web': notification_type.get('web', False),
413-
'email': False if email_opt_out else notification_type.get('email', False),
414-
'push': notification_type.get('push', False),
415-
'email_cadence': notification_type.get('email_cadence', 'Daily'),
416-
}
417-
return non_core_notification_type_preferences
418-
419-
def get_notification_app_preference(self, notification_app, email_opt_out=False):
420-
"""
421-
Returns notification app preferences for the given notification app.
422-
"""
423-
core_notification_types, non_core_notification_types = self.get_core_and_non_core_notification_types(
424-
notification_app,
425-
)
426-
non_core_notification_types_preferences = self.get_non_core_notification_type_preferences(
427-
non_core_notification_types, email_opt_out
428-
)
429-
core_notification_types_name = [notification_type.get('name') for notification_type in core_notification_types]
430-
return non_core_notification_types_preferences, core_notification_types_name
431-
432-
433-
class NotificationAppManager:
434-
"""
435-
Notification app manager
436-
"""
437-
438-
def add_core_notification_preference(self, notification_app_attrs, notification_types, email_opt_out=False):
439-
"""
440-
Adds core notification preference for the given notification app.
441-
"""
442-
notification_types['core'] = {
443-
'web': notification_app_attrs.get('web', False),
444-
'email': False if email_opt_out else notification_app_attrs.get('email', False),
445-
'push': notification_app_attrs.get('push', False),
446-
'email_cadence': notification_app_attrs.get('email_cadence', 'Daily'),
447-
}
448-
449-
def get_notification_app_preferences(self, email_opt_out=False):
450-
"""
451-
Returns notification app preferences for the given name.
452-
"""
453-
course_notification_preference_config = {}
454-
for notification_app_key, notification_app_attrs in COURSE_NOTIFICATION_APPS.items():
455-
notification_app_preferences = {}
456-
notification_types, core_notifications = NotificationTypeManager().get_notification_app_preference(
457-
notification_app_key,
458-
email_opt_out
459-
)
460-
self.add_core_notification_preference(notification_app_attrs, notification_types, email_opt_out)
461-
462-
notification_app_preferences['enabled'] = notification_app_attrs.get('enabled', False)
463-
notification_app_preferences['core_notification_types'] = core_notifications
464-
notification_app_preferences['notification_types'] = notification_types
465-
course_notification_preference_config[notification_app_key] = notification_app_preferences
466-
return course_notification_preference_config
467-
468-
469366
def get_notification_content(notification_type: str, context: dict[str, Any]):
470367
"""
471368
Returns notification content for the given notification type with provided context.
@@ -489,8 +386,8 @@ def get_notification_content(notification_type: str, context: dict[str, Any]):
489386
if notification_type == 'course_update':
490387
notification_type = 'course_updates'
491388

492-
# Retrieve the notification type object from NotificationTypeManager.
493-
notification_type = NotificationTypeManager().notification_types.get(notification_type, None)
389+
# Retrieve the notification type object from the default preferences (derived from COURSE_NOTIFICATION_TYPES).
390+
notification_type = get_default_values_of_preferences().get(notification_type, None)
494391

495392
if notification_type:
496393
# Check if the notification is grouped.
@@ -512,19 +409,6 @@ def get_notification_content(notification_type: str, context: dict[str, Any]):
512409
return ''
513410

514411

515-
def get_default_values_of_preference(notification_app, notification_type):
516-
"""
517-
Returns default preference for notification_type
518-
"""
519-
default_prefs = NotificationAppManager().get_notification_app_preferences()
520-
app_prefs = default_prefs.get(notification_app, {})
521-
core_notification_types = app_prefs.get('core_notification_types', [])
522-
notification_types = app_prefs.get('notification_types', {})
523-
if notification_type in core_notification_types:
524-
return notification_types.get('core', {})
525-
return notification_types.get(notification_type, {})
526-
527-
528412
def get_default_values_of_preferences() -> dict[str, dict[str, Any]]:
529413
"""
530414
Returns default preferences for all notification apps

openedx/core/djangoapps/notifications/serializers.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,6 @@ def validate_notification_channel(notification_channel: str) -> str:
138138
return notification_channel
139139

140140

141-
def get_non_editable_channels(app_name):
142-
"""
143-
Returns a dict of notification: [non-editable channels] for the given app name.
144-
"""
145-
non_editable = {"core": COURSE_NOTIFICATION_APPS[app_name].get("non_editable", [])}
146-
for type_name, type_dict in COURSE_NOTIFICATION_TYPES.items():
147-
if type_dict.get("non_editable") and not type_dict["is_core"]:
148-
non_editable[type_name] = type_dict["non_editable"]
149-
return non_editable
150-
151-
152141
def add_non_editable_in_preference(preference):
153142
"""
154143
Add non_editable preferences to the preference dict
@@ -211,7 +200,6 @@ def validate(self, attrs):
211200
# Validate notification type
212201
if all([
213202
not COURSE_NOTIFICATION_TYPES.get(notification_type),
214-
notification_type != "core",
215203
notification_type != "grouped_notification",
216204
]):
217205
raise ValidationError(f'{notification_type} is not a valid notification type.')

openedx/core/djangoapps/notifications/tasks.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
from openedx.core.djangoapps.notifications.audience_filters import NotificationFilter
1616
from openedx.core.djangoapps.notifications.base_notification import (
1717
COURSE_NOTIFICATION_TYPES,
18-
get_default_values_of_preference,
19-
get_notification_content
18+
get_notification_content, get_default_values_of_preferences
2019
)
2120

2221
from openedx.core.djangoapps.notifications.email.tasks import send_immediate_cadence_email
@@ -118,7 +117,7 @@ def send_notifications(user_ids, course_key: str, app_name, notification_type, c
118117
grouping_enabled = group_by_id and grouping_function is not None
119118
generated_notification = None
120119
sender_id = context.pop('sender_id', None)
121-
default_web_config = get_default_values_of_preference(app_name, notification_type).get('web', False)
120+
default_web_config = get_default_values_of_preferences().get(notification_type, {}).get('web', False)
122121
generated_notification_audience = []
123122
email_notification_mapping = {}
124123
push_notification_audience = []

0 commit comments

Comments
 (0)