diff --git a/openedx/core/djangoapps/notifications/exceptions.py b/openedx/core/djangoapps/notifications/exceptions.py new file mode 100644 index 0000000000..2eb8bb7942 --- /dev/null +++ b/openedx/core/djangoapps/notifications/exceptions.py @@ -0,0 +1,6 @@ +""" Notification-related exceptions. """ + + +class InvalidNotificationTypeError(Exception): + """ Exception raised when an invalid notification type is passed. """ + pass # lint-amnesty, pylint: disable=unnecessary-pass diff --git a/openedx/core/djangoapps/notifications/grouping_notifications.py b/openedx/core/djangoapps/notifications/grouping_notifications.py index a1da88683f..eeb4400462 100644 --- a/openedx/core/djangoapps/notifications/grouping_notifications.py +++ b/openedx/core/djangoapps/notifications/grouping_notifications.py @@ -2,14 +2,16 @@ Notification grouping utilities for notifications """ import datetime +from abc import ABC, abstractmethod from typing import Dict, Type, Union from pytz import utc -from abc import ABC, abstractmethod - +from openedx.core.djangoapps.notifications.base_notification import COURSE_NOTIFICATION_TYPES from openedx.core.djangoapps.notifications.models import Notification +from .exceptions import InvalidNotificationTypeError + class BaseNotificationGrouper(ABC): """ @@ -42,6 +44,10 @@ class NotificationRegistry: """ Registers the grouper class for the given notification type. """ + if notification_type not in COURSE_NOTIFICATION_TYPES: + raise InvalidNotificationTypeError( + f"'{notification_type}' is not a valid notification type." + ) cls._groupers[notification_type] = grouper_class return grouper_class @@ -106,7 +112,7 @@ class NewPostGrouper(BaseNotificationGrouper): } -@NotificationRegistry.register('ora_staff_notification') +@NotificationRegistry.register('ora_staff_notifications') class OraStaffGrouper(BaseNotificationGrouper): """ Grouper for new ora staff notifications. diff --git a/openedx/core/djangoapps/notifications/tests/test_notification_grouping.py b/openedx/core/djangoapps/notifications/tests/test_notification_grouping.py index a3aace632c..23419e46cb 100644 --- a/openedx/core/djangoapps/notifications/tests/test_notification_grouping.py +++ b/openedx/core/djangoapps/notifications/tests/test_notification_grouping.py @@ -26,6 +26,10 @@ class TestNotificationRegistry(unittest.TestCase): Tests for the NotificationRegistry class """ + @patch.dict( + 'openedx.core.djangoapps.notifications.base_notification.COURSE_NOTIFICATION_TYPES', + {'test_notification': 'Test Notification'} + ) def test_register_and_get_grouper(self): """ Test that the register and get_grouper methods work as expected