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

- Create LaTeX template for substitutions PDF with django template engine

- Refactor a big part of PDF generation to new template (Missing: teacher, subject, room)
- Add template tag for setting variables dynamic in templates
parent fc07837d
No related branches found
No related tags found
1 merge request!86Merge school-apps
......@@ -25,6 +25,14 @@
<orderEntry type="library" name="jquery" level="application" />
</component>
<component name="TemplatesService">
<option name="templateFileTypes">
<list>
<option value="HTML" />
<option value="XHTML" />
<option value="XML" />
<option value="LaTeX source file" />
</list>
</option>
<option name="TEMPLATE_CONFIGURATION" value="Django" />
<option name="TEMPLATE_FOLDERS">
<list>
......
import os
import subprocess
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils import formats
......@@ -144,60 +145,66 @@ def generate_class_tex(subs, date, header_info):
tex_body = ""
# 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")
# 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)
if header_info.is_box_needed():
tex_body += TEX_HEADER_BOX_START
for row in header_info.rows:
tex_body += TEX_HEADER_BOX_ROW_A % row[0]
tex_body += TEX_HEADER_BOX_MIDDLE
for row in header_info.rows:
tex_body += TEX_HEADER_BOX_ROW_B % row[1]
tex_body += TEX_HEADER_BOX_END
# tex_body += TEX_HEADER_CLASS % (status_date, current_date, head_date)
# if header_info.is_box_needed():
# tex_body += TEX_HEADER_BOX_START
# for row in header_info.rows:
# tex_body += TEX_HEADER_BOX_ROW_A % row[0]
# tex_body += TEX_HEADER_BOX_MIDDLE
# for row in header_info.rows:
# tex_body += TEX_HEADER_BOX_ROW_B % row[1]
# tex_body += TEX_HEADER_BOX_END
# Begin table
tex_body += TEX_TABLE_HEADER_CLASS
# tex_body += TEX_TABLE_HEADER_CLASS
color_background = True
last_classes = ""
# color_background = True
# last_classes = ""
for sub in subs:
# Color groups of classes in grey/white
if last_classes != sub.classes:
color_background = not color_background
# if last_classes != sub.classes:
# color_background = not color_background
#
# last_classes = sub.classes
last_classes = sub.classes
if color_background:
tex_body += '\\rowcolor{grey}'
# if color_background:
# tex_body += '\\rowcolor{grey}'
# Get color tag for row
color = "\color{%s}" % sub.color
# color = "\color{%s}" % sub.color
# Print classes
# print(sub.classes)
tex_body += color
tex_body += '\\textbf{' + sub.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 += color
# tex_body += color
tex_body += tex_replacer(i) + ' & '
# Print badge (Cancellation)
if sub.badge is not None:
tex_body += """\\large\\badge{%s}""" % sub.badge
# 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}\\\\\n" % (sub.text or "")
# tex_body += color
# tex_body += "\\Large\\textit{%s}\\\\\n" % (sub.text or "")
# End table
tex_body += '\\end{longtable}'
# tex_body += '\\end{longtable}'
# Connect header, body and footer
tex_content = TEX_HEADER + tex_body + TEX_FOOTER
return tex_content
# tex_content = TEX_HEADER + tex_body + TEX_FOOTER
# return tex_content
context = {
"subs": subs,
"date": date,
"header_info": header_info
}
return render_to_string("timetable/latex/substitutions.tex", context)
{% load common %}
\documentclass[11pt]{article}
\usepackage[ngerman]{babel}
\usepackage[sfdefault]{cabin}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,left=1cm,right=1cm,top=2cm,bottom=2cm,bindingoffset=0mm]{geometry}
% Packages
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{color, colortbl}
\usepackage{ulem, xpatch}
\xpatchcmd{\sout}
{\bgroup}
{\bgroup\def\ULthickness{1.5pt}}
{}{}
% Badge box
\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}
% Define colors
\definecolor{grey}{RGB}{208, 208, 208}
\definecolor{darkgrey}{rgb}{0.6,0.6,0.6}
\definecolor{white}{rgb}{1,1,1}
\definecolor{ green }{RGB}{76,175,80}
\definecolor{green}{RGB}{76,175,80}
\definecolor{ black }{RGB}{0,0,0}
% Define header
\pagestyle{fancy}
% Left header: logo
%\lhead{\includegraphics[width=5cm]{}}
% Define footer
\lfoot{Katharineum zu Lübeck}
\cfoot{\thepage}
\rfoot{\small Umsetzung: © 2018--2019 by Computer-AG}
\rhead{\textbf{
Vertretungen {{ date|date:"j. F Y, \\K\\W W"}}}\\
Stand: {% now "j. F Y H:i" %}\\
}
\begin{document}
\Large
\subsubsection*{}
{# \vspace{10pt}#}
\section*{\Huge Vertretungen {{ date|date:"l, j. F Y"}}}
{% if header_info.is_box_needed %}
\fbox{
\begin{tabular}{@{}ll@{}}
{% for row in header_info.rows %}
\textbf{ {{ row.0 }} } & {{ row.1 }} \\
{% endfor %}
\end{tabular}
}
{% endif %}
% Init table
{% set color_background = 1 %}
{% set last_classes = "" %}
\def\arraystretch{1.5}
\begin{longtable}{p{20mm}p{8mm}p{32mm}p{25mm}p{30mm}p{45mm}}
\textbf{Klassen} &
\textbf{Std.} &
\textbf{Lehrer} &
\textbf{Fach} &
\textbf{Raum} &
\textbf{Hinweis}\\
\hline
\endhead
{% for sub in subs %}
{# ------------------------------ #}
{# Color groups of classes in grey/white #}
{% if last_classes != sub.classes %}
{% if color_background %}
{% set color_background = 0 %}
{% else %}
{% set color_background = 1 %}
{% endif %}
{% endif %}
{% set last_classes = sub.classes %}
{# Background color #}
{% if color_background %}
\rowcolor{grey}
{% endif %}
{# ------------------------------ #}
{% with c=sub.color %}
{# Display classes #}
\color{ {{c}} }
\textbf{ {{ sub.classes }} } &
{# Display lesson number #}
\color{ {{c}} }
{{ sub.lesson }} &
\color{ {{c}} }
lehrer &
\color{ {{c}} }
fach &
\color{ {{c}} }
raum &
{# Display badge (for cancellations) #}
{% if sub.badge %}
\large\badge{ {{ sub.badge }} }
{% endif %}
{# Display notice and new line #}
\color{ {{c}} }
\Large\textit{ {{sub.text|default:""}} } \\
{% endwith %}
{% endfor %}
\end{longtable}
\end{document}
\ No newline at end of file
from django import template
register = template.Library()
class SetVarNode(template.Node):
def __init__(self, var_name, var_value):
self.var_name = var_name
self.var_value = var_value
def render(self, context):
try:
value = template.Variable(self.var_value).resolve(context)
except template.VariableDoesNotExist:
value = ""
context[self.var_name] = value
return u""
@register.tag(name='set')
def set_var(parser, token):
"""
{% set some_var = '123' %}
"""
parts = token.split_contents()
if len(parts) < 4:
raise template.TemplateSyntaxError("'set' tag must be of the form: {% set <var_name> = <var_value> %}")
return SetVarNode(parts[1], parts[3])
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