Skip to content
Snippets Groups Projects
Commit 439b33ae authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

- Merge dev into feature/dashboard

- Do some refactoring
parent bd6d5210
No related branches found
No related tags found
No related merge requests found
import datetime
from django.utils import timezone, formats
from schoolapps.settings import LONG_WEEK_DAYS
from untisconnect.api import TYPE_TEACHER, get_teacher_by_shortcode, TYPE_CLASS, get_class_by_name, get_all_teachers, \
get_all_classes, get_all_rooms, get_all_subjects
from userinformation import UserInformation
def get_name_for_next_week_day_from_today() -> str:
"""
Return the next week day as you would say it from today: "today", "tomorrow" or "<weekday>"
:return: Formatted date
"""
# Next weekday
next_weekday: timezone.datetime = get_next_weekday_with_time(timezone.now(), timezone.now().time())
if next_weekday.date() == timezone.now().date():
# Today
date_formatted = "heute"
elif next_weekday.date() == timezone.now().date() + timezone.timedelta(days=1):
# Tomorrow
date_formatted = "morgen"
else:
# Other weekday
date_formatted = LONG_WEEK_DAYS[next_weekday.isoweekday() - 1][0]
return date_formatted
def get_calendar_weeks(year=timezone.datetime.now().year):
weeks = []
# Get first day of year > first calendar week
first_day_of_year = timezone.datetime(year=year, month=1, day=1)
if first_day_of_year.isoweekday() != 1:
days_to_next_monday = 1 - first_day_of_year.isoweekday()
first_day_of_year += datetime.timedelta(days=days_to_next_monday)
# Go for all weeks in year and create week dict
first_day_of_week = first_day_of_year
for i in range(52):
calendar_week = i + 1
last_day_of_week = first_day_of_week + datetime.timedelta(days=4)
weeks.append({
"calendar_week": calendar_week,
"first_day": first_day_of_week,
"last_day": last_day_of_week
})
first_day_of_week += datetime.timedelta(weeks=1)
return weeks
def find_out_what_is_today(year=None, month=None, day=None):
date = timezone.datetime.now()
time = datetime.datetime.now().time()
if year is not None and day is not None and month is not None:
date = timezone.datetime(year=year, month=month, day=day)
if date != timezone.datetime.now():
time = datetime.time(0)
return date, time
def get_calendar_week(calendar_week, year=timezone.datetime.now().year):
weeks = get_calendar_weeks(year=year)
for week in weeks:
if week["calendar_week"] == calendar_week:
return week
return None
def get_next_weekday(date=None):
"""Get the next weekday by a datetime object"""
if date is None:
date = timezone.now().date()
if date.isoweekday() in {6, 7}:
if date.isoweekday() == 6:
plus = 2
else:
plus = 1
date += datetime.timedelta(days=plus)
return date
def get_next_weekday_with_time(date=None, time=None) -> datetime.datetime:
"""Get the next weekday by a datetime object"""
if date is None:
date = timezone.now().date()
if time is None:
time = timezone.now().time()
if time > datetime.time(15, 35):
date += datetime.timedelta(days=1)
if date.isoweekday() in {6, 7}:
if date.isoweekday() == 6:
plus = 2
else:
plus = 1
date += datetime.timedelta(days=plus)
return date
def calendar_week(date: datetime) -> int:
return date.isocalendar()[1]
def weekday(date: datetime) -> int:
return date.isoweekday() - 1
def format_lesson_time(time: datetime) -> str:
return formats.date_format(time, "H:i")
......@@ -112,8 +112,10 @@ from .api import *
from .api_helper import untis_split_third
from .drive import drive
drive = drive
def parse():
global drive
lessons = []
......
......@@ -5,6 +5,7 @@ from django.utils import timezone
from schoolapps import settings
from schoolapps.settings import LESSONS
from untisconnect.api import format_classes, TYPE_CLASS, TYPE_TEACHER, TYPE_ROOM
from untisconnect.datetimeutils import format_lesson_time
from untisconnect.events import get_all_events_by_date
from untisconnect.api import format_classes, get_today_holidays
from untisconnect.parse import parse
......@@ -55,7 +56,9 @@ def parse_lesson_times():
"number": i + 1,
"number_format": t[1],
"start": start_time,
"start_format": format_lesson_time(start_time),
"end": end_time,
"end_format": format_lesson_time(end_time)
})
return times
......@@ -169,7 +172,6 @@ def get_plan(type, id, smart=False, monday_of_week=None):
element_container.is_hol = True
element_container.element.holiday_reason = hols_for_weekday[time.day - 1][0].name
if type != TYPE_ROOM or i == room_index:
# Add this container object to the LessonContainer object in the plan array
plan[time.hour - 1][0][time.day - 1].append(element_container)
......
from untisconnect.api import TYPE_TEACHER, get_teacher_by_shortcode, TYPE_CLASS, get_class_by_name, get_all_teachers, \
get_all_classes, get_all_rooms, get_all_subjects
from untisconnect.datetimeutils import get_calendar_week, calendar_week, weekday
from untisconnect.plan import get_plan
from userinformation import UserInformation
def get_type_and_object_of_user(user):
_type = UserInformation.user_type(user)
if _type == UserInformation.TEACHER:
# Teacher
_type = TYPE_TEACHER
shortcode = user.username
el = get_teacher_by_shortcode(shortcode)
elif _type == UserInformation.STUDENT:
# Student
_type = TYPE_CLASS
_name = UserInformation.user_classes(user)[0]
el = get_class_by_name(_name)
else:
# Nothing of both
return None, None
return _type, el
def overview_dict():
return {
'teachers': get_all_teachers(),
'classes': get_all_classes(),
'rooms': get_all_rooms(),
'subjects': get_all_subjects()
}
def get_plan_for_day(_type, plan_id, date):
# Get calendar week and monday of week
monday_of_week = get_calendar_week(calendar_week(date), date.year)["first_day"]
week_day = weekday(date)
# Get plan
plan, holidays = get_plan(_type, plan_id, smart=True, monday_of_week=monday_of_week)
lessons = [(row[week_day], time) for row, time in plan]
return lessons
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment