-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
268 lines (229 loc) · 10.4 KB
/
main.py
File metadata and controls
268 lines (229 loc) · 10.4 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python3
"""
A NiceGUI app to plan appointments for multiple teachers using timetables
from WebUntis. It fetches the timetables of specified teachers and displays a
weekly overview highlighting busy slots.
"""
import configparser
from datetime import datetime, timedelta
from fastapi import Request
from nicegui import app, ui, events
import untisplanner
from fullcalendar import FullCalendar
APP_TITLE = 'UntisPlanner'
DEBUG = False
# create list with five colors for teachers
TEACHER_COLORS = [
"#f5766f",
'#fad07b',
"#62d5dd",
'#c77dff',
"#71d93a"
]
UNTIS_API = None
LESSON_CALENDAR = None
class Language:
"""
A class to handle language selection and retrieval.
Based on: https://github.com/zauberzeug/nicegui/discussions/5840
"""
@property
def current(self) -> str:
"""Gets the current language code from user storage, defaulting to 'en' if not set."""
return app.storage.user.get('language', 'en')
@current.setter
def current(self, code: str) -> None:
"""Sets the current language code in user storage if it's a supported language."""
if code in ('de', 'en'):
app.storage.user['language'] = code
def detect_from_request(self):
"""Detects the preferred language from the client's request headers and sets it accordingly."""
if ui.context.client.request:
code = ''
for lang in ui.context.client.request.headers.get('accept-language', '').split(','):
if lang[:2] in ('de', 'en'):
code = lang[:2]
break
if code:
app.storage.user['language'] = code
@property
def is_de(self) -> bool:
"""Returns True if the current language is German ('de'), otherwise False."""
return self.current == 'de'
@property
def is_en(self) -> bool:
"""Returns True if the current language is English ('en'), otherwise False."""
return self.current == 'en'
LANGUAGE = Language()
def prepare_events():
"""Prepare calendar events based on the selected teachers and date range."""
LESSON_CALENDAR.clear_events()
for i, teacher in enumerate(app.storage.client['selected_teachers']):
current_teacher = UNTIS_API.session.teachers().filter(surname=teacher)[0]
timetable_entries = UNTIS_API.get_timetable(current_teacher, start=app.storage.client['start_date'],
end=app.storage.client['end_date'])
for lesson in timetable_entries:
# filter out periods that aren't lessons
if lesson.klassen and lesson.subjects[0].name != '---':
add_event_for_lesson(teacher, lesson, TEACHER_COLORS[i % len(TEACHER_COLORS)])
def add_event_for_lesson(teacher, lesson, color):
"""
Adds an event to the calendar for a given lesson, ensuring no duplicates
for the same teacher and time slot.
"""
try:
for _ in lesson.teachers:
# remove existing event for the teacher in the same time slot to avoid duplicates
LESSON_CALENDAR.remove_event(title=teacher, start=str(lesson.start), end=str(lesson.end))
# create new event for all periods of the teacher
LESSON_CALENDAR.add_event(title=teacher, start=str(lesson.start), end=str(lesson.end),
display='block', color=color,
classes=', '.join(str(klasse) for klasse in lesson.klassen),
subjects=', '.join(str(subject) for subject in lesson.subjects))
if DEBUG:
print(f'Added event: Teacher={teacher}, Start={lesson.start}, End={lesson.end}, '
f'Classes={lesson.klassen}, Subjects={lesson.subjects}')
except IndexError as e:
if DEBUG:
print(f'Error processing period: {e}')
def prepare_dropdown(teacher_list):
"""Gets last names for all teachers and prepares the dropdown."""
teacher_names = [teacher.surname for teacher in teacher_list]
ui.select(options=teacher_names, multiple=True, with_input=True, new_value_mode='add-unique',
clearable=True, label='Select teachers:' if LANGUAGE.is_en else 'Lehrkraft auswählen:',
on_change=handle_teacher_change)
def handle_teacher_change(event: events.GenericEventArguments):
"""
Updates the list of selected teachers based on changes in the dropdown.
Reverts the list to the previous value if more than 5 items are selected.
"""
if len(event.value) > 5:
event.sender.value = app.storage.client['selected_teachers']
ui.notify('You can only select up to 5 teachers' if LANGUAGE.is_en
else 'Sie können nur bis zu 5 Lehrkräfte auswählen', color='warning')
else:
if DEBUG:
added = set(event.value) - set(app.storage.client['selected_teachers'])
removed = set(app.storage.client['selected_teachers']) - set(event.value)
if added:
ui.notify(f'Added: {", ".join(added)}' if LANGUAGE.is_en else f'Hinzugefügt: {", ".join(added)}')
if removed:
ui.notify(f'Removed: {", ".join(removed)}' if LANGUAGE.is_en else f'Entfernt: {", ".join(removed)}')
app.storage.client['selected_teachers'] = event.value
prepare_events()
prepare_legend.refresh()
def prepare_calendar():
"""
Prepares the FullCalendar component with specified options and styling. No
events are added at this stage; they will be added dynamically based on the
selected teachers and date range by prepare_events().
"""
global LESSON_CALENDAR
options = {
'editable': False,
'selectable': False,
'businessHours': {
'daysOfWeek': [1, 2, 3, 4, 5],
'startTime': '08:00',
'endTime': '15:00',
},
'locale': LANGUAGE.current,
'timeZone': 'local',
'allDaySlot': False,
'nowIndicator': True,
'weekends': False,
'headerToolbar': {
'left': '',
'center': 'title',
'right': 'prev,next',
},
'slotMinTime': '06:00:00',
'slotMaxTime': '21:00:00',
'initialView': 'timeGridWeek',
'height': 'auto',
'width': 'auto',
'events': [],
}
custom_css="""
.fc-event-time {
display: none;
}
.fc-event-title {
display: none;
font-weight: 800;
}
"""
LESSON_CALENDAR = FullCalendar(options, custom_css, on_click=handle_click, on_change=handle_change)
def handle_click(event: events.GenericEventArguments):
"""Show notification with information about the clicked lesson."""
if 'info' in event.args:
e = event.args['info']['event']
title = e['title']
classes = e['extendedProps']['classes']
subjects = e['extendedProps']['subjects']
if DEBUG:
ui.notify(f'Teacher: {title}, Class: {classes}, Subject: {subjects}' if LANGUAGE.is_en
else f'Lehrkraft: {title}, Klasse: {classes}, Fach: {subjects}')
def handle_change(event: events.GenericEventArguments):
"""Update the date range in storage when the calendar view changes and refresh events."""
if 'info' in event.args:
start_date = datetime.fromisoformat(event.args['info']['startStr']).date()
end_date = datetime.fromisoformat(event.args['info']['endStr']).date()
if DEBUG:
print(f'Current view range: {start_date} - {end_date}' if LANGUAGE.is_en
else f'Aktuelle Ansicht: {start_date} - {end_date}')
app.storage.client['start_date'] = start_date
app.storage.client['end_date'] = end_date
prepare_events()
@ui.refreshable
def prepare_legend():
"""Prepares a legend showing the selected teachers with their corresponding colors."""
with ui.row().classes('justify-center gap-12 w-full'):
for i, teacher_name in enumerate(app.storage.client['selected_teachers']):
with ui.column():
color = TEACHER_COLORS[i % len(TEACHER_COLORS)]
s = f'background-color: {color}; color: white; padding: 10px 20px;' \
'border-radius: 10px; font-weight: bold;font-size: 125%'
ui.label(teacher_name).classes('text-lg').style(s)
ui.space()
def preload_logged_in_user(request: Request, teacher_list):
"""Preloads the logged-in user's name into the dropdown if available."""
if 'X-authentik-username' in request.headers:
username_from_request = request.headers['X-authentik-username']
if DEBUG:
print(f"Authenticated user: {username_from_request}" if LANGUAGE.is_en
else f"Authentifizierter Benutzer: {username_from_request}")
teacher_name = [teacher.surname for teacher in teacher_list
if str(teacher.surname).casefold() == str(username_from_request).casefold()]
if teacher_name:
app.storage.client['selected_teachers'].append(teacher_name[0])
prepare_events()
prepare_legend.refresh()
return username_from_request
return None
@ui.page('/')
def main():
"""Main function to set up the NiceGUI app, load configuration, and initialize components."""
LANGUAGE.detect_from_request()
# load configuration from file
configfile = 'webuntis-config.ini'
config = configparser.ConfigParser()
config.read(configfile)
# create an UntisPlaner instance
cred = config['credentials']
global UNTIS_API
UNTIS_API = untisplanner.UntisPlanner(cred['user'], cred['password'], cred['server'], cred['school'])
teacher_list = UNTIS_API.get_list_of_teachers()
# define defaults
app.storage.client['start_date'] = datetime.now().date() - timedelta(days=datetime.now().weekday())
app.storage.client['end_date'] = app.storage.client['start_date'] + timedelta(days=6)
app.storage.client['selected_teachers'] = []
# build UI elements
ui.html(f'<h1 style="font-size: 4em;">{APP_TITLE}</h1>')
prepare_dropdown(teacher_list)
prepare_calendar()
prepare_legend()
if __name__ in {'__main__', '__mp_main__'}:
# run the NiceGUI app
ui.run(host='0.0.0.0', port=8080, favicon='📆', language='de-DE',
storage_secret='uqu7geitaic7eawee2Ieyaoshatietioshai8aiya')