protocols.py 2.55 KB
Newer Older
1
2
3
4
5
6
7
"""
Class for protocol objects

Author: L. Lamm (lamm@ifam.rwth-aachen.de)
"""
from src.exceptions import *
from docxtpl import DocxTemplate
8
import os, re
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


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':
27
28
            basedir = os.path.dirname(os.path.dirname(__file__))
            template_file = os.path.join(basedir, r'templates', r'protocols', r'protocol_german_written.docx')
29
30
31
32
        else:
            raise ProtocolTemplateDoesNotExist()

        for room in self.exam.Rooms:
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
            if len(room.AssignedParticipants) > 0:
                doc = DocxTemplate(template_file)

                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

                participant_list = []
                for participant in room.AssignedParticipants:
                    attributes = [str(participant.Matriculation),
                                  str(participant.Lastname),
                                  str(participant.Firstname),
                                  str(participant.NumberOfTrials)]
                    participant_list.append({'cols': attributes})

                context = {'COURSE': self.exam.Name,
                           'EXAMINER': self.exam.Examiner,
                           'DATE': self.exam.Date,
                           'ROOM': room.Name,
                           'SUPERVISOR': supervisor,
                           'EX_SUPERVISORS': ex_supervisor,
                           'NUM_PARTICIPANTS': len(room.AssignedParticipants),
                           'col_labels': ['Matr.-Nr.', 'Nachname', 'Vorname', 'Versuch'],
                           'tbl_contents': participant_list
                           }
                doc.render(context)
                doc.save(file_str)
                del doc