|
1 | 1 | import logging |
| 2 | +import time |
2 | 3 |
|
3 | 4 | from django.core.cache import cache |
4 | 5 | from django.core.exceptions import ValidationError |
@@ -211,15 +212,16 @@ def get_config_error_notification_target_url(obj, field, absolute_url=True): |
211 | 212 | return f"{url}#config-group" |
212 | 213 |
|
213 | 214 |
|
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) |
215 | 222 | vpn = kwargs.get("instance") |
216 | 223 | action = kwargs.get("action", "").replace("_", " ") |
217 | 224 | 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) |
223 | 225 | message_map = { |
224 | 226 | "error": { |
225 | 227 | "verb": _("encountered an unrecoverable error"), |
@@ -255,15 +257,15 @@ def send_api_task_notification(type, **kwargs): |
255 | 257 | ) |
256 | 258 |
|
257 | 259 |
|
258 | | -def handle_recovery_notification(task_key, **kwargs): |
| 260 | +def handle_recovery_notification(task_key, sleep_time=False, **kwargs): |
259 | 261 | task_result = cache.get(task_key) |
260 | 262 | if task_result == "error": |
261 | | - send_api_task_notification("success", **kwargs) |
| 263 | + send_api_task_notification("success", sleep_time=sleep_time, **kwargs) |
262 | 264 | cache.set(task_key, "success", timeout=None) |
263 | 265 |
|
264 | 266 |
|
265 | | -def handle_error_notification(task_key, **kwargs): |
| 267 | +def handle_error_notification(task_key, sleep_time=False, **kwargs): |
266 | 268 | cached_value = cache.get(task_key) |
267 | 269 | if cached_value != "error": |
268 | 270 | 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