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

Apply merging of sub rows for HTML table, too | Refactor and comment | Add...

Apply merging of sub rows for HTML table, too | Refactor and comment | Add __eq__ to API objects to improve matching
parent 52614036
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,6 @@ from django.template.loader import render_to_string
from schoolapps.settings import BASE_DIR
from debug.models import register_log_with_filename
LOGO_FILENAME = os.path.join(BASE_DIR, "static", "common", "logo.png")
......@@ -31,58 +30,9 @@ def generate_pdf(tex, filename):
register_log_with_filename("latex_{}".format(filename), "latex", "{}.log".format(filename), process.returncode)
def equal(sub1, sub2):
if sub1.classes == sub2.classes and sub1.sub and sub2.sub and \
sub1.sub.teacher_old == sub2.sub.teacher_old and \
sub1.sub.teacher_new == sub2.sub.teacher_new and \
sub1.sub.subject_old == sub2.sub.subject_old and \
sub1.sub.subject_new == sub2.sub.subject_new and \
sub1.sub.room_old == sub2.sub.room_old and \
sub1.sub.room_new == sub2.sub.room_new and \
sub1.sub.text == sub2.sub.text:
print("Treffer:", sub1.classes, '=', sub2.classes, '\n',
sub1.sub.teacher_old, '=', sub2.sub.teacher_old, '\n',
sub1.sub.teacher_new, '=', sub2.sub.teacher_old, '\n',
sub1.sub.subject_old, '=', sub2.sub.subject_old, '\n',
sub1.sub.subject_new, '=', sub2.sub.subject_new, '\n',
sub1.sub.room_old, '=', sub2.sub.room_new, '\n',
sub1.sub.text, '=', sub2.sub.text)
return True
def merge_subs(subs):
new_subs = []
i = 0
print("Länge:",len(subs))
while i < len(subs) - 1:
j = 1
# Hier geht's schon los: Warum ist sub.teacher_old der gesuchte String, aber sub.subject_old ist ein Objekt?
# print('Komplett:',subs[i].classes, subs[i].sub.teacher_old.shortcode, subs[i].sub.subject_old, subs[i].sub.room_old, subs[i].sub.text)
# sub = inspect.getmembers(subs[i])
# for s in sub:
# print('sub:',len(s), s[0], s[1])
while equal(subs[i], subs[i + j]):
j += 1
if i + j > len(subs) - 1:
break
if j > 1:
new_sub = subs[i]
new_sub.lesson = subs[i].lesson + '-' + subs[i + j - 1].lesson
print('lesson',new_sub.lesson)
new_subs.append(new_sub)
else:
new_subs.append(subs[i])
# get last item
if i == len(subs) - 2:
new_subs.append(subs[i+1])
break
i += j
return(new_subs)
def generate_class_tex(subs, date, header_info, hints=None):
"""Generate LaTeX for a PDF by a substitution table"""
subs = merge_subs(subs)
context = {
"subs": subs,
"date": date,
......
import datetime
import os
import time
import traceback
from typing import List
from PyPDF2 import PdfFileMerger
from django.contrib.auth.decorators import login_required, permission_required
......@@ -17,7 +19,7 @@ from timetable.hints import get_all_hints_by_time_period, get_all_hints_by_class
from timetable.pdf import generate_class_tex, generate_pdf
from untisconnect.plan import get_plan, TYPE_TEACHER, TYPE_CLASS, TYPE_ROOM, parse_lesson_times
from untisconnect.sub import get_substitutions_by_date, generate_sub_table, get_header_information
from untisconnect.sub import get_substitutions_by_date, generate_sub_table, get_header_information, SubRow
from untisconnect.api import *
from untisconnect.events import get_all_events_by_date
from userinformation import UserInformation
......@@ -302,6 +304,55 @@ def get_next_weekday_with_time(date, time):
# SUBSTITUTIONS #
#################
# TODO: Move to own helper file later
def equal(sub_row_1: SubRow, sub_row_2: SubRow) -> bool:
"""
Checks the equality of two sub rows
:param sub_row_1: SubRow 1
:param sub_row_2: SubRow 2
:return: Equality
"""
return sub_row_1.classes == sub_row_2.classes and sub_row_1.sub and sub_row_2.sub and \
sub_row_1.sub.teacher_old == sub_row_2.sub.teacher_old and \
sub_row_1.sub.teacher_new == sub_row_2.sub.teacher_new and \
sub_row_1.sub.subject_old == sub_row_2.sub.subject_old and \
sub_row_1.sub.subject_new == sub_row_2.sub.subject_new and \
sub_row_1.sub.room_old == sub_row_2.sub.room_old and \
sub_row_1.sub.room_new == sub_row_2.sub.room_new and \
sub_row_1.sub.text == sub_row_2.sub.text
def merge_sub_rows(sub_table: List[SubRow]) -> List[SubRow]:
"""
Merge equal sub rows with different lesson numbers to one
:param sub_table:
:return:
"""
new_sub_table = []
i = 0
while i < len(sub_table) - 1:
j = 1
while equal(sub_table[i], sub_table[i + j]):
j += 1
if i + j > len(sub_table) - 1:
break
if j > 1:
new_sub_row = sub_table[i]
new_sub_row.lesson = sub_table[i].lesson + '-' + sub_table[i + j - 1].lesson
new_sub_table.append(new_sub_row)
else:
new_sub_table.append(sub_table[i])
# get last item
if i == len(sub_table) - 2:
new_sub_table.append(sub_table[i + 1])
break
i += j
return new_sub_table
def sub_pdf(request, plan_date=None):
"""Show substitutions as PDF for the next weekday (specially for monitors)"""
......@@ -325,6 +376,7 @@ def sub_pdf(request, plan_date=None):
subs = get_substitutions_by_date(date)
sub_table = generate_sub_table(subs, events)
sub_table = merge_sub_rows(sub_table)
# Get header information and hints
header_info = get_header_information(subs, date, events)
......@@ -385,6 +437,9 @@ def substitutions(request, year=None, month=None, day=None):
sub_table = generate_sub_table(subs, events)
# Merge Subs
sub_table = merge_sub_rows(sub_table)
# Get header information and hints
header_info = get_header_information(subs, date, events)
hints = list(get_all_hints_by_time_period(date, date))
......
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