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

Comment and refactor [TIMETABLE/SUBSTITUTIONS]

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