Skip to content
Snippets Groups Projects
Commit 92b5aa4d authored by Julian's avatar Julian
Browse files

Merge remote-tracking branch 'origin/feature/events' into feature/events2

parents 23cc1321 ce207a54
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,10 @@ from untisconnect.api_helper import get_term_by_id, run_using, untis_date_to_dat
from . import models
from timetable.settings import untis_settings
TYPE_TEACHER = 0
TYPE_ROOM = 1
TYPE_CLASS = 2
def run_all(obj, filter_term=True):
return run_default_filter(run_using(obj).all(), filter_term=filter_term)
......@@ -158,10 +162,10 @@ def format_classes(classes):
else:
classes_as_dict[step].append(part)
out = ""
out = []
for key, value in classes_as_dict.items():
out += key + "".join(value)
return out
out.append(key + "".join(value))
return ", ".join(out)
########
......@@ -276,6 +280,8 @@ class Absence(object):
def __init__(self):
self.filled = None
self.teacher = None
self.room = None
self.type = TYPE_TEACHER
self.from_date = None
self.to_date = None
self.from_lesson = None
......@@ -284,8 +290,14 @@ class Absence(object):
def create(self, db_obj):
self.filled = True
print(db_obj.ida)
self.teacher = get_teacher_by_id(db_obj.ida)
# print(db_obj.ida)
print(db_obj.typea)
self.type = TYPE_TEACHER if db_obj.typea != 102 else TYPE_ROOM
if self.type == TYPE_TEACHER:
self.teacher = get_teacher_by_id(db_obj.ida)
else:
self.room = get_room_by_id(db_obj.ida)
self.from_date = untis_date_to_date(db_obj.datefrom)
self.to_date = untis_date_to_date(db_obj.dateto)
self.from_lesson = db_obj.lessonfrom
......
from .api import *
def build_drive():
odrive = {
"teachers": get_all_teachers(),
"rooms": get_all_rooms(),
"classes": get_all_classes(),
"subjects": get_all_subjects(),
"corridors": get_all_corridors(),
}
drive = {}
for key, value in odrive.items():
drive[key] = {}
for el in value:
id = el.id
drive[key][id] = el
return drive
drive = build_drive()
\ No newline at end of file
from django.conf import settings
from .drive import drive
from .api_helper import untis_date_to_date, date_to_untis_date
from .api import row_by_row_helper, run_all
from . import models
#########
# EVENT #
#########
class Event(object):
def __init__(self):
self.filled = None
self.text = None
self.teachers = []
self.classes = []
self.rooms = []
self.absences = []
self.from_date = None
self.to_date = None
self.from_lesson = None
self.to_lesson = None
self.is_whole_day = None
def create(self, db_obj):
"""0~0~19~0~1859~0,0~0~65~0~1860~0,0~0~21~0~1861~0,0~0~3~0~1862~0"""
self.filled = True
event_parsed = db_obj.eventelement1.split(",")
elements = []
for element in event_parsed:
elements.append(element.split("~"))
for element in elements:
if element[0] != "0" and element[0] != "":
class_id = int(element[0])
obj = drive["classes"][class_id]
self.classes.append(obj)
if element[2] != "0" and element[2] != "":
teacher_id = int(element[2])
obj = drive["teachers"][teacher_id]
self.teachers.append(obj)
if element[3] != "0" and element[3] != "":
room_id = int(element[3])
obj = drive["rooms"][room_id]
self.rooms.append(obj)
if element[4] != "0" and element[4] != "":
absence = models.Absence.objects.using("untis").get(absence_id=int(element[4]))
self.absences.append(absence)
self.text = db_obj.text
self.from_date = untis_date_to_date(db_obj.datefrom)
self.to_date = untis_date_to_date(db_obj.dateto)
self.from_lesson = db_obj.lessonfrom
self.to_lesson = db_obj.lessonto
self.is_whole_day = self.from_lesson == 1 and self.to_lesson >= settings.TIMETABLE_HEIGHT
def get_all_events_by_date(date):
d_i = int(date_to_untis_date(date))
db_rows = run_all(models.Event.objects.filter(dateto__gte=d_i, datefrom__lte=d_i, deleted=0), filter_term=False)
return row_by_row_helper(db_rows, Event)
......@@ -111,28 +111,8 @@ class LessonTime(object):
from .api import *
from .api_helper import untis_split_third
def build_drive():
odrive = {
"teachers": get_all_teachers(),
"rooms": get_all_rooms(),
"classes": get_all_classes(),
"subjects": get_all_subjects(),
"corridors": get_all_corridors(),
}
drive = {}
for key, value in odrive.items():
drive[key] = {}
for el in value:
id = el.id
drive[key][id] = el
return drive
drive = build_drive()
from .drive import drive
drive = drive
def parse():
global drive
......@@ -172,25 +152,25 @@ def get_lesson_element_by_id_and_teacher(lesson_id, teacher, hour=None, weekday=
return None, None
el = None
i = 0
# print(lesson.elements)
for i, element in enumerate(lesson.elements):
# print(element.teacher.shortcode)
if element.teacher.id == teacher.id:
el = element
break
if teacher is not None:
for i, element in enumerate(lesson.elements):
if element.teacher is not None:
if element.teacher.id == teacher.id:
el = element
break
elif len(lesson.elements) > 0:
el = lesson.elements[0]
else:
el = None
t = None
# print(lesson.times)
# print(weekday)
# print(hour)
for time in lesson.times:
# print("DAY", time.day, time.hour)
if time.day == weekday and time.hour == hour:
t = time
# print(t)
room = None
if t is not None and len(t.rooms) > i:
# print(t.rooms)
# print(len(t.rooms))
room = t.rooms[i]
if el is not None:
......
......@@ -4,14 +4,10 @@ from django.utils import timezone
from schoolapps import settings
from schoolapps.settings import LESSONS
from untisconnect.api import format_classes
from untisconnect.api import format_classes, TYPE_CLASS, TYPE_TEACHER, TYPE_ROOM
from untisconnect.parse import parse
from untisconnect.sub import get_substitutions_by_date_as_dict, TYPE_CANCELLATION
TYPE_TEACHER = 0
TYPE_ROOM = 1
TYPE_CLASS = 2
class LessonContainer(object):
"""
......@@ -184,6 +180,10 @@ def get_plan(type, id, smart=False, monday_of_week=None):
if sub["sub"].teacher_new.id == id:
found = True
if sub["sub"].teacher_old:
if sub["sub"].teacher_old.id == id:
found = True
elif type == TYPE_ROOM:
if sub["sub"].room_new:
if sub["sub"].room_new.id == id:
......
from django.utils import timezone
from untisconnect import models
from untisconnect.api import run_default_filter, row_by_row_helper, format_classes, get_all_absences_by_date
from untisconnect.api import run_default_filter, row_by_row_helper, format_classes, get_all_absences_by_date, \
TYPE_TEACHER
from untisconnect.api_helper import run_using, untis_split_first, untis_date_to_date, date_to_untis_date
from untisconnect.parse import get_lesson_element_by_id_and_teacher, build_drive
from untisconnect.parse import get_lesson_element_by_id_and_teacher
from untisconnect.drive import build_drive
TYPE_SUBSTITUTION = 0
TYPE_CANCELLATION = 1
......@@ -12,6 +14,12 @@ TYPE_CORRIDOR = 3
def parse_type_of_untis_flags(flags):
"""
Get type of substitution by parsing UNTIS flags
:param flags: UNTIS flags (string)
:return: type (int, constants are provided)
"""
type_ = TYPE_SUBSTITUTION
if "E" in flags:
type_ = TYPE_CANCELLATION
......@@ -20,6 +28,7 @@ def parse_type_of_untis_flags(flags):
return type_
# Build cache
drive = build_drive()
......@@ -51,6 +60,8 @@ class Substitution(object):
def create(self, db_obj):
self.filled = True
# Metadata
self.id = db_obj.substitution_id
self.lesson_id = db_obj.lesson_idsubst
self.date = untis_date_to_date(db_obj.date)
......@@ -58,13 +69,11 @@ class Substitution(object):
self.type = parse_type_of_untis_flags(db_obj.flags)
self.text = db_obj.text
# Lesson
# Teacher
# print(db_obj.teacher_idlessn)
if db_obj.teacher_idlessn != 0:
self.teacher_old = drive["teachers"][db_obj.teacher_idlessn]
# print(self.teacher_new, self.teacher_old, self.lesson_id, self.id)
if db_obj.teacher_idsubst != 0:
self.teacher_new = drive["teachers"][db_obj.teacher_idsubst]
......@@ -75,10 +84,11 @@ class Substitution(object):
self.teacher_old = self.teacher_new
self.teacher_new = None
# print(self.teacher_old, self.teacher_new)
self.lesson_element, self.room_old = get_lesson_element_by_id_and_teacher(self.lesson_id, self.teacher_old,
self.lesson, self.date.weekday() + 1)
# print(self.lesson)
# print(self.room_old)
# Subject
self.subject_old = self.lesson_element.subject if self.lesson_element is not None else None
if db_obj.subject_idsubst != 0:
......@@ -88,37 +98,34 @@ class Substitution(object):
self.subject_new = None
# Room
# self.rooms_old = self.lesson_element.rooms if self.lesson_element is not None else []
# if len(self.rooms_old) >= 1:
# self.room_old = self.rooms_old[0]
if db_obj.room_idsubst != 0:
self.room_new = drive["rooms"][db_obj.room_idsubst]
if self.room_old is not None and self.room_old.id == self.room_new.id:
self.room_new = None
# if self.rooms_old
# print(self.room_new)
# print("CORRIDOR")
# print(self.corridor)
# Supervisement
if db_obj.corridor_id != 0:
self.corridor = drive["corridors"][db_obj.corridor_id]
self.type = TYPE_CORRIDOR
# Classes
# Classes
self.classes = []
class_ids = untis_split_first(db_obj.classids, conv=int)
# print(class_ids)
for id in class_ids:
self.classes.append(drive["classes"][id])
# print(self.classes)
def substitutions_sorter(sub):
"""
Sorting helper (sort function) for substitutions
:param sub: Substitution to sort
:return: A string for sorting by
"""
# First, sort by class
sort_by = "".join(class_.name for class_ in sub.classes)
sort_by = sub.classes
# If the sub hasn't got a class, then put it to the bottom
if sort_by == "":
......@@ -132,81 +139,38 @@ def substitutions_sorter(sub):
class SubRow(object):
def __init__(self):
self.sub = None
self.color = "black"
self.css_class = "black-text"
self.lesson = ""
self.classes = ""
self.teacher = ""
self.teacher_full = ""
self.teachers = [] # Only for events
self.rooms = [] # Only for events
self.absences = [] # Only for events
self.subject = ""
self.subject_full = ""
self.room = ""
self.room_full = ""
self.text = ""
self.extra = ""
self.is_event = False
def generate_teacher_row(sub, full=False):
# print(sub.id)
teacher = ""
if sub.type == 1:
teacher = "<s>{}</s>".format(sub.teacher_old.shortcode if not full else sub.teacher_old.name)
elif sub.teacher_new and sub.teacher_old:
teacher = "<s>{}</s> → <strong>{}</strong>".format(
sub.teacher_old.shortcode if not full else sub.teacher_old.name,
sub.teacher_new.shortcode if not full else sub.teacher_new.name)
elif sub.teacher_new and not sub.teacher_old:
teacher = "<strong>{}</strong>".format(sub.teacher_new.shortcode if not full else sub.teacher_new.name)
elif sub.teacher_old:
teacher = "<strong>{}</strong>".format(sub.teacher_old.shortcode if not full else sub.teacher_old.name)
return teacher
def generate_subject_row(sub, full=False):
if sub.type == 3:
subject = "Aufsicht"
elif not sub.subject_new and not sub.subject_old:
subject = ""
elif sub.type == 1 or sub.type == 2:
subject = "<s>{}</s>".format(sub.subject_old.shortcode if not full else sub.subject_old.name)
elif sub.subject_new and sub.subject_old:
subject = "<s>{}</s> → <strong>{}</strong>".format(
sub.subject_old.shortcode if not full else sub.subject_old.name,
sub.subject_new.shortcode if not full else sub.subject_new.name)
elif sub.subject_new and not sub.subject_old:
subject = "<strong>{}</strong>".format(sub.subject_new.shortcode if not full else sub.subject_new.name)
else:
subject = "<strong>{}</strong>".format(sub.subject_old.shortcode if not full else sub.subject_old.name)
return subject
def generate_room_row(sub, full=False):
room = ""
if sub.type == 3:
room = sub.corridor.name
elif sub.type == 1 or sub.type == 2:
pass
elif sub.room_new and sub.room_old:
room = "<s>{}</s> → <strong>{}</strong>".format(sub.room_old.shortcode if not full else sub.room_old.name,
sub.room_new.shortcode if not full else sub.room_new.name)
elif sub.room_new and not sub.room_old:
room = sub.room_new.shortcode if not full else sub.room_new.name
elif not sub.room_new and not sub.room_old:
pass
else:
room = sub.room_old.shortcode if not full else sub.room_old.name
return room
def generate_sub_table(subs):
def generate_sub_table(subs, events=[]):
"""
Parse substitutions and prepare than for displaying in plan
:param subs: Substitutions to parse
:return: A list of SubRow objects
"""
sub_rows = []
for sub in subs:
sub_row = SubRow()
sub_row.sub = sub
# Color
sub_row.color = "black"
if sub.type == 1 or sub.type == 2:
sub_row.css_class = "green-text"
......@@ -215,40 +179,49 @@ def generate_sub_table(subs):
sub_row.css_class = "blue-text"
sub_row.color = "blue"
# Format lesson
if sub.type == 3:
sub_row.lesson = "{}./{}".format(sub.lesson - 1, sub.lesson)
sub_row.lesson = "{}./{}.".format(sub.lesson - 1, sub.lesson)
else:
sub_row.lesson = "{}.".format(sub.lesson)
# for class_ in sub.classes:
# sub_row.classes += class_.name
# Classes
sub_row.classes = format_classes(sub.classes)
sub_row.teacher = generate_teacher_row(sub)
sub_row.teacher_full = generate_teacher_row(sub, full=True)
sub_row.subject = generate_subject_row(sub)
sub_row.subject_full = generate_subject_row(sub, full=True)
sub_row.room = generate_room_row(sub)
sub_row.room_full = generate_room_row(sub, full=True)
# if DEBUG:
# # Add id only if debug mode is on
# if sub.text:
# sub_row.text = sub.text + " " + str(sub.id)
# else:
# sub_row.text = str(sub.id)
# else:
# Hint text
sub_row.text = sub.text
# Badge
sub_row.badge = None
if sub.type == 1:
sub_row.badge = "Schüler frei"
elif sub.type == 2:
sub_row.badge = "Lehrer frei"
# Debugging information
sub_row.extra = "{} {}".format(sub.id, sub.lesson_id)
sub_rows.append(sub_row)
for event in events:
sub_row = SubRow()
sub_row.is_event = True
if event.from_lesson != event.to_lesson:
sub_row.lesson = "{}.-{}.".format(event.from_lesson, event.to_lesson)
sub_row.classes = format_classes(event.classes)
sub_row.teachers = event.teachers
sub_row.rooms = event.rooms
sub_row.absences = event.absences
sub_row.color = "purple"
sub_row.text = event.text
sub_rows.append(sub_row)
sub_rows.sort(key=substitutions_sorter)
return sub_rows
......@@ -303,6 +276,8 @@ def get_header_information(subs, date):
if info.absences:
elements = []
for absence in info.absences:
if absence.type != TYPE_TEACHER:
continue
if absence.is_whole_day:
# Teacher is missing the whole day
elements.append("{}".format(absence.teacher.shortcode))
......@@ -323,7 +298,7 @@ def get_substitutions_by_date(date):
filter_term=False)
subs = row_by_row_helper(subs_raw, Substitution)
subs.sort(key=substitutions_sorter)
# subs.sort(key=substitutions_sorter)
return subs
......
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