-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
158 lines (126 loc) · 4.34 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import datetime
from typing import Optional
from zlib import crc32
import icalendar
from pydantic import BaseModel
from src.config_base import VDayOfWeek
from src.utils import get_color, nearest_weekday
class ResponseSports(BaseModel):
class Sport(BaseModel):
id: int
name: str
special: bool
sports: list[Sport]
class ResponseSportSchedule(BaseModel):
class SportScheduleEvent(BaseModel):
class ExtendedProps(BaseModel):
group_id: int
training_class: str
current_load: int
capacity: int
def __hash__(self):
string_to_hash = str(
(
self.group_id,
self.training_class,
)
)
return crc32(string_to_hash.encode("utf-8"))
title: Optional[str]
daysOfWeek: list[int]
startTime: str
endTime: str
extendedProps: ExtendedProps
def __hash__(self):
string_to_hash = str(
(
self.title,
self.daysOfWeek,
self.startTime,
self.endTime,
hash(self.extendedProps),
)
)
return crc32(string_to_hash.encode("utf-8"))
__root__: list[SportScheduleEvent]
class SportScheduleEvent(BaseModel):
sport: ResponseSports.Sport
sport_schedule_event: ResponseSportSchedule.SportScheduleEvent
@property
def summary(self) -> str:
title = self.sport.name
if subtitle := self.sport_schedule_event.title:
title += " - " + subtitle
return title
@property
def location(self) -> str:
return self.sport_schedule_event.extendedProps.training_class
@property
def start(self) -> datetime.time:
return datetime.datetime.strptime(self.sport_schedule_event.startTime, "%H:%M:%S").time()
@property
def end(self) -> datetime.time:
return datetime.datetime.strptime(self.sport_schedule_event.endTime, "%H:%M:%S").time()
def __hash__(self):
string_to_hash = str(
(
self.sport.id,
hash(self.sport_schedule_event),
)
)
return crc32(string_to_hash.encode("utf-8"))
def get_uid(self) -> str:
"""
Get unique identifier for the event
:return: unique identifier
:rtype: str
"""
return "%[email protected]" % abs(hash(self))
@property
def description(self) -> str:
"""
Description of the event
:return: description of the event
:rtype: str
"""
r = {
"Location": self.location,
"Time": f"{self.start.strftime('%H:%M')} - {self.end.strftime('%H:%M')}",
"Special": self.sport.special if self.sport.special else None,
}
r = {k: v for k, v in r.items() if v}
return "\n".join([f"{k}: {v}" for k, v in r.items()])
def get_vevent(self, very_first_date: datetime.date, very_last_date: datetime.date) -> icalendar.Event:
"""
Get icalendar event
:return: icalendar event
:rtype: icalendar.Event
"""
vevent = icalendar.Event()
vevent["summary"] = self.summary
vevent["location"] = self.location
vevent["uid"] = self.get_uid()
starting = nearest_weekday(
very_first_date,
self.sport_schedule_event.daysOfWeek[0] - 1, # 0 is Monday
)
dtstart = datetime.datetime.combine(starting, self.start)
dtend = datetime.datetime.combine(starting, self.end)
vevent["rrule"] = icalendar.vRecur(
{
"freq": "weekly",
"until": very_last_date,
"byday": [
icalendar.vWeekday(VDayOfWeek.get_by_index(day - 1)) for day in self.sport_schedule_event.daysOfWeek
],
}
)
vevent["dtstart"] = icalendar.vDatetime(dtstart)
vevent["dtend"] = icalendar.vDatetime(dtend)
vevent["categories"] = [
icalendar.vText("sport"),
icalendar.vText(self.sport.name),
]
vevent["color"] = get_color(self.summary)
vevent["description"] = icalendar.vText(self.description)
return vevent