Skip to content

Commit 1c5db2e

Browse files
committed
Re #2554 - Clours should be same as UI, {{diff_full}} token should also get HTML colours
1 parent a3a3ab0 commit 1c5db2e

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

changedetectionio/diff.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import difflib
22
from typing import List, Iterator, Union
33

4+
REMOVED_STYLE = "background-color: #fadad7; color: #b30000;"
5+
ADDED_STYLE = "background-color: #eaf2c2; color: #406619;"
6+
47
def same_slicer(lst: List[str], start: int, end: int) -> List[str]:
58
"""Return a slice of the list, or a single element if start == end."""
69
return lst[start:end] if start != end else [lst[start]]
@@ -33,24 +36,26 @@ def customSequenceMatcher(
3336
"""
3437
cruncher = difflib.SequenceMatcher(isjunk=lambda x: x in " \t", a=before, b=after)
3538

39+
40+
3641
for tag, alo, ahi, blo, bhi in cruncher.get_opcodes():
3742
if include_equal and tag == 'equal':
3843
yield before[alo:ahi]
3944
elif include_removed and tag == 'delete':
4045
if html_colour:
41-
yield [f'<span style="background-color: #ffcecb;">{line}</span>' for line in same_slicer(before, alo, ahi)]
46+
yield [f'<span style="{REMOVED_STYLE}">{line}</span>' for line in same_slicer(before, alo, ahi)]
4247
else:
4348
yield [f"(removed) {line}" for line in same_slicer(before, alo, ahi)] if include_change_type_prefix else same_slicer(before, alo, ahi)
4449
elif include_replaced and tag == 'replace':
4550
if html_colour:
46-
yield [f'<span style="background-color: #ffcecb;">{line}</span>' for line in same_slicer(before, alo, ahi)] + \
47-
[f'<span style="background-color: #dafbe1;">{line}</span>' for line in same_slicer(after, blo, bhi)]
51+
yield [f'<span style="{REMOVED_STYLE}">{line}</span>' for line in same_slicer(before, alo, ahi)] + \
52+
[f'<span style="{ADDED_STYLE}">{line}</span>' for line in same_slicer(after, blo, bhi)]
4853
else:
4954
yield [f"(changed) {line}" for line in same_slicer(before, alo, ahi)] + \
5055
[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)
5156
elif include_added and tag == 'insert':
5257
if html_colour:
53-
yield [f'<span style="background-color: #dafbe1;">{line}</span>' for line in same_slicer(after, blo, bhi)]
58+
yield [f'<span style="{ADDED_STYLE}">{line}</span>' for line in same_slicer(after, blo, bhi)]
5459
else:
5560
yield [f"(added) {line}" for line in same_slicer(after, blo, bhi)] if include_change_type_prefix else same_slicer(after, blo, bhi)
5661

changedetectionio/tests/test_notification.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ def test_global_send_test_notification(client, live_server, measure_memory_usage
442442
assert b"Error: You must have atleast one watch configured for 'test notification' to work" in res.data
443443

444444

445-
def test_html_color_notifications(client, live_server, measure_memory_usage):
445+
def _test_color_notifications(client, notification_body_token):
446446

447-
#live_server_setup(live_server)
447+
from changedetectionio.diff import ADDED_STYLE, REMOVED_STYLE
448448

449449
set_original_response()
450450

@@ -461,7 +461,7 @@ def test_html_color_notifications(client, live_server, measure_memory_usage):
461461
data={
462462
"application-fetch_backend": "html_requests",
463463
"application-minutes_between_check": 180,
464-
"application-notification_body": '{{diff}}',
464+
"application-notification_body": notification_body_token,
465465
"application-notification_format": "HTML Color",
466466
"application-notification_urls": test_notification_url,
467467
"application-notification_title": "New ChangeDetection.io Notification - {{ watch_url }}",
@@ -492,11 +492,17 @@ def test_html_color_notifications(client, live_server, measure_memory_usage):
492492

493493
with open("test-datastore/notification.txt", 'r') as f:
494494
x = f.read()
495-
assert '<span style="background-color: #ffcecb;">Which is across multiple lines' in x
495+
assert f'<span style="{REMOVED_STYLE}">Which is across multiple lines' in x
496496

497497

498498
client.get(
499499
url_for("form_delete", uuid="all"),
500500
follow_redirects=True
501501
)
502502

503+
def test_html_color_notifications(client, live_server, measure_memory_usage):
504+
505+
#live_server_setup(live_server)
506+
_test_color_notifications(client, '{{diff}}')
507+
_test_color_notifications(client, '{{diff_full}}')
508+

changedetectionio/update_worker.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def queue_notification_for_watch(self, notification_q, n_object, watch):
7777
'current_snapshot': snapshot_contents,
7878
'diff': diff.render_diff(prev_snapshot, current_snapshot, line_feed_sep=line_feed_sep, html_colour=html_colour_enable),
7979
'diff_added': diff.render_diff(prev_snapshot, current_snapshot, include_removed=False, line_feed_sep=line_feed_sep),
80-
'diff_full': diff.render_diff(prev_snapshot, current_snapshot, include_equal=True, line_feed_sep=line_feed_sep),
80+
'diff_full': diff.render_diff(prev_snapshot, current_snapshot, include_equal=True, line_feed_sep=line_feed_sep, html_colour=html_colour_enable),
8181
'diff_patch': diff.render_diff(prev_snapshot, current_snapshot, line_feed_sep=line_feed_sep, patch_format=True),
8282
'diff_removed': diff.render_diff(prev_snapshot, current_snapshot, include_added=False, line_feed_sep=line_feed_sep),
8383
'notification_timestamp': now,

0 commit comments

Comments
 (0)