Skip to content
Open
15 changes: 15 additions & 0 deletions openwisp_notifications/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ class AbstractNotification(UUIDModel, BaseNotification):
_action_object = BaseNotification.action_object
_target = BaseNotification.target

@property
def verb(self):
config = {}
try:
config = get_notification_configuration(self.type)
except (NotificationRenderException, TypeError) as e:
logger.error(
"Couldn't get notification config for type %s : %s", self.type, e
)
return config.get("verb") or self.__dict__.get("verb")

@verb.setter
def verb(self, value):
self.__dict__["verb"] = value

class Meta(BaseNotification.Meta):
abstract = True

Expand Down
2 changes: 1 addition & 1 deletion openwisp_notifications/base/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class AbstractNotification(models.Model):
actor = GenericForeignKey("actor_content_type", "actor_object_id")
actor.short_description = _("actor")

verb = models.CharField(_("verb"), max_length=255)
verb = models.CharField(_("verb"), max_length=255, null=True, blank=True)
description = models.TextField(_("description"), blank=True, null=True)

target_content_type = models.ForeignKey(
Expand Down
2 changes: 0 additions & 2 deletions openwisp_notifications/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def notify_handler(**kwargs):
level = kwargs.pop(
"level", notification_template.get("level", Notification.LEVELS.info)
)
verb = notification_template.get("verb", kwargs.pop("verb", None))
user_app_name = User._meta.app_label

where = Q(is_superuser=True)
Expand Down Expand Up @@ -146,7 +145,6 @@ def notify_handler(**kwargs):
notification = Notification(
recipient=recipient,
actor=actor,
verb=str(verb),
public=public,
description=description,
timestamp=timestamp,
Expand Down
17 changes: 17 additions & 0 deletions openwisp_notifications/migrations/0012_remove_notification_verb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.2.6 on 2025-10-01 09:39

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("openwisp_notifications", "0011_populate_organizationnotificationsettings"),
]

operations = [
migrations.RemoveField(
model_name="notification",
name="verb",
),
]
32 changes: 32 additions & 0 deletions openwisp_notifications/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,38 @@ def test_notification_preference_page(self):
response = self.client.get(reverse(preference_page, args=(uuid4(),)))
self.assertEqual(response.status_code, 404)

@mock_notification_types
def test_dynamic_verb_changed(self):
self.notification_options.update(
{"type": "default", "target": self._get_org_user()}
)
default_config = get_notification_configuration("default")
original_message = default_config["message"]
original_verb = default_config.get("verb", "default verb")
default_config["message"] = "Notification with {notification.verb}"
default_config["verb"] = "initial verb"

self._create_notification()
notification = notification_queryset.first()

with self.subTest("Test initial verb from config"):
self.assertEqual(notification.verb, "initial verb")
self.assertIn("initial verb", notification.message)

with self.subTest("Test verb changes dynamically from config"):
default_config["verb"] = "updated verb"
del notification.message
self.assertEqual(notification.verb, "updated verb")
self.assertIn("updated verb", notification.message)

with self.subTest("Test fallback to database verb"):
unregister_notification_type("default")
notification.__dict__["verb"] = "db verb"
self.assertEqual(notification.verb, "db verb")

default_config["message"] = original_message
default_config["verb"] = original_verb


class TestTransactionNotifications(TestOrganizationMixin, TransactionTestCase):
def setUp(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.2.6 on 2025-10-01 22:24

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("sample_notifications", "0003_default_groups_permissions"),
]

operations = [
migrations.RemoveField(
model_name="notification",
name="verb",
),
]