From 4ac21f6678c4e1683c4831a724ea3cb5f7deec9b Mon Sep 17 00:00:00 2001 From: Tom Teichler <tom.teichler@teckids.org> Date: Sun, 19 Jan 2020 22:01:47 +0100 Subject: [PATCH] Move utils related to schoolapps to extra file. --- aleksis/apps/untis/schoolapps_util.py | 53 +++++++++++++++++++++++++++ aleksis/apps/untis/util.py | 48 ------------------------ 2 files changed, 53 insertions(+), 48 deletions(-) create mode 100644 aleksis/apps/untis/schoolapps_util.py diff --git a/aleksis/apps/untis/schoolapps_util.py b/aleksis/apps/untis/schoolapps_util.py new file mode 100644 index 0000000..4895a34 --- /dev/null +++ b/aleksis/apps/untis/schoolapps_util.py @@ -0,0 +1,53 @@ +from datetime import date, time, timedelta +from typing import BinaryIO, Optional, Union +from xml.dom import Node, minidom + +from django.http import HttpRequest +from django.utils.translation import ugettext as _ + +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] + + holidays_for_date = holidays[week_day] + return lessons, holidays_for_date diff --git a/aleksis/apps/untis/util.py b/aleksis/apps/untis/util.py index 3b61df0..16086c7 100644 --- a/aleksis/apps/untis/util.py +++ b/aleksis/apps/untis/util.py @@ -10,13 +10,6 @@ from aleksis.core.models import Group, Person from aleksis.core.util import messages -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_child_node_text(node: Node, tag: str) -> Optional[str]: tag_nodes = node.getElementsByTagName(tag) @@ -172,44 +165,3 @@ def untis_import_xml(request: HttpRequest, untis_xml: Union[BinaryIO, str]) -> N lesson.save() - -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] - - holidays_for_date = holidays[week_day] - return lessons, holidays_for_date -- GitLab