From 8e5bf9196598ddb99ea86f55271c341cc8075735 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Wed, 3 Apr 2024 15:18:21 +0200 Subject: [PATCH] "Send Test Notification" button from watch form edit should respect global settings and tag/group settings ( #2289, #2263 ) --- changedetectionio/flask_app.py | 28 +++++++++++++++----- changedetectionio/static/js/notifications.js | 6 +---- changedetectionio/store.py | 5 +++- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/changedetectionio/flask_app.py b/changedetectionio/flask_app.py index 550a57e7..3515743e 100644 --- a/changedetectionio/flask_app.py +++ b/changedetectionio/flask_app.py @@ -516,21 +516,35 @@ def changedetection_app(config=None, datastore_o=None): watch = datastore.data['watching'].get(watch_uuid) if watch_uuid else None - # validate URLS - if not len(request.form['notification_urls'].strip()): + notification_urls = request.form['notification_urls'].strip().splitlines() + + if not notification_urls: + logger.debug("Test notification - Trying by group/tag") + if request.form['tags'].strip(): + for k in request.form['tags'].split(','): + tag = datastore.tag_exists_by_name(k.strip()) + notification_urls = tag.get('notifications_urls') if tag and tag.get('notifications_urls') else None + + if not notification_urls: + logger.debug("Test notification - Trying by global system settings notifications") + if datastore.data['settings']['application'].get('notification_urls'): + notification_urls = datastore.data['settings']['application']['notification_urls'] + + + if not notification_urls: return make_response({'error': 'No Notification URLs set'}, 400) - for server_url in request.form['notification_urls'].splitlines(): - if len(server_url.strip()): - if not apobj.add(server_url): - message = '{} is not a valid AppRise URL.'.format(server_url) + for n_url in notification_urls: + if len(n_url.strip()): + if not apobj.add(n_url): + message = '{} is not a valid AppRise URL.'.format(n_url) return make_response({'error': message}, 400) try: # use the same as when it is triggered, but then override it with the form test values n_object = { 'watch_url': request.form['window_url'], - 'notification_urls': request.form['notification_urls'].splitlines() + 'notification_urls': notification_urls } # Only use if present, if not set in n_object it should use the default system value diff --git a/changedetectionio/static/js/notifications.js b/changedetectionio/static/js/notifications.js index 046b645c..2c2e9c61 100644 --- a/changedetectionio/static/js/notifications.js +++ b/changedetectionio/static/js/notifications.js @@ -28,15 +28,11 @@ $(document).ready(function() { notification_format: $('#notification_format').val(), notification_title: $('#notification_title').val(), notification_urls: $('.notification-urls').val(), + tags: $('#tags').val(), window_url: window.location.href, } - if (!data['notification_urls'].length) { - alert("Notification URL list is empty, cannot send test.") - return; - } - $.ajax({ type: "POST", url: notification_base_url, diff --git a/changedetectionio/store.py b/changedetectionio/store.py index 768b8af2..24a0c405 100644 --- a/changedetectionio/store.py +++ b/changedetectionio/store.py @@ -657,7 +657,10 @@ class ChangeDetectionStore: return res def tag_exists_by_name(self, tag_name): - return any(v.get('title', '').lower() == tag_name.lower() for k, v in self.__data['settings']['application']['tags'].items()) + # Check if any tag dictionary has a 'title' attribute matching the provided tag_name + tags = self.__data['settings']['application']['tags'].values() + return next((v for v in tags if v.get('title', '').lower() == tag_name.lower()), + None) def get_updates_available(self): import inspect