-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.py
87 lines (66 loc) · 2.68 KB
/
display.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from classes import ChangeSet, Page, PropertyChange
from enum import Enum
from typing import List, Dict, Tuple
from webhook import WebhookEmbed
class MessageColor(Enum):
ADD = 0x44CC00
REMOVE = 0xFF2A00
CHANGE = 0xFFD000
def generate_changeset_messages(changeset: ChangeSet) -> List[WebhookEmbed]:
# changed: List[Tuple[Page, List[PropertyChange]]]
res = []
res += generate_added_messages(changeset.added)
res += generate_removed_messages(changeset.removed)
res += generate_changed_messages(changeset.changed)
return res
def generate_properties_text(page: Page) -> str:
PROPERTY_ID_IGNORELIST = ["title"]
lines = []
for property_id, property in page["properties"].items():
if property_id in PROPERTY_ID_IGNORELIST:
continue
if len(property.value) == 0:
lines.append(f"{property.name}: (None)")
else:
lines.append(f"{property.name}: `{property.value}`")
return "\n".join(lines)
def generate_changes_text(changes: List[PropertyChange]) -> str:
lines = []
for change in changes:
old_change_text = f"`{change['old_value']}`" if len(change["old_value"]) > 0 else "(None)"
new_change_text = f"`{change['new_value']}`" if len(change["new_value"]) > 0 else "(None)"
lines.append(f"{change['property_name']}: {old_change_text} → {new_change_text}")
return "\n".join(lines)
def generate_added_messages(additions: List[Page]) -> List[WebhookEmbed]:
res = []
for addition in additions:
message = WebhookEmbed(
title=f"Task Added: {addition['properties']['title'].value}",
url=addition["url"],
description=generate_properties_text(addition),
color=MessageColor.ADD.value
)
res.append(message)
return res
def generate_removed_messages(removals: List[Page]) -> List[WebhookEmbed]:
res = []
for removal in removals:
message = WebhookEmbed(
title=f"Task Deleted: {removal['properties']['title'].value}",
url = removal["url"],
description = "",
color=MessageColor.REMOVE.value
)
res.append(message)
return res
def generate_changed_messages(pages: List[Tuple[Page, List[PropertyChange]]]) -> List[WebhookEmbed]:
res = []
for page, changes in pages:
message = WebhookEmbed(
title = f"Task Changed: {page['properties']['title'].value}",
url = page["url"],
description = generate_changes_text(changes),
color = MessageColor.CHANGE.value
)
res.append(message)
return res