From b9e0ad052f3387c94be0d52da1fc5719f4daad1a Mon Sep 17 00:00:00 2001 From: Minty Date: Sun, 22 Aug 2021 22:36:10 +0200 Subject: [PATCH] New notification tokens - watch_uuid, watch_title, watch_tag, (#201) * New notification tokens ; Tokens added: watch_uuid, watch_title, watch_tag, updated settings description --- .gitignore | 1 + changedetectionio/notification.py | 22 +++++++++++++++----- changedetectionio/templates/settings.html | 12 +++++++++++ changedetectionio/tests/test_notification.py | 12 ++++++++--- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index cdaa9072..07a2a887 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ __pycache__ .pytest_cache build dist +.vscode/settings.json diff --git a/changedetectionio/notification.py b/changedetectionio/notification.py index 0772ce55..3106f80d 100644 --- a/changedetectionio/notification.py +++ b/changedetectionio/notification.py @@ -4,6 +4,9 @@ import apprise valid_tokens = { 'base_url': '', 'watch_url': '', + 'watch_uuid': '', + 'watch_title': '', + 'watch_tag': '', 'diff_url': '', 'preview_url': '', 'current_snapshot': '' @@ -22,7 +25,7 @@ def process_notification(n_object, datastore): n_title = datastore.data['settings']['application']['notification_title'] # Insert variables into the notification content - notification_parameters = create_notification_parameters(n_object) + notification_parameters = create_notification_parameters(n_object, datastore) raw_notification_text = [n_body, n_title] parameterised_notification_text = dict( @@ -39,11 +42,19 @@ def process_notification(n_object, datastore): # Notification title + body content parameters get created here. -def create_notification_parameters(n_object): +def create_notification_parameters(n_object, datastore): from copy import deepcopy + # in the case we send a test notification from the main settings, there is no UUID. uuid = n_object['uuid'] if 'uuid' in n_object else '' + if uuid != '': + watch_title = datastore.data['watching'][uuid]['title'] + watch_tag = datastore.data['watching'][uuid]['tag'] + else: + watch_title = 'Change Detection' + watch_tag = '' + # Create URLs to customise the notification with base_url = os.getenv('BASE_URL', '').strip('"') watch_url = n_object['watch_url'] @@ -64,11 +75,12 @@ def create_notification_parameters(n_object): { 'base_url': base_url, 'watch_url': watch_url, + 'watch_uuid': uuid, + 'watch_title': watch_title, + 'watch_tag': watch_tag, 'diff_url': diff_url, 'preview_url': preview_url, 'current_snapshot': n_object['current_snapshot'] if 'current_snapshot' in n_object else '' }) - return tokens - - + return tokens \ No newline at end of file diff --git a/changedetectionio/templates/settings.html b/changedetectionio/templates/settings.html index 5f6c9107..94884435 100644 --- a/changedetectionio/templates/settings.html +++ b/changedetectionio/templates/settings.html @@ -82,6 +82,18 @@ {watch_url} The URL being watched. + + {watch_uuid} + The UUID of the watch. + + + {watch_title} + The title of the watch. + + + {watch_tag} + The tag of the watch. + {preview_url} The URL of the preview page generated by changedetection.io. diff --git a/changedetectionio/tests/test_notification.py b/changedetectionio/tests/test_notification.py index 0ad8694a..fcebb9f7 100644 --- a/changedetectionio/tests/test_notification.py +++ b/changedetectionio/tests/test_notification.py @@ -1,5 +1,6 @@ import os import time +import re from flask import url_for from . util import set_original_response, set_modified_response, live_server_setup @@ -35,7 +36,8 @@ def test_check_notification(client, live_server): url_for("edit_page", uuid="first"), data={"notification_urls": notification_url, "url": test_url, - "tag": "", + "tag": "my tag", + "title": "my title", "headers": "", "fetch_backend": "html_requests", "trigger_check": "y"}, @@ -100,6 +102,9 @@ def test_check_notification(client, live_server): data={"notification_title": "New ChangeDetection.io Notification - {watch_url}", "notification_body": "BASE URL: {base_url}\n" "Watch URL: {watch_url}\n" + "Watch UUID: {watch_uuid}\n" + "Watch title: {watch_title}\n" + "Watch tag: {watch_tag}\n" "Preview: {preview_url}\n" "Diff URL: {diff_url}\n" "Snapshot: {current_snapshot}\n" @@ -129,8 +134,9 @@ def test_check_notification(client, live_server): with open("test-datastore/notification.txt", "r") as f: notification_submission = f.read() - # @todo regex that diff/uuid-31123-123-etc - + assert re.search('Watch UUID: [0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}', notification_submission, re.IGNORECASE) + assert "Watch title: my title" in notification_submission + assert "Watch tag: my tag" in notification_submission assert "diff/" in notification_submission assert "preview/" in notification_submission assert ":-)" in notification_submission