forked from iamkroot/erp-gcal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.py
138 lines (115 loc) · 4.23 KB
/
events.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
from collections import defaultdict
from datetime import timedelta as td
from functools import partial
from dates import (RFC_WEEKDAYS, last_workday, day_changes as CHANGE_DATES,
midsem_dates as MIDSEM_DATES, holidays as HOLIDAYS)
from utils import combine_dt
from enum import Flag, auto
class EventType(Flag):
Lectures = auto()
Midsem = auto()
Compre = auto()
All = Lectures | Midsem | Compre
DATE_FMT = '%Y%m%dT%H%M%S'
RFC_WDAY = tuple(RFC_WEEKDAYS.values())
COLORS = {'event': {'L': '9', 'P': '6', 'T': '10'},
'midsem': '4', 'compre': '7'}
INCLUDE_DATES = defaultdict(list)
for change_date, day in CHANGE_DATES.items():
INCLUDE_DATES[day].append(change_date)
LAST_DATE = (last_workday + td(days=1)).strftime('%Y%m%d')
def join_event_dt(event, date_):
return combine_dt(date_, event['start'].time()).strftime(DATE_FMT)
def get_indates(event):
indates = set()
for wday in event['wdays']:
dates = INCLUDE_DATES.get(RFC_WDAY[wday - 1], [])
indates.update(dates)
return indates
def get_exdates(event, indates):
exdates = HOLIDAYS.copy()
if MIDSEM_DATES:
midsem_date = MIDSEM_DATES['start']
while midsem_date <= MIDSEM_DATES['end']:
if midsem_date.isoweekday() in event['wdays']:
exdates.add(midsem_date)
midsem_date = midsem_date + td(days=1)
for change_date in CHANGE_DATES:
if change_date.isoweekday() in event['wdays'] and change_date not in indates:
exdates.add(change_date)
return exdates
def make_section_events(course_name, section):
for event in section['sched']:
rrule = {
'FREQ': 'WEEKLY',
'BYDAY': ','.join(RFC_WDAY[day - 1] for day in event['wdays']),
'UNTIL': LAST_DATE
}
get_dt = partial(join_event_dt, event)
indates = get_indates(event)
exdates = get_exdates(event, indates)
gcal_event = {
'summary': f"{course_name} {section['num']}",
'description': section['instructors'],
'location': event['room'],
'start': {
'dateTime': event['start'].isoformat(),
'timeZone': 'Asia/Kolkata'
},
'end': {
'dateTime': event['end'].isoformat(),
'timeZone': 'Asia/Kolkata'
},
'recurrence': [
'RRULE:' + ";".join(f"{k}={v}" for k, v in rrule.items()),
],
'reminders': {
'useDefault': False,
'overrides': [
{
'method': 'popup',
'minutes': 10
}
]
},
'colorId': COLORS['event'][section['num'][0]]
}
if indates:
gcal_event['recurrence'].append(
'RDATE;TZID=Asia/Kolkata:' + ','.join(map(get_dt, indates)))
if exdates:
gcal_event['recurrence'].append(
'EXDATE;TZID=Asia/Kolkata:' + ','.join(map(get_dt, exdates)))
yield gcal_event
def make_event(title, start, end, color):
return {
'summary': title,
'start': {
'dateTime': start.isoformat(),
'timeZone': 'Asia/Kolkata'
},
'end': {
'dateTime': end.isoformat(),
'timeZone': 'Asia/Kolkata'
},
'colorId': color
}
def make_midsem_event(course_name, midsem):
return make_event(course_name + ' Midsem', midsem['start'],
midsem['end'], COLORS['midsem'])
def make_compre_event(course_name, compre):
return make_event(course_name + ' Compre', compre['start'],
compre['end'], COLORS['compre'])
def make_course_events(course, event_types=EventType.All):
name = course['name']
if event_types & EventType.Lectures:
for section in course['sections']:
yield from make_section_events(name, section)
if event_types & EventType.Midsem:
midsem = course.get('midsem')
if midsem:
yield make_midsem_event(name, midsem)
if event_types & EventType.Compre:
compre = course.get('compre')
if compre:
yield make_compre_event(name, compre)