Skip to content

Commit 4b41bef

Browse files
committed
[fix] Implement toast notification and avoid triggering it while the page is reloading #1049
Added sleep_time arguement to implement toast and implement passing it to sender appropriately. Fixes #1049
1 parent 102c0d0 commit 4b41bef

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

openwisp_controller/config/tasks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def trigger_vpn_server_endpoint(endpoint, auth_token, vpn_id):
149149
)
150150
handle_error_notification(
151151
task_key,
152+
sleep_time=5,
152153
exception=e,
153154
instance=vpn,
154155
action="update",
@@ -157,6 +158,7 @@ def trigger_vpn_server_endpoint(endpoint, auth_token, vpn_id):
157158
logger.info(f"Triggered update webhook of VPN Server UUID: {vpn_id}")
158159
handle_recovery_notification(
159160
task_key,
161+
sleep_time=5,
160162
instance=vpn,
161163
action="update",
162164
)

openwisp_controller/config/utils.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import time
23

34
from django.core.cache import cache
45
from django.core.exceptions import ValidationError
@@ -211,15 +212,16 @@ def get_config_error_notification_target_url(obj, field, absolute_url=True):
211212
return f"{url}#config-group"
212213

213214

214-
def send_api_task_notification(type, **kwargs):
215+
def send_api_task_notification(type, sleep_time=False, **kwargs):
216+
"""
217+
The sleep_time argument is needed to avoid triggering the toast
218+
notification in the admin while the page is reloading.
219+
"""
220+
if sleep_time:
221+
time.sleep(sleep_time)
215222
vpn = kwargs.get("instance")
216223
action = kwargs.get("action", "").replace("_", " ")
217224
exception = kwargs.get("exception")
218-
# Adding some delay here to prevent overlapping
219-
# of the django success message container
220-
# with the ow-notification container
221-
# https://github.com/openwisp/openwisp-notifications/issues/264
222-
# sleep(2)
223225
message_map = {
224226
"error": {
225227
"verb": _("encountered an unrecoverable error"),
@@ -255,15 +257,15 @@ def send_api_task_notification(type, **kwargs):
255257
)
256258

257259

258-
def handle_recovery_notification(task_key, **kwargs):
260+
def handle_recovery_notification(task_key, sleep_time=False, **kwargs):
259261
task_result = cache.get(task_key)
260262
if task_result == "error":
261-
send_api_task_notification("success", **kwargs)
263+
send_api_task_notification("success", sleep_time=sleep_time, **kwargs)
262264
cache.set(task_key, "success", timeout=None)
263265

264266

265-
def handle_error_notification(task_key, **kwargs):
267+
def handle_error_notification(task_key, sleep_time=False, **kwargs):
266268
cached_value = cache.get(task_key)
267269
if cached_value != "error":
268270
cache.set(task_key, "error", timeout=None)
269-
send_api_task_notification("error", **kwargs)
271+
send_api_task_notification("error", sleep_time=sleep_time, **kwargs)

0 commit comments

Comments
 (0)