From e49711f449e733891bb7f36182a1d68e9a699ead Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Mon, 16 Dec 2024 14:46:39 +0100 Subject: [PATCH] Notification - `HTML Color` format notification colors should be same as UI, `{{diff_full}}` token should also get HTML colors ( #2842 #2554 ) --- changedetectionio/diff.py | 13 +++++++++---- changedetectionio/tests/test_notification.py | 14 ++++++++++---- changedetectionio/update_worker.py | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/changedetectionio/diff.py b/changedetectionio/diff.py index 0d5fb363..1fa9b60a 100644 --- a/changedetectionio/diff.py +++ b/changedetectionio/diff.py @@ -1,6 +1,9 @@ import difflib from typing import List, Iterator, Union +REMOVED_STYLE = "background-color: #fadad7; color: #b30000;" +ADDED_STYLE = "background-color: #eaf2c2; color: #406619;" + def same_slicer(lst: List[str], start: int, end: int) -> List[str]: """Return a slice of the list, or a single element if start == end.""" return lst[start:end] if start != end else [lst[start]] @@ -33,24 +36,26 @@ def customSequenceMatcher( """ cruncher = difflib.SequenceMatcher(isjunk=lambda x: x in " \t", a=before, b=after) + + for tag, alo, ahi, blo, bhi in cruncher.get_opcodes(): if include_equal and tag == 'equal': yield before[alo:ahi] elif include_removed and tag == 'delete': if html_colour: - yield [f'{line}' for line in same_slicer(before, alo, ahi)] + yield [f'{line}' for line in same_slicer(before, alo, ahi)] else: yield [f"(removed) {line}" for line in same_slicer(before, alo, ahi)] if include_change_type_prefix else same_slicer(before, alo, ahi) elif include_replaced and tag == 'replace': if html_colour: - yield [f'{line}' for line in same_slicer(before, alo, ahi)] + \ - [f'{line}' for line in same_slicer(after, blo, bhi)] + yield [f'{line}' for line in same_slicer(before, alo, ahi)] + \ + [f'{line}' for line in same_slicer(after, blo, bhi)] else: yield [f"(changed) {line}" for line in same_slicer(before, alo, ahi)] + \ [f"(into) {line}" for line in same_slicer(after, blo, bhi)] if include_change_type_prefix else same_slicer(before, alo, ahi) + same_slicer(after, blo, bhi) elif include_added and tag == 'insert': if html_colour: - yield [f'{line}' for line in same_slicer(after, blo, bhi)] + yield [f'{line}' for line in same_slicer(after, blo, bhi)] else: yield [f"(added) {line}" for line in same_slicer(after, blo, bhi)] if include_change_type_prefix else same_slicer(after, blo, bhi) diff --git a/changedetectionio/tests/test_notification.py b/changedetectionio/tests/test_notification.py index b4871951..b558546f 100644 --- a/changedetectionio/tests/test_notification.py +++ b/changedetectionio/tests/test_notification.py @@ -442,9 +442,9 @@ def test_global_send_test_notification(client, live_server, measure_memory_usage assert b"Error: You must have atleast one watch configured for 'test notification' to work" in res.data -def test_html_color_notifications(client, live_server, measure_memory_usage): +def _test_color_notifications(client, notification_body_token): - #live_server_setup(live_server) + from changedetectionio.diff import ADDED_STYLE, REMOVED_STYLE set_original_response() @@ -461,7 +461,7 @@ def test_html_color_notifications(client, live_server, measure_memory_usage): data={ "application-fetch_backend": "html_requests", "application-minutes_between_check": 180, - "application-notification_body": '{{diff}}', + "application-notification_body": notification_body_token, "application-notification_format": "HTML Color", "application-notification_urls": test_notification_url, "application-notification_title": "New ChangeDetection.io Notification - {{ watch_url }}", @@ -492,7 +492,7 @@ def test_html_color_notifications(client, live_server, measure_memory_usage): with open("test-datastore/notification.txt", 'r') as f: x = f.read() - assert 'Which is across multiple lines' in x + assert f'Which is across multiple lines' in x client.get( @@ -500,3 +500,9 @@ def test_html_color_notifications(client, live_server, measure_memory_usage): follow_redirects=True ) +def test_html_color_notifications(client, live_server, measure_memory_usage): + + #live_server_setup(live_server) + _test_color_notifications(client, '{{diff}}') + _test_color_notifications(client, '{{diff_full}}') + \ No newline at end of file diff --git a/changedetectionio/update_worker.py b/changedetectionio/update_worker.py index 942f87bf..e64c7c53 100644 --- a/changedetectionio/update_worker.py +++ b/changedetectionio/update_worker.py @@ -77,7 +77,7 @@ class update_worker(threading.Thread): 'current_snapshot': snapshot_contents, 'diff': diff.render_diff(prev_snapshot, current_snapshot, line_feed_sep=line_feed_sep, html_colour=html_colour_enable), 'diff_added': diff.render_diff(prev_snapshot, current_snapshot, include_removed=False, line_feed_sep=line_feed_sep), - 'diff_full': diff.render_diff(prev_snapshot, current_snapshot, include_equal=True, line_feed_sep=line_feed_sep), + 'diff_full': diff.render_diff(prev_snapshot, current_snapshot, include_equal=True, line_feed_sep=line_feed_sep, html_colour=html_colour_enable), 'diff_patch': diff.render_diff(prev_snapshot, current_snapshot, line_feed_sep=line_feed_sep, patch_format=True), 'diff_removed': diff.render_diff(prev_snapshot, current_snapshot, include_added=False, line_feed_sep=line_feed_sep), 'notification_timestamp': now,