-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
79 lines (65 loc) · 2.33 KB
/
models.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
import datetime
from zlib import crc32
from pydantic import BaseModel
import icalendar
from src.utils import get_color
class WorkshopEvent(BaseModel):
summary: str
speaker: str = None
comments: list[str] = None
location: str = None
capacity: str = None
timeslots: list[tuple[datetime.time, datetime.time]] = None
dtstart: datetime.datetime = None
dtend: datetime.datetime = None
def __hash__(self) -> int:
string_to_hash = str(
(
"bootcamp",
"workshops",
self.summary,
self.dtstart.isoformat(),
self.dtend.isoformat(),
)
)
return crc32(string_to_hash.encode("utf-8"))
def get_uid(self) -> str:
return "%[email protected]" % abs(hash(self))
def set_datetime(self, start_time, end_time, date):
self.dtstart = datetime.datetime.combine(date, start_time)
self.dtend = datetime.datetime.combine(date, end_time)
@property
def description(self):
description = ""
if self.speaker:
description += f"{self.speaker}\n"
if self.comments:
description += "\n".join(self.comments) + "\n"
if self.capacity:
description += f"Capacity: {self.capacity}\n"
if self.location:
description += f"Location: {self.location}\n"
if len(self.timeslots) > 1:
description += "Timeslots:\n"
for i, timeslot in enumerate(self.timeslots):
description += f"{i + 1}) {timeslot[0].strftime('%H:%M')} - {timeslot[1].strftime('%H:%M')}\n"
else:
description += (
f"Timeslot: {self.timeslots[0][0].strftime('%H:%M')} - {self.timeslots[0][1].strftime('%H:%M')}\n"
)
return description
def get_vevent(self):
vevent = icalendar.Event(
summary=self.summary,
uid=self.get_uid(),
categories=["bootcamp"],
dtstart=icalendar.vDatetime(self.dtstart),
dtend=icalendar.vDatetime(self.dtend),
)
description = self.description
if description:
vevent["description"] = description
if self.location:
vevent["location"] = self.location
vevent["color"] = get_color(self.summary)
return vevent