-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathitem.py
124 lines (99 loc) · 3.45 KB
/
item.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from enum import Enum
from calrules.rules import Response
from exchangelib.items.calendar_item import BaseMeetingItem, MeetingCancellation, MeetingRequest
from exchangelib.properties import MessageHeader
class ItemType(Enum):
REQUEST = 1
CANCELLATION = 2
class ItemError(Exception):
pass
class ItemActionError(Exception):
pass
class ItemThreadIndexError(Exception):
pass
class Item:
item: BaseMeetingItem
type: ItemType
subject: str
sender: str
recipients: list[str]
has_conflicts: bool
sent_date: int
modified: bool
duration: str
action: Response
def __init__(self, item: BaseMeetingItem = None):
self.action = Response.NOOP
self.item = item
if item is not None:
self.hydrate(item)
def hydrate(self, item: BaseMeetingItem):
if item is None:
raise ItemError('No item passed in')
if type(item) is MeetingRequest:
self.type = ItemType.REQUEST
elif type(item) is MeetingCancellation:
self.type = ItemType.CANCELLATION
else:
raise ItemError("Type not known")
self.subject = item.subject
self.sender = item.sender.email_address
if item.to_recipients is None and item.cc_recipients is None:
to_recipients = []
elif item.cc_recipients is None:
to_recipients = item.to_recipients
elif item.to_recipients is None:
to_recipients = item.cc_recipients
else:
to_recipients = item.to_recipients + item.cc_recipients
self.recipients = [to.email_address for to in to_recipients]
if isinstance(item, MeetingRequest) and item.conflicting_meeting_count > 0:
self.has_conflicts = True
else:
self.has_conflicts = False
if isinstance(item, MeetingRequest):
self.duration = item.duration
self.modified = False if item.is_unmodified else True
def mark(self, action: Response, message: str):
self.action = action
self.action_message = message
def to_dict(self):
item_type = "REQUEST" if self.type is ItemType.REQUEST else "CANCELLATION"
return {
'type': item_type,
'subject': self.subject,
'sender': self.sender,
'recipients': self.recipients,
'has_conflicts': self.has_conflicts,
# 'duration': self.duration,
'modified': self.modified
}
def delete(self):
try:
if self.item.associated_calendar_item_id:
self.item.account.inbox.get(
id=self.item.associated_calendar_item_id.id,
changekey=self.item.associated_calendar_item_id.changekey
).delete()
self.item.delete(affected_task_occurrences='AllOccurrences')
except Exception as e:
raise ItemActionError(e)
def accept(self):
try:
self.item.accept()
except Exception as e:
raise ItemActionError(e)
def decline(self):
try:
self.item.decline()
except Exception as e:
raise ItemActionError(e)
def maybe(self):
try:
self.item.tentatively_accept()
except Exception as e:
raise ItemActionError(e)
def thread_index(self) -> str:
for h in self.item.headers:
if h.name == "Thread-Index":
return h.value