Skip to content

Commit

Permalink
re #2554 - Colors should be the same as UI preview and {{diff_full}} …
Browse files Browse the repository at this point in the history
…should get the colour output too
  • Loading branch information
dgtlmoon committed Dec 16, 2024
1 parent 2fb072c commit cd7f83e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
13 changes: 9 additions & 4 deletions changedetectionio/diff.py
Original file line number Diff line number Diff line change
@@ -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]]
Expand Down Expand Up @@ -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'<span style="background-color: #ffcecb;">{line}</span>' for line in same_slicer(before, alo, ahi)]
yield [f'<span style="{REMOVED_STYLE}">{line}</span>' 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'<span style="background-color: #ffcecb;">{line}</span>' for line in same_slicer(before, alo, ahi)] + \
[f'<span style="background-color: #dafbe1;">{line}</span>' for line in same_slicer(after, blo, bhi)]
yield [f'<span style="{REMOVED_STYLE}">{line}</span>' for line in same_slicer(before, alo, ahi)] + \
[f'<span style="{ADDED_STYLE}">{line}</span>' 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'<span style="background-color: #dafbe1;">{line}</span>' for line in same_slicer(after, blo, bhi)]
yield [f'<span style="{ADDED_STYLE}">{line}</span>' 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)

Expand Down
14 changes: 10 additions & 4 deletions changedetectionio/tests/test_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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 }}",
Expand Down Expand Up @@ -492,11 +492,17 @@ def test_html_color_notifications(client, live_server, measure_memory_usage):

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


client.get(
url_for("form_delete", uuid="all"),
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}}')

2 changes: 1 addition & 1 deletion changedetectionio/update_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def queue_notification_for_watch(self, notification_q, n_object, watch):
'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,
Expand Down

0 comments on commit cd7f83e

Please sign in to comment.