@@ -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):
366363COURSE_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-
469366def 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-
528412def get_default_values_of_preferences () -> dict [str , dict [str , Any ]]:
529413 """
530414 Returns default preferences for all notification apps
0 commit comments