Rework schedule a bit to make it more maintainable & easier to code

pull/84/head
Stijn De Clercq 2021-08-06 23:47:50 +02:00
parent 54d31c943a
commit 1857bdefe9
4 changed files with 161 additions and 94 deletions

View File

@ -1,9 +1,10 @@
import dacite
from dacite import from_dict from dacite import from_dict
from dataclasses import dataclass, field from dataclasses import dataclass, field
from datetime import datetime, timedelta from datetime import datetime, timedelta
from enums.platforms import Platforms from enums.platform import Platform, get_platform
from functions.config import get from functions.config import get
from functions.timeFormatters import fromArray, forward_to_weekday from functions.timeFormatters import fromArray, forward_to_weekday, intToWeekday
import json import json
from typing import Dict, Optional, List from typing import Dict, Optional, List
@ -30,9 +31,7 @@ class Holiday:
@dataclass @dataclass
class Course: class Course:
day: str name: str
week: int
course_dict: Dict
@dataclass @dataclass
@ -51,8 +50,37 @@ class Timeslot:
is_special: bool = False is_special: bool = False
location: Optional[Location] = None location: Optional[Location] = None
online_link: Optional[str] = None online_link: Optional[str] = None
online_platform: Optional[Platforms] = None online_platform: Optional[Platform] = None
@staticmethod
def from_slot_dict(slot_dict: Dict, course_dict: Dict, current_week: int):
"""
Construct a Timeslot from a dict of data
"""
if "weeks" in slot_dict and str(current_week) in slot_dict["weeks"]:
return Timeslot.special_from_dict(slot_dict, course_dict, str(current_week))
course = Course(course_dict["course"])
start_time = slot_dict["time"]["start"]
end_time = slot_dict["time"]["end"]
# Location can be none if a class is online-only
location = dacite.from_dict(Location, slot_dict["location"]) if "location" in slot_dict else None
# Find platform & link if this class is online
online_platform: Platform = get_platform(slot_dict.get("online", None))
online_link = course_dict["online_links"][Platform.value["rep"]] if online_platform is not None else None
return Timeslot(course=course, start_time=start_time, end_time=end_time, canceled=False, is_special=False,
location=location, online_platform=online_platform, online_link=online_link)
@staticmethod
def special_from_dict(slot_dict: Dict, course_dict: Dict, current_week: str):
"""
Create a SPECIAL Timeslot from a dict and data
"""
course = Course(course_dict["course"])
# TODO
@dataclass @dataclass
class Schedule: class Schedule:
@ -78,18 +106,21 @@ class Schedule:
self.check_holidays() self.check_holidays()
# Store the target weekday (in case it exists) so we can ask for the next # TODO show a custom embed when no class instead of fast-forwarding
# friday after the holiday, for example # # Store the target weekday (in case it exists) so we can ask for the next
target_weekday = -1 if not self.targetted_weekday else self.day.weekday() # # friday after the holiday, for example
# target_weekday = -1 if not self.targetted_weekday else self.day.weekday()
#
# # Show schedule for after holidays
# if self.current_holiday is not None:
# # Set day to day after holiday
# self.day = self.current_holiday.end_date_parsed + timedelta(days=1)
#
# # Find the next [DAY] after the holidays
# if target_weekday != -1:
# self.day = forward_to_weekday(self.day, target_weekday)
# Show schedule for after holidays self._weekday_str = intToWeekday(self.day.weekday())
if self.current_holiday is not None:
# Set day to day after holiday
self.day = self.current_holiday.end_date_parsed + timedelta(days=1)
# Find the next [DAY] after the holidays
if target_weekday != -1:
self.day = forward_to_weekday(self.day, target_weekday)
print(self.day) print(self.day)
@ -114,8 +145,8 @@ class Schedule:
""" """
Load the schedule from the JSON file Load the schedule from the JSON file
""" """
semester = get("semester") semester = get_platform("semester")
year = get("year") year = get_platform("year")
with open(f"files/schedules/{year}{semester}.json", "r") as fp: with open(f"files/schedules/{year}{semester}.json", "r") as fp:
return json.load(fp) return json.load(fp)
@ -133,14 +164,29 @@ class Schedule:
# Add +1 at the end because week 1 would be 0 as it's not over yet # Add +1 at the end because week 1 would be 0 as it's not over yet
return (diff.days // 7) + self.holiday_offset + 1 return (diff.days // 7) + self.holiday_offset + 1
def find_slot_for_course(self, course_dict: Dict) -> List[Timeslot]: def find_slots_for_course(self, course_dict: Dict, current_week: int) -> List[Timeslot]:
""" """
Create time timeslots for a course Create time timeslots for a course
""" """
pass slots_today = []
# First create a list of all slots of today
for slot in course_dict["slots"]:
# This slot is for a different day
if slot["time"]["day"] != self._weekday_str.lower():
continue
slots_today.append(slot)
# Create Timeslots
slots_today = list(map(lambda x: Timeslot.from_slot_dict(x, course_dict, current_week), slots_today))
return slots_today
def create_schedule(self): def create_schedule(self):
""" """
Create the schedule for the current week Create the schedule for the current week
""" """
week: int = self.get_week() week: int = self.get_week()
slots: List[List[Timeslot]] = [self.find_slots_for_course(course, week) for course in self.schedule_dict["schedule"]]
slots_flattened = [item for sublist in slots for item in sublist]

View File

@ -1,7 +1,8 @@
from enum import Enum from enum import Enum
from typing import Optional
class Platforms(Enum): class Platform(Enum):
""" """
An Enum to represent online class platforms An Enum to represent online class platforms
Name: The name of the platform Name: The name of the platform
@ -11,3 +12,17 @@ class Platforms(Enum):
MSTeams = {"name": "MS Teams", "rep": "msteams"} MSTeams = {"name": "MS Teams", "rep": "msteams"}
Ufora = {"name": "Ufora", "rep": "ufora"} Ufora = {"name": "Ufora", "rep": "ufora"}
Zoom = {"name": "Zoom", "rep": "zoom"} Zoom = {"name": "Zoom", "rep": "zoom"}
def get_platform(rep: Optional[str]) -> Optional[Platform]:
"""
Find the platform that corresponds to the given name
"""
if rep is None:
return None
for platform in Platform:
if platform.value["rep"] == rep:
return platform
return None

View File

@ -10,123 +10,128 @@
"schedule": [ "schedule": [
{ {
"course": "Computerarchitectuur", "course": "Computerarchitectuur",
"online_links": {
"zoom": "https://ufora.ugent.be/d2l/ext/rp/228912/lti/framedlaunch/556e197e-e87b-4c27-be5d-53adc7a41826", "zoom": "https://ufora.ugent.be/d2l/ext/rp/228912/lti/framedlaunch/556e197e-e87b-4c27-be5d-53adc7a41826",
"msteams": "https://teams.microsoft.com/l/team/19%3ad7295f0bc4634a61b461504d4a7134b3%40thread.tacv2/conversations?groupId=8755cb96-1ef5-4ea3-b806-eeebf8a85ae8&tenantId=d7811cde-ecef-496c-8f91-a1786241b99c", "msteams": "https://teams.microsoft.com/l/team/19%3ad7295f0bc4634a61b461504d4a7134b3%40thread.tacv2/conversations?groupId=8755cb96-1ef5-4ea3-b806-eeebf8a85ae8&tenantId=d7811cde-ecef-496c-8f91-a1786241b99c"
},
"slots": [ "slots": [
] ]
}, },
{ {
"course": "Multimedia", "course": "Multimedia",
"zoom": "https://ugent-be.zoom.us/j/94248831947?pwd=ZCt4UnBLSzViZnFEQmkzWE5SYnF2QT09", "online_links": {
"zoom": "https://ugent-be.zoom.us/j/94248831947?pwd=ZCt4UnBLSzViZnFEQmkzWE5SYnF2QT09"
},
"slots": [ "slots": [
{ {
"location": {
"campus": "Sterre", "campus": "Sterre",
"building": "S9", "building": "S9",
"room": "A3", "room": "A3"
"time": [ },
"woensdag", "time": {
1130, "day": "woensdag",
1330 "start": 1130,
] "end": 1330
}
}, },
{ {
"online": "ZOOM", "online": "ZOOM",
"time": [ "time": {
"vrijdag", "day": "vrijdag",
1300, "start": 1300,
1530 "end": 1530
] }
} }
] ]
}, },
{ {
"course": "Wetenschappelijk Rekenen", "course": "Wetenschappelijk Rekenen",
"zoom": "https://ufora.ugent.be/d2l/ext/rp/236404/lti/framedlaunch/556e197e-e87b-4c27-be5d-53adc7a41826", "online_links": {
"zoom": "https://ufora.ugent.be/d2l/ext/rp/236404/lti/framedlaunch/556e197e-e87b-4c27-be5d-53adc7a41826"
},
"slots": [ "slots": [
{ {
"online": "ZOOM", "online": "ZOOM",
"time": [ "time": {
"dinsdag", "day": "dinsdag",
1130, "start": 1130,
1300 "end": 1300
] }
}, },
{ {
"online": "ZOOM", "online": "ZOOM",
"time": [ "time": {
"woensdag", "day": "woensdag",
1500, "start": 1500,
1800 "end": 1800
] }
}, },
{ {
"online": "ZOOM", "online": "ZOOM",
"time": [ "time": {
"donderdag", "day": "donderdag",
830, "start": 830,
1000 "end": 1000
] }
} }
] ]
}, },
{ {
"course": "Software Engineering Lab 1", "course": "Software Engineering Lab 1",
"online_links": {
"zoom": "https://ufora.ugent.be/d2l/ext/rp/235800/lti/framedlaunch/556e197e-e87b-4c27-be5d-53adc7a41826", "zoom": "https://ufora.ugent.be/d2l/ext/rp/235800/lti/framedlaunch/556e197e-e87b-4c27-be5d-53adc7a41826",
"msteams": "https://teams.microsoft.com/l/team/19%3a4dfd5b2fb1ae4aa9b72706aa3a0d6867%40thread.tacv2/conversations?groupId=256d5c58-5d53-43f5-9436-497b0c852c75&tenantId=d7811cde-ecef-496c-8f91-a1786241b99c", "msteams": "https://teams.microsoft.com/l/team/19%3a4dfd5b2fb1ae4aa9b72706aa3a0d6867%40thread.tacv2/conversations?groupId=256d5c58-5d53-43f5-9436-497b0c852c75&tenantId=d7811cde-ecef-496c-8f91-a1786241b99c"
},
"slots": [ "slots": [
{ {
"online": "MS Teams", "online": "MS Teams",
"time": [ "time": {
"dinsdag", "day": "dinsdag",
1430, "start": 1430,
1700 "end": 1700
] }
}, },
{ {
"online": "MS Teams", "online": "MS Teams",
"time": [ "time": {
"vrijdag", "day": "vrijdag",
830, "start": 830,
1130 "end": 1130
] }
} }
] ]
}, },
{ {
"course": "Webdevelopment", "course": "Webdevelopment",
"zoom": "https://ugent-be.zoom.us/j/93166767783?pwd=MWdvb1BnNnlPSnAyNk52QmRzdjcwdz09", "online_links": {
"zoom": "https://ugent-be.zoom.us/j/93166767783?pwd=MWdvb1BnNnlPSnAyNk52QmRzdjcwdz09"
},
"slots": [ "slots": [
{ {
"campus": "Sterre", "weeks": {
"building": "S9", "1": {
"room": "A3", "canceled": true
"time": [ }
"woensdag",
900,
1100
]
}, },
{ "location": {
"weeks": [
1
],
"canceled": true,
"campus": "Sterre", "campus": "Sterre",
"building": "S9", "building": "S9",
"room": "A3", "room": "A3"
"time": [ },
"woensdag", "time": {
900, "day": "woensdag",
1100 "start": 900,
] "end": 1100
}
}, },
{ {
"online": "ZOOM", "online": "ZOOM",
"time": [ "time": {
"donderdag", "day": "donderdag",
1000, "start": 1000,
1300 "end": 1300
] }
} }
] ]
} }

View File

@ -24,6 +24,7 @@ def find_target_date(arg: Optional[str]) -> datetime:
elif arg.lower() == "overmorgen": # Day after tomorrow's schedule elif arg.lower() == "overmorgen": # Day after tomorrow's schedule
day += timedelta(days=2) day += timedelta(days=2)
# TODO show a different embed when "(over)morgen" is requested & it lands on a weekend
# Don't land on a weekend # Don't land on a weekend
day = skip_weekends(day) day = skip_weekends(day)