Py.Cafe

KuboHA/

edupage-student-dashboard

Edupage Student Dashboard

DocsPricing
  • static/
  • templates/
  • app.py
  • index.html
  • requirements.txt
app.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
from flask import Flask, render_template, request, redirect, url_for, session
from edupage_api import Edupage
from edupage_api.substitution import Substitution, TimetableChange
from datetime import datetime, date, timedelta
from typing import Optional, Union
from enum import Enum

app = Flask(__name__)
app.secret_key = '738-531-827'

class EduStudent:
    def __init__(self, person_id: int, name: str, gender: str, in_school_since: Optional[datetime], class_id: int, number_in_class: int):
        self.person_id = person_id
        self.name = name
        self.gender = gender
        self.in_school_since = in_school_since
        self.class_id = class_id
        self.number_in_class = number_in_class

def time_since_posted(timestamp):
    now = datetime.now()
    diff = now - timestamp

    if diff.days > 0:
        return f"{diff.days} days ago"
    elif diff.seconds // 3600 > 0:
        return f"{diff.seconds // 3600} hours ago"
    elif diff.seconds // 60 > 0:
        return f"{diff.seconds // 60} minutes ago"
    else:
        return "Just now"

@app.route('/')
def index():
    return render_template('login.html')

@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    password = request.form['password']
    subdomain = request.form['subdomain']

    edupage = Edupage()
    try:
        two_factor_login = edupage.login(username, password, subdomain)

        student_info = None
        try:
            students = edupage.get_students()
            for student in students:
                if username.lower() in student.name.lower().replace(' ', ''):
                    student_info = student
                    break
        except Exception as e:
            print(f"Error getting student info: {e}")

        if two_factor_login:
            session['subdomain'] = subdomain
            session['username'] = student_info.name if student_info else username
            session['student_id'] = student_info.person_id if student_info else None
            session['password'] = password  # Store password in session
            session['session_id'] = edupage.session.cookies.get('PHPSESSID')
            return redirect(url_for('two_factor'))
        else:
            session['subdomain'] = subdomain
            session['username'] = student_info.name if student_info else username
            session['student_id'] = student_info.person_id if student_info else None
            session['password'] = password
            session['session_id'] = edupage.session.cookies.get('PHPSESSID')
            return redirect(url_for('dashboard'))
    except Exception as e:
        return render_template('login.html', error=str(e))

@app.route('/two_factor', methods=['GET', 'POST'])
def two_factor():
    if 'subdomain' not in session or 'username' not in session or 'session_id' not in session:
        return redirect(url_for('index'))

    if request.method == 'POST':
        code = request.form['code']
        edupage = Edupage()
        try:
            edupage.login(session['username'], session['password'], session['subdomain'])
            edupage.session.cookies.set('PHPSESSID', session['session_id'])

            two_factor_login = edupage.login(session['username'], session['password'], session['subdomain'])
            two_factor_login.finish_with_code(code)

            session['session_id'] = edupage.session.cookies.get('PHPSESSID')
            return redirect(url_for('dashboard'))
        except Exception as e:
            return render_template('two_factor.html', error=str(e))

    return render_template('two_factor.html')

@app.route('/dashboard')
def dashboard():
    if 'subdomain' not in session or 'username' not in session or 'session_id' not in session:
        return redirect(url_for('index'))

    edupage = Edupage()
    edupage.login(session['username'], session['password'], session['subdomain'])
    edupage.session.cookies.set('PHPSESSID', session['session_id'])

    students = edupage.get_students()
    if 'student_id' in session and session['student_id']:
        student_data = next((student for student in students if student.person_id == session['student_id']), None)
    else:
        student_data = next((student for student in students if student.name == session['username']), None)
    
    notifications = edupage.get_notifications()
    
    today = date.today()
    
    def should_show_tomorrow():
        now = datetime.now()
        cutoff = now.replace(hour=14, minute=30, second=0, microsecond=0)
        return now >= cutoff

    if should_show_tomorrow():
        target_date = today + timedelta(days=1)
    else:
        target_date = today

    timetable = edupage.get_my_timetable(target_date)

    for notification in notifications:
        notification.formatted_timestamp = time_since_posted(notification.timestamp)
    
    now = datetime.now()
    cutoff_time = now.replace(hour=14, minute=30, second=0, microsecond=0)
    show_tomorrow = now >= cutoff_time
    
    today = date.today()
    target_date = today + timedelta(days=1) if show_tomorrow else today
    
    meals_data = edupage.get_meals(target_date)
    
    meals_list = []
    if meals_data:
        if meals_data.snack:
            meals_list.append({
                'type': 'Snack',
                'menus': meals_data.snack.menus,
                'ordered_meal': meals_data.snack.ordered_meal if meals_data.snack.ordered_meal else 'X'
            })
        if meals_data.lunch:
            meals_list.append({
                'type': 'Lunch',
                'menus': meals_data.lunch.menus,
                'ordered_meal': meals_data.lunch.ordered_meal if meals_data.lunch.ordered_meal else 'X'
            })
        if meals_data.afternoon_snack:
            meals_list.append({
                'type': 'Afternoon Snack',
                'menus': meals_data.afternoon_snack.menus,
                'ordered_meal': meals_data.afternoon_snack.ordered_meal if meals_data.afternoon_snack.ordered_meal else 'X'
            })

    return render_template('dashboard.html', 
                         student=student_data,
                         notifications=notifications,
                         timetable=timetable,
                         meals=meals_list,
                         show_tomorrow=show_tomorrow,
                         event_type_map=EVENT_TYPE_MAP,
                         event_type_icons=EVENT_TYPE_ICONS)

@app.route('/substitutions/', defaults={'date_str': None})
@app.route('/substitutions/<date_str>')
def get_timetable_changes(date_str):
    if 'subdomain' not in session or 'username' not in session or 'session_id' not in session:
        return redirect(url_for('index'))

    # Recreate the Edupage object
    edupage = Edupage()
    edupage.login(session['username'], session['password'], session['subdomain'])
    edupage.session.cookies.set('PHPSESSID', session['session_id'])
    
    try:
        if date_str:
            specific_date = datetime.strptime(date_str, '%Y-%m-%d').date()
        else:
            specific_date = date.today()
    except ValueError:
        specific_date = date.today()

    prev_date = specific_date - timedelta(days=1)
    next_date = specific_date + timedelta(days=1)

    students = edupage.get_students()
    if 'student_id' in session and session['student_id']:
        student_data = next((student for student in students if student.person_id == session['student_id']), None)
    else:
        student_data = next((student for student in students if student.name == session['username']), None)

    substitution = Substitution(edupage)
    changes = substitution.get_timetable_changes(specific_date)

    if changes is None:
        changes = []

    changes = [change for change in changes if change.change_class == '2.A']

    return render_template('substitutions.html', changes=changes, student=student_data, current_date=specific_date, prev_date=prev_date, next_date=next_date)


@app.route('/lunches/', defaults={'date_str': None})
@app.route('/lunches/<date_str>')
def lunches(date_str):
    if 'subdomain' not in session or 'username' not in session or 'session_id' not in session:
        return redirect(url_for('index'))

    edupage = Edupage()
    edupage.login(session['username'], session['password'], session['subdomain'])
    edupage.session.cookies.set('PHPSESSID', session['session_id'])

    try:
        if date_str:
            specific_date = datetime.strptime(date_str, '%Y-%m-%d').date()
        else:
            specific_date = date.today()
    except ValueError:
        specific_date = date.today()

    prev_date = specific_date - timedelta(days=1)
    next_date = specific_date + timedelta(days=1)

    meals_data = edupage.get_meals(specific_date)

    students = edupage.get_students()
    if 'student_id' in session and session['student_id']:
        student_data = next((student for student in students if student.person_id == session['student_id']), None)
    else:
        student_data = next((student for student in students if student.name == session['username']), None)
    
    if meals_data is None:
        meals_list = []
    else:
        meals_list = []
        if meals_data.snack:
            meals_list.append(meals_data.snack)
        if meals_data.lunch:
            meals_list.append(meals_data.lunch)
        if meals_data.afternoon_snack:
            meals_list.append(meals_data.afternoon_snack)

    return render_template('lunches.html', 
                         meals=meals_list, 
                         current_date=specific_date,
                         student=student_data,
                         prev_date=prev_date,
                         next_date=next_date)

class Term(Enum):
    FIRST = "P1"
    SECOND = "P2"

@app.route('/grades')
def grades():
    if 'subdomain' not in session or 'username' not in session or 'session_id' not in session:
        return redirect(url_for('index'))

    edupage = Edupage()
    edupage.login(session['username'], session['password'], session['subdomain'])
    edupage.session.cookies.set('PHPSESSID', session['session_id'])

    year = edupage.get_school_year()
    grades_data = edupage.get_grades_for_term(year, Term.SECOND)
    students = edupage.get_students()
    if 'student_id' in session and session['student_id']:
        student_data = next((student for student in students if student.person_id == session['student_id']), None)
    else:
        student_data = next((student for student in students if student.name == session['username']), None)
    
    if grades_data:
        grades_data.sort(key=lambda x: x.date, reverse=True)

    return render_template('grades.html', grades=grades_data, student=student_data)

@app.route('/timetable/', defaults={'date_str': None})
@app.route('/timetable/<date_str>')
def get_timetable(date_str):
    if 'subdomain' not in session or 'username' not in session or 'session_id' not in session:
        return redirect(url_for('index'))

    edupage = Edupage()
    edupage.login(session['username'], session['password'], session['subdomain'])
    edupage.session.cookies.set('PHPSESSID', session['session_id'])

    try:
        if date_str:
            specific_date = datetime.strptime(date_str, '%Y-%m-%d').date()
        else:
            specific_date = date.today()
    except ValueError:
        specific_date = date.today()

    prev_date = specific_date - timedelta(days=1)
    next_date = specific_date + timedelta(days=1)

    students = edupage.get_students()
    if 'student_id' in session and session['student_id']:
        student_data = next((student for student in students if student.person_id == session['student_id']), None)
    else:
        student_data = next((student for student in students if student.name == session['username']), None)
    
    if student_data is None:
        return redirect(url_for('index'))

    timetable = edupage.get_my_timetable(specific_date)

    if timetable is None:
        timetable = []

    return render_template('timetable.html', timetable=timetable, student=student_data, current_date=specific_date, prev_date=prev_date, next_date=next_date)

EVENT_TYPE_MAP = {
    "album": "Album",
    "arrival_to_school": "Arrival to School",
    "bee": "Bee",
    "big_exam": "Big Exam",
    "booked_room": "Booked Room",
    "change_room": "Change Room",
    "classification_meeting": "Classification Meeting",
    "class_book": "Class Book",
    "class_teacher_event": "Class Teacher Event",
    "class_teacher_lesson": "Class Teacher Lesson",
    "confirmation": "Confirmation",
    "contest": "Contest",
    "culture": "Culture",
    "distant_learning": "Distant Learning",
    "event": "Event",
    "exam_assignment": "Exam Assignment",
    "exam_evaluation": "Exam Evaluation",
    "excursion": "Excursion",
    "excused_lesson": "Excused Lesson",
    "food_credit": "Food Credit",
    "strava_vydaj": "Food Served",
    "free_day": "Free Day",
    "grade": "Grade",
    "grades_doc": "Grades Document",
    "holiday": "Holiday",
    "homework": "Homework",
    "homework_student_state": "Homework Student State",
    "homework_test": "Homework Test",
    "h_attendance": "Attendance",
    "h_bee": "Bee",
    "h_clearcache": "Clear Cache",
    "h_cleardbi": "Clear DBI",
    "h_clearisicdata": "Clear ISIC Data",
    "h_contest": "Contest",
    "h_dailyplan": "Daily Plan",
    "h_edusettings": "Edu Settings",
    "h_financie": "Finances",
    "h_grades": "Grades",
    "h_homework": "Homework",
    "h_igroups": "Groups",
    "h_process": "Process",
    "h_processtypes": "Process Types",
    "h_settings": "Settings",
    "h_substitution": "Substitution",
    "h_timetable": "Timetable",
    "h_userphoto": "User Photo",
    "h_stravamenu": "Strava Menu",
    "h_dailyplan": "Daily Plan",
    "pipnutie": "Check In/Out",
    "h_clearplany": "Clear Plans",
    "sprava": "Message",
    "lesson": "Lesson",
    "message": "Message",
    "news": "News",
    "new_menu": "New Menu",
    "oral_exam": "Oral Exam",
    "other": "Other",
    "paper": "Paper",
    "parents_evening": "Parents' Evening",
    "poll": "Poll",
    "process": "Process",
    "project": "Project",
    "project_exam": "Project Exam",
    "project_lesson": "Project Lesson",
    "representation": "Representation",
    "safety_instructioning": "Safety Instructioning",
    "school_event": "School Event",
    "school_trip": "School Trip",
    "short_exam": "Short Exam",
    "short_holiday": "Short Holiday",
    "student_absent": "Student Absent",
    "substitution": "Substitution",
    "teacher_meeting": "Teacher Meeting",
    "testing": "Testing",
    "test_result": "Test Result",
    "testpridelenie": "Assigned Test",
    "timetable": "Timetable",
    "tt_cancel": "Timetable Cancel",
    "tutoring": "Tutoring",
    "znamka": "New Grade"
}

EVENT_TYPE_ICONS = {
    "album": "fa-photo-video",
    "arrival_to_school": "fa-school",
    "bee": "fa-bee",
    "big_exam": "fa-file-alt",
    "booked_room": "fa-door-closed",
    "change_room": "fa-exchange-alt",
    "classification_meeting": "fa-users",
    "class_book": "fa-book",
    "class_teacher_event": "fa-chalkboard-teacher",
    "class_teacher_lesson": "fa-chalkboard",
    "confirmation": "fa-check-circle",
    "contest": "fa-trophy",
    "culture": "fa-theater-masks",
    "distant_learning": "fa-laptop-house",
    "event": "fa-calendar-alt",
    "exam_assignment": "fa-tasks",
    "exam_evaluation": "fa-clipboard-check",
    "excursion": "fa-bus",
    "excused_lesson": "fa-user-check",
    "food_credit": "fa-utensils",
    "strava_vydaj": "fa-utensils",
    "free_day": "fa-sun",
    "grade": "fa-graduation-cap",
    "grades_doc": "fa-file-alt",
    "holiday": "fa-umbrella-beach",
    "homework": "fa-book-open",
    "homework_student_state": "fa-user-graduate",
    "homework_test": "fa-file-alt",
    "h_attendance": "fa-user-check",
    "h_bee": "fa-bee",
    "h_clearcache": "fa-trash-alt",
    "h_cleardbi": "fa-trash-alt",
    "h_clearisicdata": "fa-trash-alt",
    "h_contest": "fa-trophy",
    "h_dailyplan": "fa-calendar-day",
    "h_edusettings": "fa-cogs",
    "h_financie": "fa-money-bill-wave",
    "h_grades": "fa-graduation-cap",
    "h_homework": "fa-book-open",
    "h_igroups": "fa-users",
    "h_process": "fa-cogs",
    "h_processtypes": "fa-cogs",
    "h_settings": "fa-cogs",
    "h_substitution": "fa-exchange-alt",
    "h_timetable": "fa-calendar-alt",
    "h_userphoto": "fa-user",
    "h_stravamenu": "fa-utensils",
    "pipnutie": "fa-sign-in-alt",
    "h_clearplany": "fa-trash-alt",
    "sprava": "fa-envelope",
    "lesson": "fa-chalkboard",
    "message": "fa-envelope",
    "news": "fa-newspaper",
    "new_menu": "fa-bars",
    "oral_exam": "fa-microphone",
    "other": "fa-ellipsis-h",
    "paper": "fa-file-alt",
    "parents_evening": "fa-users",
    "poll": "fa-poll",
    "process": "fa-cogs",
    "project": "fa-project-diagram",
    "project_exam": "fa-file-alt",
    "project_lesson": "fa-chalkboard",
    "representation": "fa-users",
    "safety_instructioning": "fa-shield-alt",
    "school_event": "fa-school",
    "school_trip": "fa-bus",
    "short_exam": "fa-file-alt",
    "short_holiday": "fa-umbrella-beach",
    "student_absent": "fa-user-times",
    "substitution": "fa-exchange-alt",
    "teacher_meeting": "fa-users",
    "testing": "fa-vial",
    "test_result": "fa-clipboard-check",
    "testpridelenie": "fa-tasks",
    "timetable": "fa-calendar-alt",
    "tt_cancel": "fa-calendar-times",
    "tutoring": "fa-chalkboard-teacher",
    "znamka": "fa-graduation-cap"
}

if __name__ == '__main__':
    app.run(debug=True)