From 47bbe7831d35fd905a1b525e79fa03b776bc5e14 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Wed, 8 Jan 2025 12:28:40 +0100 Subject: [PATCH] Push notification immediately on test --- changedetectionio/flask_app.py | 19 ++++- changedetectionio/notification.py | 6 +- changedetectionio/static/js/notifications.js | 73 ++++++++++--------- .../static/styles/scss/styles.scss | 10 ++- changedetectionio/static/styles/styles.css | 9 ++- .../templates/_common_fields.html | 4 +- changedetectionio/tests/test_notification.py | 12 +-- 7 files changed, 82 insertions(+), 51 deletions(-) diff --git a/changedetectionio/flask_app.py b/changedetectionio/flask_app.py index 7d0c9d5ccf9..122839b89f8 100644 --- a/changedetectionio/flask_app.py +++ b/changedetectionio/flask_app.py @@ -598,17 +598,28 @@ def ajax_callback_send_notification_test(watch_uuid=None): if 'notification_title' in request.form and request.form['notification_title'].strip(): n_object['notification_title'] = request.form.get('notification_title', '').strip() + elif datastore.data['settings']['application'].get('notification_title'): + n_object['notification_title'] = datastore.data['settings']['application'].get('notification_title') + else: + n_object['notification_title'] = "Test title" if 'notification_body' in request.form and request.form['notification_body'].strip(): n_object['notification_body'] = request.form.get('notification_body', '').strip() + elif datastore.data['settings']['application'].get('notification_body'): + n_object['notification_body'] = datastore.data['settings']['application'].get('notification_body') + else: + n_object['notification_body'] = "Test body" + n_object['as_async'] = False n_object.update(watch.extra_notification_token_values()) + from .notification import process_notification + sent_obj = process_notification(n_object, datastore) - from . import update_worker - new_worker = update_worker.update_worker(update_q, notification_q, app, datastore) - new_worker.queue_notification_for_watch(notification_q=notification_q, n_object=n_object, watch=watch) except Exception as e: - return make_response(f"Error: str(e)", 400) + e_str = str(e) + e_str=e_str.replace("DEBUG - .CustomNotifyPluginWrapper'>",'') + + return make_response(e_str, 400) return 'OK - Sent test notifications' diff --git a/changedetectionio/notification.py b/changedetectionio/notification.py index ddd859f576d..7eed328d129 100644 --- a/changedetectionio/notification.py +++ b/changedetectionio/notification.py @@ -67,6 +67,10 @@ def process_notification(n_object, datastore): sent_objs = [] from .apprise_asset import asset + + if 'as_async' in n_object: + asset.async_mode = n_object.get('as_async') + apobj = apprise.Apprise(debug=True, asset=asset) if not n_object.get('notification_urls'): @@ -157,8 +161,6 @@ def process_notification(n_object, datastore): attach=n_object.get('screenshot', None) ) - # Give apprise time to register an error - time.sleep(3) # Returns empty string if nothing found, multi-line string otherwise log_value = logs.getvalue() diff --git a/changedetectionio/static/js/notifications.js b/changedetectionio/static/js/notifications.js index 95a0763c4ec..1cccd8510b1 100644 --- a/changedetectionio/static/js/notifications.js +++ b/changedetectionio/static/js/notifications.js @@ -1,42 +1,43 @@ -$(document).ready(function() { +$(document).ready(function () { - $('#add-email-helper').click(function (e) { - e.preventDefault(); - email = prompt("Destination email"); - if(email) { - var n = $(".notification-urls"); - var p=email_notification_prefix; - $(n).val( $.trim( $(n).val() )+"\n"+email_notification_prefix+email ); - } - }); - - $('#send-test-notification').click(function (e) { - e.preventDefault(); - - data = { - notification_body: $('#notification_body').val(), - notification_format: $('#notification_format').val(), - notification_title: $('#notification_title').val(), - notification_urls: $('.notification-urls').val(), - tags: $('#tags').val(), - window_url: window.location.href, - } + $('#add-email-helper').click(function (e) { + e.preventDefault(); + email = prompt("Destination email"); + if (email) { + var n = $(".notification-urls"); + var p = email_notification_prefix; + $(n).val($.trim($(n).val()) + "\n" + email_notification_prefix + email); + } + }); + $('#send-test-notification').click(function (e) { + e.preventDefault(); - $.ajax({ - type: "POST", - url: notification_base_url, - data : data, - statusCode: { - 400: function(data) { - // More than likely the CSRF token was lost when the server restarted - alert(data.responseText); + data = { + notification_body: $('#notification_body').val(), + notification_format: $('#notification_format').val(), + notification_title: $('#notification_title').val(), + notification_urls: $('.notification-urls').val(), + tags: $('#tags').val(), + window_url: window.location.href, } - } - }).done(function(data){ - console.log(data); - alert(data); - }) - }); + + $('.notifications-wrapper .spinner').fadeIn(); + $('#notification-test-log').show(); + $.ajax({ + type: "POST", + url: notification_base_url, + data: data, + statusCode: { + 400: function (data) { + $("#notification-test-log>span").text(data.responseText); + }, + } + }).done(function (data) { + $("#notification-test-log>span").text(data); + }).always(function () { + $('.notifications-wrapper .spinner').hide(); + }) + }); }); diff --git a/changedetectionio/static/styles/scss/styles.scss b/changedetectionio/static/styles/scss/styles.scss index ecaf7ed966e..4c698088d00 100644 --- a/changedetectionio/static/styles/scss/styles.scss +++ b/changedetectionio/static/styles/scss/styles.scss @@ -380,7 +380,15 @@ a.pure-button-selected { } .notifications-wrapper { - padding: 0.5rem 0 1rem 0; + padding-top: 0.5rem; + #notification-test-log { + padding-top: 1rem; + white-space: pre-wrap; + word-break: break-word; + overflow-wrap: break-word; + max-width: 100%; + box-sizing: border-box; + } } label { diff --git a/changedetectionio/static/styles/styles.css b/changedetectionio/static/styles/styles.css index 1600fde3224..d49506dc874 100644 --- a/changedetectionio/static/styles/styles.css +++ b/changedetectionio/static/styles/styles.css @@ -780,7 +780,14 @@ a.pure-button-selected { cursor: pointer; } .notifications-wrapper { - padding: 0.5rem 0 1rem 0; } + padding-top: 0.5rem; } + .notifications-wrapper #notification-test-log { + padding-top: 1rem; + white-space: pre-wrap; + word-break: break-word; + overflow-wrap: break-word; + max-width: 100%; + box-sizing: border-box; } label:hover { cursor: pointer; } diff --git a/changedetectionio/templates/_common_fields.html b/changedetectionio/templates/_common_fields.html index 9447f9035bf..53d27a50bc6 100644 --- a/changedetectionio/templates/_common_fields.html +++ b/changedetectionio/templates/_common_fields.html @@ -24,11 +24,13 @@
- Send test notification + Send test notification {% if emailprefix %} Add email Add an email address {% endif %} Notification debug logs +
+
diff --git a/changedetectionio/tests/test_notification.py b/changedetectionio/tests/test_notification.py index b558546fcd6..0c20a9f10b0 100644 --- a/changedetectionio/tests/test_notification.py +++ b/changedetectionio/tests/test_notification.py @@ -360,7 +360,10 @@ def test_global_send_test_notification(client, live_server, measure_memory_usage #live_server_setup(live_server) set_original_response() if os.path.isfile("test-datastore/notification.txt"): - os.unlink("test-datastore/notification.txt") + os.unlink("test-datastore/notification.txt") \ + + # 1995 UTF-8 content should be encoded + test_body = 'change detection is cool 网站监测 内容更新了' # otherwise other settings would have already existed from previous tests in this file res = client.post( @@ -368,8 +371,7 @@ def test_global_send_test_notification(client, live_server, measure_memory_usage data={ "application-fetch_backend": "html_requests", "application-minutes_between_check": 180, - #1995 UTF-8 content should be encoded - "application-notification_body": 'change detection is cool 网站监测 内容更新了', + "application-notification_body": test_body, "application-notification_format": default_notification_format, "application-notification_urls": "", "application-notification_title": "New ChangeDetection.io Notification - {{ watch_url }}", @@ -399,12 +401,10 @@ def test_global_send_test_notification(client, live_server, measure_memory_usage assert res.status_code != 400 assert res.status_code != 500 - # Give apprise time to fire - time.sleep(4) with open("test-datastore/notification.txt", 'r') as f: x = f.read() - assert 'change detection is cool 网站监测 内容更新了' in x + assert test_body in x os.unlink("test-datastore/notification.txt")