Skip to content

Commit fa1fa99

Browse files
authored
[change] Changed zerotier background notifications to "Generic Message" type #1048
Closes #1048
1 parent 2295ce9 commit fa1fa99

File tree

4 files changed

+62
-46
lines changed

4 files changed

+62
-46
lines changed

openwisp_controller/config/apps.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -237,47 +237,6 @@ def register_notification_types(self):
237237
},
238238
models=[self.device_model],
239239
)
240-
241-
register_notification_type(
242-
"api_task_error",
243-
{
244-
"verbose_name": _("Background API Task ERROR"),
245-
"verb": _("encountered an unrecoverable error"),
246-
"level": "error",
247-
"email_subject": _(
248-
'[{site.name}] ERROR: "{notification.target}" - '
249-
"VPN Server {action} API Task {notification.verb}"
250-
),
251-
"message": _(
252-
"Unable to perform {action} operation on the "
253-
"[{notification.target}]({notification.target_link}) VPN server "
254-
"due to an unrecoverable error (status code: {status_code})"
255-
),
256-
# Disable email notifications by default
257-
"email_notification": False,
258-
},
259-
models=[self.vpn_model],
260-
)
261-
register_notification_type(
262-
"api_task_recovery",
263-
{
264-
"verbose_name": _("Background API Task RECOVERY"),
265-
"verb": _("has been completed successfully"),
266-
"level": "info",
267-
"email_subject": _(
268-
'[{site.name}] RECOVERY: "{notification.target}" - '
269-
"VPN Server {action} API Task {notification.verb}"
270-
),
271-
"message": _(
272-
"The {action} operation on [{notification.target}]"
273-
"({notification.target_link}) {notification.verb}"
274-
),
275-
# Disable email notifications by default
276-
"email_notification": False,
277-
},
278-
models=[self.vpn_model],
279-
)
280-
281240
# Unregister default notification type
282241
try:
283242
unregister_notification_type("default")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by Django 5.2.2 on 2025-06-30 11:34
2+
3+
from django.db import migrations
4+
5+
6+
def cleanup_api_task_notification_types(apps, schema_editor):
7+
if not apps.is_installed("openwisp_notifications"):
8+
return
9+
Notification = apps.get_model("openwisp_notifications", "Notification")
10+
NotificationSetting = apps.get_model(
11+
"openwisp_notifications", "NotificationSetting"
12+
)
13+
NotificationSetting.objects.filter(
14+
type__in=["api_task_error", "api_task_recovery"]
15+
).delete()
16+
Notification.objects.filter(
17+
type__in=["api_task_error", "api_task_recovery"]
18+
).delete()
19+
20+
21+
class Migration(migrations.Migration):
22+
23+
dependencies = [
24+
("config", "0059_zerotier_templates_ow_zt_to_global"),
25+
]
26+
27+
operations = [
28+
migrations.RunPython(
29+
cleanup_api_task_notification_types, reverse_code=migrations.RunPython.noop
30+
),
31+
]

openwisp_controller/config/tasks_zerotier.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from celery import shared_task
66
from django.core.cache import cache
77
from django.core.exceptions import ObjectDoesNotExist
8+
from django.utils.translation import gettext as _
89
from openwisp_notifications.signals import notify
910
from requests.exceptions import RequestException
1011
from swapper import load_model
@@ -35,12 +36,37 @@ def _send_api_task_notification(self, type, **kwargs):
3536
# with the ow-notification container
3637
# https://github.com/openwisp/openwisp-notifications/issues/264
3738
sleep(2)
39+
message_map = {
40+
"error": {
41+
"verb": _("encountered an unrecoverable error"),
42+
"message": _(
43+
"Unable to perform {action} operation on the "
44+
"{target} VPN server due to an "
45+
"unrecoverable error "
46+
"(status code: {status_code})"
47+
),
48+
"level": "error",
49+
},
50+
"recovery": {
51+
"verb": _("has been completed successfully"),
52+
"message": _("The {action} operation on {target} {verb}."),
53+
"level": "info",
54+
},
55+
}
56+
meta = message_map[type]
3857
notify.send(
39-
type=f"api_task_{type}",
58+
type="generic_message",
4059
sender=vpn,
4160
target=vpn,
4261
action=action,
43-
status_code=status_code,
62+
verb=meta["verb"],
63+
message=meta["message"].format(
64+
action=action,
65+
target=str(vpn),
66+
status_code=status_code,
67+
verb=meta["verb"],
68+
),
69+
level=meta["level"],
4470
)
4571

4672
def handle_api_call(self, fn, *args, send_notification=True, **kwargs):

openwisp_controller/config/tests/test_notifications.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def test_zerotier_api_tasks_notification(
273273
notification = notification_qs.first()
274274
self.assertEqual(notification.actor, vpn)
275275
self.assertEqual(notification.target, vpn)
276-
self.assertEqual(notification.type, "api_task_error")
276+
self.assertEqual(notification.type, "generic_message")
277277
self.assertIn(
278278
"Unable to perform update operation", notification.message
279279
)
@@ -315,14 +315,14 @@ def test_zerotier_api_tasks_notification(
315315
self.assertEqual(mock_info.call_count, 1)
316316
self.assertEqual(notification_recovery.actor, vpn)
317317
self.assertEqual(notification_recovery.target, vpn)
318-
self.assertEqual(notification_recovery.type, "api_task_recovery")
318+
self.assertEqual(notification_recovery.type, "generic_message")
319319
self.assertIn("The update operation on", notification_recovery.message)
320320
# For unrecoverable error
321321
self.assertEqual(mock_warn.call_count, 0)
322322
self.assertEqual(mock_error.call_count, 1)
323323
self.assertEqual(notification_error.actor, vpn)
324324
self.assertEqual(notification_error.target, vpn)
325-
self.assertEqual(notification_error.type, "api_task_error")
325+
self.assertEqual(notification_error.type, "generic_message")
326326
self.assertIn(
327327
"Unable to perform update member operation",
328328
notification_error.message,

0 commit comments

Comments
 (0)