From 0e8054d8c79d66c7971d0f581143a3aa5ef63cfb Mon Sep 17 00:00:00 2001 From: LammLukas Date: Mon, 3 Feb 2020 14:23:08 +0100 Subject: [PATCH] Added protocol class with docx export --- src/protocols.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/protocols.py diff --git a/src/protocols.py b/src/protocols.py new file mode 100644 index 0000000..ad18191 --- /dev/null +++ b/src/protocols.py @@ -0,0 +1,53 @@ +""" +Class for protocol objects + +Author: L. Lamm (lamm@ifam.rwth-aachen.de) +""" + +from src.exam import Exam +from src.exceptions import * +from docxtpl import DocxTemplate +import re + + +class Protocol: + """Class for objects of type protocol""" + def __init__(self, exam): + """Constructor of protocol object""" + self.exam = exam + + def make_protocol(self, file, doc_format='docx', lang='german'): + """Exports protocol""" + if doc_format == 'docx': + self.make_docx_protocol(file, lang) + else: + raise FormatNotDefined() + + def make_docx_protocol(self, file, lang): + """Export docx protocol from template""" + if lang == 'german': + doc = DocxTemplate("./templates/protocols/protocol_german_written.docx") + else: + raise ProtocolTemplateDoesNotExist() + + for room in self.exam.Rooms: + split_str = re.sub("\.docx$", '', file) + file_str = split_str + '_' + room.Name + '.docx' + + if room.SupervisorInCharge is None: + supervisor = '' + else: + supervisor = room.SupervisorInCharge + if room.ExtraSupervisors is None: + ex_supervisor = '' + else: + ex_supervisor = room.SupervisorInCharge + + context = {'COURSE': self.exam.Name, + 'EXAMINER': self.exam.Examiner, + 'DATE': self.exam.Date, + 'ROOM': room.Name, + 'SUPERVISOR': supervisor, + 'EX_SUPERVISORS': ex_supervisor} + doc.render(context) + doc.save(file_str) \ No newline at end of file -- GitLab