Skip to content

Commit

Permalink
[fix] Assigned a callable to model choices attribute to deal with the…
Browse files Browse the repository at this point in the history
… latest NOTIFICATION_CHOICES

Created get_notification_choices() to dynamically update the choices
attribute of models with the latest NOTIFICATION_CHOICES. this fixes
issues in tests where the updates that applied on NOTIFICATION_CHOICES don't
reflect on the choices attributes in the models.
  • Loading branch information
mohamedAbdelaleem committed Mar 27, 2024
1 parent 22e6b1e commit 59656ff
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 3 additions & 3 deletions openwisp_notifications/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from openwisp_notifications import settings as app_settings
from openwisp_notifications.exceptions import NotificationRenderException
from openwisp_notifications.types import (
NOTIFICATION_CHOICES,
get_notification_choices,
get_notification_configuration,
)
from openwisp_notifications.utils import _get_absolute_url, _get_object_link
Expand All @@ -31,7 +31,7 @@

class AbstractNotification(UUIDModel, BaseNotification):
CACHE_KEY_PREFIX = 'ow-notifications-'
type = models.CharField(max_length=30, null=True, choices=NOTIFICATION_CHOICES)
type = models.CharField(max_length=30, null=True, choices=get_notification_choices)
_actor = BaseNotification.actor
_action_object = BaseNotification.action_object
_target = BaseNotification.target
Expand Down Expand Up @@ -212,7 +212,7 @@ class AbstractNotificationSetting(UUIDModel):
type = models.CharField(
max_length=30,
null=True,
choices=NOTIFICATION_CHOICES,
choices=get_notification_choices,
verbose_name='Notification Type',
)
organization = models.ForeignKey(
Expand Down
2 changes: 1 addition & 1 deletion openwisp_notifications/tests/test_notification_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_notification_type_registered(self):

self._get_admin()
self.assertEqual(queryset.count(), 1)
self.assertEquals(
self.assertEqual(
queryset.first().__str__(), 'Test Notification Type - default'
)

Expand Down
9 changes: 9 additions & 0 deletions openwisp_notifications/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
NOTIFICATION_ASSOCIATED_MODELS = set()


def get_notification_choices():
"""
Return a list of notification choices.
This method used as a callable for 'choices' attribute in models
allowing the models to deal with the updated list.
"""
return NOTIFICATION_CHOICES


def get_notification_configuration(notification_type):
if not notification_type:
return {}
Expand Down

0 comments on commit 59656ff

Please sign in to comment.