Skip to content

Commit

Permalink
Make class timetables accessible by the `Edupage.get_foreign_timetabl…
Browse files Browse the repository at this point in the history
…e()` method
  • Loading branch information
BelKed committed Jul 11, 2024
1 parent 9943cc4 commit c0bd912
Showing 1 changed file with 45 additions and 24 deletions.
69 changes: 45 additions & 24 deletions edupage_api/foreign_timetables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from datetime import datetime, time, timedelta
from typing import List, Optional

from edupage_api.classes import Classes
from edupage_api.classrooms import Classrooms
from edupage_api.dbi import DbiHelper
from edupage_api.exceptions import (
InsufficientPermissionsException,
Expand Down Expand Up @@ -86,46 +88,65 @@ def __get_timetable_data(self, id: int, table: str, date: datetime):
return timetable_data_response.get("ttitems")

@ModuleHelper.logged_in
def get_timetable_for_person(self, id: int, date: datetime) -> List[LessonSkeleton]:
def get_timetable_for_person(
self, target_id: int, date: datetime
) -> List[LessonSkeleton]:
all_teachers = People(self.edupage).get_teachers()
students = People(self.edupage).get_students()

def teacher_by_id(id: int):
filtered = list(filter(lambda x: x.person_id == id, all_teachers))
def teacher_by_id(target_id: int):
filtered = list(filter(lambda x: x.person_id == target_id, all_teachers))
if not filtered:
raise MissingDataException(f"Teacher with id {id} doesn't exist!")
raise MissingDataException(
f"Teacher with id {target_id} doesn't exist!"
)

return filtered[0]

def student_by_id(id: int):
filtered = list(filter(lambda x: x.person_id == id, students))
def student_by_id(target_id: int):
filtered = list(filter(lambda x: x.person_id == target_id, students))
if not filtered:
raise MissingDataException(f"Student with id {id} doesn't exist!")
raise MissingDataException(
f"Student with id {target_id} doesn't exist!"
)

return filtered[0]

def classroom_by_id(id: str):
return DbiHelper(self.edupage).fetch_classroom_number(id)
def classroom_by_id(target_id: int):
if not Classrooms(self.edupage).get_classroom(target_id):
raise MissingDataException(
f"Classroom with id {target_id} doesn't exist!"
)

def class_by_id(target_id: int):
if not Classes(self.edupage).get_class(target_id):
raise MissingDataException(
f"Classroom with id {target_id} doesn't exist!"
)

def find_table_by_id(target_id):
lookup_functions = [
(teacher_by_id, "teachers"),
(student_by_id, "students"),
(classroom_by_id, "classrooms"),
(class_by_id, "classes"),
]

for func, table_name in lookup_functions:
try:
func(target_id)
return table_name
except:
continue

table = None
try:
teacher_by_id(id)
table = "teachers"
except:
try:
student_by_id(id)
table = "students"
except:
if classroom_by_id(id):
table = "classrooms"

if not table:
raise MissingDataException(
f"Teacher, student or class with id {id} doesn't exist!"
f"Teacher, student, classroom, or class with id {target_id} doesn't exist!"
)

table = find_table_by_id(target_id)

try:
timetable_data = self.__get_timetable_data(id, table, date)
timetable_data = self.__get_timetable_data(target_id, table, date)
except RequestError as e:
if "insuficient" in str(e).lower():
raise InsufficientPermissionsException(f"Missing permissions: {str(e)}")
Expand Down

0 comments on commit c0bd912

Please sign in to comment.