summaryrefslogtreecommitdiff
path: root/maconomy/templates.py
blob: 80cafbb7fd948508c88288d26374273fb2602dd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
69
70
71
72
73
74
75
76
77
78
79
from string import Template
from maconomy import utils

class BaseTemplate(object):
    def __init__(self, template_file):
        self.set_template(template_file)

    def build(self, **kwargs):
        return self.template.substitute(**kwargs)

    def set_template(self, template_file):
        with open(template_file, 'r') as f:
            template_base = f.read()
        self.template = Template(template_base)

    def tag(self, tag, content):
        return u"<{tag}>{content}</{tag}>".format(tag=tag, content=content)


class UnsubmittedEmailTemplate(BaseTemplate):
    def __init__(self):
        self.set_template("templates/unsubmitted.html")

    def build(self, week, maconomyurl, helpurl):
        return self.template.substitute(week=week, maconomyurl=maconomyurl, helpurl=helpurl)

class MissingEmailTemplate(BaseTemplate):
    def __init__(self):
        self.set_template("templates/missing.html")

    def build(self, maconomyurl, helpurl):
        return self.template.substitute(maconomyurl=maconomyurl, helpurl=helpurl)

class ManagerEmailTemplate(BaseTemplate):
    def __init__(self):
        self.set_template("templates/manager.html")
        self.status_template = EmployeeStatusListTemplate()

    def build(self, timesheets, maconomyurl):
        statuslist = self.build_statuslist(timesheets)
        return self.template.substitute(statuslist=statuslist,maconomyurl=maconomyurl, amount=len(timesheets))

    def build_statuslist(self, timesheets):
        result = u""
        unfinished = [timesheet for timesheet in timesheets if not timesheet.is_submitted()]
        #Sort to get unsubmitted last
        unfinished.sort(key=lambda t: t.status_summary())
        if unfinished:
            result += self.tag("h4","Unfinished:")+u"\n{}".format(self.status_template.build(unfinished))

        notapproved = set(timesheets) - set(unfinished)
        if notapproved:
            result += self.tag("h4", "For approval:") + u"\n{}".format(self.status_template.build(notapproved))
        return result

class EmployeeStatusListTemplate(BaseTemplate):
    def __init__(self):
        pass
    def build(self, timesheets):
        statuslist = u"\n".join([self.status_summary(t) for t in timesheets])
        return self.tag("ul", statuslist) if statuslist else ""

    def status_summary(self, timesheet):
        content = u"{}".format(timesheet.status_summary())
        return self.tag("li", content)

class CEOEmailTemplate(BaseTemplate):
    def __init__(self):
        self.set_template("templates/ceo.html")
        self.status_template = EmployeeStatusListTemplate()

    def build(self, timesheets):
        per_manager = utils.per_manager(timesheets)
        statuslist = u""
        for manager_id, relevant_timesheets in per_manager.items():
            relevant_timesheets.sort(key=lambda t: t.status_summary())
            statuslist += self.tag("h4", u"Status for {} employees:".format(manager_id))
            statuslist += self.status_template.build(relevant_timesheets)
        return self.template.substitute(statuslist=statuslist)