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

Comment and refactor [TIMETABLE/SUBSTITUTIONS]

parent 571ff193
No related branches found
No related tags found
1 merge request!86Merge school-apps
......@@ -4,6 +4,8 @@ import subprocess
from django.utils import timezone
from django.utils import formats
# LaTeX constants
TEX_HEADER = """\\documentclass[11pt]{article}
\\usepackage[ngerman]{babel}
\\usepackage[utf8]{inputenc}
......@@ -86,28 +88,38 @@ TEX_HEADER_CLASS = """
def generate_pdf(tex, filename):
"""Generate a PDF by LaTeX code"""
# Read LaTeX file
tex_file = open(os.path.join("latex", filename + ".tex"), "w")
tex_file.write(tex)
tex_file.close()
# Execute pdflatex to generate the PDF
bash_command = "pdflatex -output-directory latex {}.tex".format(filename)
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
# bash_command = "xreader {}.pdf".format(filename)
# process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
return True
def tex_replacer(s):
"""Replace HTML tags by LaTeX tags"""
# Strong text
s = s.replace("<strong>", "\\textbf{")
s = s.replace("<s>", "\\sout{")
s = s.replace("</strong>", "}")
# Struck out text
s = s.replace("<s>", "\\sout{")
s = s.replace("</s>", "}")
# Arrow
s = s.replace("", "$\\rightarrow$")
return s
def generate_class_tex(subs, date):
"""Generate LaTeX for a PDF by a substitution table"""
tex_body = ""
# Format dates
......
......@@ -68,6 +68,8 @@ def plan(request, plan_type, plan_id):
def get_next_weekday(date):
"""Get the next weekday by a datetime object"""
if date.isoweekday() in {6, 7}:
date += datetime.timedelta(days=date.isoweekday() % 4)
return date
......@@ -75,38 +77,38 @@ def get_next_weekday(date):
@login_required
def sub_pdf(request):
"""Show substitutions as PDF for the next weekday (specially for monitors)"""
# Get the next weekday
today = timezone.datetime.now()
first_day = get_next_weekday(today)
# Get subs and generate table
subs = get_substitutions_by_date(first_day)
sub_table = generate_sub_table(subs)
# Generate LaTeX
tex = generate_class_tex(sub_table, first_day)
print(first_day)
print(tex)
# Generate PDF
generate_pdf(tex, "class")
# Read and response PDF
file = open(os.path.join("latex", "class.pdf"), "rb")
return FileResponse(file, content_type="application/pdf")
@login_required
def substitutions(request, year=None, day=None, month=None):
"""Show substitutions in a classic view"""
date = timezone.datetime.now()
if year is not None and day is not None and month is not None:
date = timezone.datetime(year=year, month=month, day=day)
print(date)
# Get subs and generate table
subs = get_substitutions_by_date(date)
sub_table = generate_sub_table(subs)
tex = generate_class_tex(sub_table, date)
print(tex)
generate_pdf(tex, "class")
for row in sub_table:
print(row.lesson)
print(row.teacher)
context = {
"subs": 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