diff --git a/schoolapps/timetable/pdf.py b/schoolapps/timetable/pdf.py index 43076389d148648b8f43bf2723e90811da92f871..1169704e9dc4cdc059e7c42fd3cccbe71180a7d3 100644 --- a/schoolapps/timetable/pdf.py +++ b/schoolapps/timetable/pdf.py @@ -23,12 +23,33 @@ TEX_HEADER = """\\documentclass[11pt]{article} {\\bgroup\def\\ULthickness{1.5pt}} {}{} +\\usepackage[framemethod=tikz]{mdframed} +\\newmdenv[ + roundcorner=5pt, + backgroundcolor=green, + linecolor=green, + skipabove=0pt, + skipbelow=0pt, + leftmargin=0pt, + rightmargin=0pt +]{badges} + +\\usepackage{tcolorbox} +\\newtcbox{\\badge}{nobeforeafter,colframe=green,colback=green,boxrule=0.5pt,arc=4pt, + boxsep=0pt,left=5pt,right=5pt,top=5pt,bottom=5pt,tcbox raise base, + grow to left by=0pt, + grow to right by=-3pt, + enlarge top by=3pt, + enlarge bottom by=3pt,coltext=white} + + \\usepackage{helvet} %Helvetica als Standardschriftart \\renewcommand{\\familydefault}{\\sfdefault} %Helvetica als Standardschriftart \\definecolor{grey}{rgb}{0.95,0.95,0.95} \\definecolor{darkgrey}{rgb}{0.6,0.6,0.6} \\definecolor{white}{rgb}{1,1,1} +\\definecolor{green}{RGB}{76,175,80} \\pagestyle{fancy} %\\renewcommand{\\sectionmark}[1]{#1} @@ -36,99 +57,12 @@ TEX_HEADER = """\\documentclass[11pt]{article} \\lhead{\\includegraphics[width=5cm]{static/common/logo.png}} \\lfoot{Katharineum zu Lübeck} \\cfoot{\\thepage} -\\rfoot{Alle Angaben ohne Gewähr} +\\rfoot{\\small Umsetzung: © 2018 by Computer-AG} \\begin{document}""" TEX_FOOTER = '\end{document}' -TEX_DIR_PATH = 'latex' -TEACHER_TEX = 'latex/teacher.tex' -TEACHER_PDF = 'latex/teacher.pdf' -CLASS_TEX = 'latex/class.tex' -CLASS_PDF = 'latex/class.pdf' - - -class SubRow(object): - def __init__(self): - self.color = "black" - self.css_class = "black-text" - self.lesson = "" - self.classes = "" - self.teacher = "" - self.subject = "" - self.room = "" - self.text = "" - self.extra = "" - - -def generate_sub_table(subs): - sub_rows = [] - for sub in subs: - sub_row = SubRow() - - if sub.type == 1 or sub.type == 2: - sub_row.css_class = "green-text" - sub_row.color = "green" - elif sub.type == 3: - sub_row.css_class = "blue-text" - sub_row.color = "blue" - - if sub.type == 3: - 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 - - if sub.type == 1: - sub_row.teacher = "<s>{}</s>".format(sub.teacher_old.shortcode) - - elif sub.teacher_new and sub.teacher_old: - sub_row.teacher = "<s>{}</s> → <strong>{}</strong>".format(sub.teacher_old.shortcode, - sub.teacher_new.shortcode) - elif sub.teacher_new and not sub.teacher_old: - sub_row.teacher = "<strong>{}</strong>".format(sub.teacher_new.shortcode) - else: - sub_row.teacher = "<strong>{}</strong>".format(sub.teacher_old.shortcode) - - if sub.type == 3: - sub_row.subject = "Aufsicht" - elif sub.type == 1 or sub.type == 2: - sub_row.subject = "<s>{}</s>".format(sub.subject_old.shortcode) - elif sub.subject_new and sub.subject_old: - sub_row.subject = "<s>{}</s> → <strong>{}</strong>".format(sub.subject_old.shortcode, - sub.subject_new.shortcode) - elif sub.subject_new and not sub.subject_old: - sub_row.subject = "<strong>{}</strong>".format(sub.subject_new.shortcode) - else: - sub_row.subject = "<strong>{}</strong>".format(sub.subject_old.shortcode) - - if sub.type == 3: - sub_row.room = sub.corridor.name - elif sub.type == 1 or sub.type == 2: - pass - elif sub.room_new and sub.room_old: - sub_row.room = "<s>{}</s> → <strong>{}</strong>".format(sub.room_old.shortcode, sub.room_new.shortcode) - elif sub.room_new and not sub.room_old: - sub_row.room = sub.room_new.shortcode - else: - sub_row.room = sub.room_old.shortcode - - sub_row.text = sub.text - - if sub.type == 1: - sub_row.badge = "Schüler frei" - elif sub.type == 2: - sub_row.badge = "Lehrer frei" - - sub_row.extra = "{} {}".format(sub.id, sub.lesson_id) - - sub_rows.append(sub_row) - return sub_rows - - TEX_TABLE_HEADER_CLASS = """ \def\\arraystretch{1.5} \\begin{longtable}{|p{20mm}|p{10mm}|p{32mm}|p{25mm}|p{30mm}|p{35mm}|} @@ -163,23 +97,24 @@ def generate_pdf(tex, filename): return True -def replacer(str): - str = str.replace("<strong>", "\\textbf{") - str = str.replace("<s>", "\\sout{") - str = str.replace("</strong>", "}") - str = str.replace("</s>", "}") - str = str.replace("→", "$\\rightarrow$") - return str +def tex_replacer(s): + s = s.replace("<strong>", "\\textbf{") + s = s.replace("<s>", "\\sout{") + s = s.replace("</strong>", "}") + s = s.replace("</s>", "}") + s = s.replace("→", "$\\rightarrow$") + return s -def generate_class_pdf(subs, date): +def generate_class_tex(subs, date): tex_body = "" - # Dates + # Format dates status_date = formats.date_format(date, format="j. F Y, \\K\\W W ") current_date = formats.date_format(timezone.datetime.now(), format="j. F Y H:i") head_date = formats.date_format(date, format="l, j. F Y") + # Generate header with dates tex_body += TEX_HEADER_CLASS % (status_date, current_date, head_date) # Begin table @@ -187,17 +122,35 @@ def generate_class_pdf(subs, date): color_background = True for sub in subs: + # Color every second row in grey if color_background: tex_body += '\\rowcolor{grey}' + # Get color tag for row + color = "\color{%s}" % sub.color + + # Print classes + tex_body += color tex_body += '\\textbf{' + sub.classes + '} & ' + + # Print lesson number, teacher, subject and room for i in [sub.lesson, sub.teacher, sub.subject, sub.room]: - tex_body += replacer(i) + ' & ' + tex_body += color + tex_body += tex_replacer(i) + ' & ' + + # Print badge (Cancellation) + if sub.badge is not None: + tex_body += """\\large\\badge{%s}""" % sub.badge + + # Print notice and new line + tex_body += color + tex_body += "\\Large\\textit{%s}\\\\\\hline\n" % (sub.text or "") - tex_body += "\\textit{%s}\\\\\\hline\n" % (sub.text or "") + # Change background color_background = not color_background # End table tex_body += '\\end{longtable}' - texcontent = TEX_HEADER + tex_body + TEX_FOOTER - return texcontent + # Connect header, body and footer + tex_content = TEX_HEADER + tex_body + TEX_FOOTER + return tex_content diff --git a/schoolapps/timetable/views.py b/schoolapps/timetable/views.py index c1548f8fb3f7c84743541d623859526375149aa8..2db06b057eccfd2a25f7483a73509018216c11db 100755 --- a/schoolapps/timetable/views.py +++ b/schoolapps/timetable/views.py @@ -2,9 +2,9 @@ from django.contrib.auth.decorators import login_required from django.http import Http404 from django.shortcuts import render -from timetable.pdf import generate_sub_table, generate_class_pdf, generate_pdf +from timetable.pdf import generate_class_tex, generate_pdf from untisconnect.parse import * -from untisconnect.sub import get_substitutions_by_date, date_to_untis_date, untis_date_to_date +from untisconnect.sub import get_substitutions_by_date, date_to_untis_date, untis_date_to_date, generate_sub_table from django.utils import timezone try: @@ -74,10 +74,10 @@ def substitutions(request, year=None, day=None, month=None): subs = get_substitutions_by_date(date) sub_table = generate_sub_table(subs) - pdf = generate_class_pdf(sub_table, date) - print(pdf) + tex = generate_class_tex(sub_table, date) + print(tex) - generate_pdf(pdf, "class") + generate_pdf(tex, "class") for row in sub_table: print(row.lesson) diff --git a/schoolapps/untisconnect/sub.py b/schoolapps/untisconnect/sub.py index 60df9ae636965ebe914af8be966374fb8e37086f..cca5e03e645e85b3df9cdbd0bdd98aed8130b10f 100644 --- a/schoolapps/untisconnect/sub.py +++ b/schoolapps/untisconnect/sub.py @@ -22,6 +22,7 @@ TYPE_CANCELLATION = 1 TYPE_TEACHER_CANCELLATION = 2 TYPE_CORRIDOR = 3 + def parse_type_of_untis_flags(flags): type_ = TYPE_SUBSTITUTION if "E" in flags: @@ -129,6 +130,88 @@ def substitutions_sorter(sub): return sort_by +class SubRow(object): + def __init__(self): + self.color = "black" + self.css_class = "black-text" + self.lesson = "" + self.classes = "" + self.teacher = "" + self.subject = "" + self.room = "" + self.text = "" + self.extra = "" + + +def generate_sub_table(subs): + sub_rows = [] + for sub in subs: + sub_row = SubRow() + + sub_row.color = "black" + if sub.type == 1 or sub.type == 2: + sub_row.css_class = "green-text" + sub_row.color = "green" + elif sub.type == 3: + sub_row.css_class = "blue-text" + sub_row.color = "blue" + + if sub.type == 3: + 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 + + if sub.type == 1: + sub_row.teacher = "<s>{}</s>".format(sub.teacher_old.shortcode) + + elif sub.teacher_new and sub.teacher_old: + sub_row.teacher = "<s>{}</s> → <strong>{}</strong>".format(sub.teacher_old.shortcode, + sub.teacher_new.shortcode) + elif sub.teacher_new and not sub.teacher_old: + sub_row.teacher = "<strong>{}</strong>".format(sub.teacher_new.shortcode) + else: + sub_row.teacher = "<strong>{}</strong>".format(sub.teacher_old.shortcode) + + if sub.type == 3: + sub_row.subject = "Aufsicht" + elif sub.type == 1 or sub.type == 2: + sub_row.subject = "<s>{}</s>".format(sub.subject_old.shortcode) + elif sub.subject_new and sub.subject_old: + sub_row.subject = "<s>{}</s> → <strong>{}</strong>".format(sub.subject_old.shortcode, + sub.subject_new.shortcode) + elif sub.subject_new and not sub.subject_old: + sub_row.subject = "<strong>{}</strong>".format(sub.subject_new.shortcode) + else: + sub_row.subject = "<strong>{}</strong>".format(sub.subject_old.shortcode) + + if sub.type == 3: + sub_row.room = sub.corridor.name + elif sub.type == 1 or sub.type == 2: + pass + elif sub.room_new and sub.room_old: + sub_row.room = "<s>{}</s> → <strong>{}</strong>".format(sub.room_old.shortcode, sub.room_new.shortcode) + elif sub.room_new and not sub.room_old: + sub_row.room = sub.room_new.shortcode + else: + sub_row.room = sub.room_old.shortcode + + sub_row.text = sub.text + + sub_row.badge = None + if sub.type == 1: + sub_row.badge = "Schüler frei" + elif sub.type == 2: + sub_row.badge = "Lehrer frei" + + sub_row.extra = "{} {}".format(sub.id, sub.lesson_id) + + sub_rows.append(sub_row) + return sub_rows + + def get_substitutions_by_date(date): subs_raw = run_default_filter( run_using(models.Substitution.objects.filter(date=date_to_untis_date(date)).order_by("classids", "lesson")),