annotate getan/template.py @ 370:f4dcfbede99b

Move justification of human_time to templates (only for zeiterfassung) The totals are often longer 100h and more, so if justification is desired here, it would not be to two full hour digits anyway.
author Thomas Arendsen Hein <thomas@intevation.de>
date Mon, 03 Mar 2014 15:57:54 +0100
parents 1b190fa27482
children a805aaed97dd
rev   line source
356
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
2 #
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
3 # (c) 2014 by Björn Ricks <bjoern.ricks@intevation.de>
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
4 #
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
5 # This is Free Software licensed under the terms of GPLv3 or later.
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
6 # For details see LICENSE coming with the source of 'getan'.
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
7
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
8 import os.path
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
9 import sys
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
10
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
11 from datetime import date, datetime, timedelta
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
12
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
13 from jinja2 import Environment, ChoiceLoader, FileSystemLoader, PackageLoader
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
14
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
15 from getan.backend import Backend, DEFAULT_DATABASE
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
16
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
17
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
18 def human_time(delta):
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
19 days = delta.days
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
20 seconds = days * 3600 * 24
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
21 s = seconds + delta.seconds
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
22 h = s / 3600
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
23 m = (s % 3600) / 60
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
24 if (s % 60) >= 30:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
25 m += 1
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
26 if m == 60:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
27 m = 0
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
28 h += 1
370
f4dcfbede99b Move justification of human_time to templates (only for zeiterfassung)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 360
diff changeset
29 return "%d:%02d" % (h, m)
356
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
30
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
31
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
32 def date_format(d):
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
33 return d.strftime("%d.%m.%Y")
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
34
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
35
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
36 def total_time(projects):
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
37 total = timedelta(0)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
38 for proj in projects:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
39 total += proj.get_total_duration()
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
40 return total
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
41
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
42
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
43 def unix_week(week, year=None):
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
44 """Convert iso week to unix week
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
45
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
46 For unix week see man date "%W"
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
47 Args:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
48 week: Week number as int to convert
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
49 year: Year as int. If year is none the current year is used.
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
50 """
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
51 if not year:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
52 year = datetime.now().year
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
53 firstday = date(year, 1, 4)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
54 isoweek = firstday.isocalendar()[1]
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
55 unixweek = int(firstday.strftime("%W"))
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
56 diff = isoweek - unixweek
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
57 return week - diff
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
58
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
59
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
60 def render(template, database=None, year=None, week=None, project=None,
360
1b190fa27482 Allow to render only projects with entries in templates
Björn Ricks <bjoern.ricks@intevation.de>
parents: 356
diff changeset
61 user=None, empty_projects=True):
356
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
62 if not user:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
63 user = os.getenv("USER")
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
64
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
65 if not database:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
66 if os.path.isfile(DEFAULT_DATABASE):
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
67 database = os.path.abspath(DEFAULT_DATABASE)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
68 else:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
69 database = os.path.expanduser(os.path.join("~", ".getan",
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
70 DEFAULT_DATABASE))
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
71 if not os.path.isfile(database):
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
72 print >> sys.stderr, "'%s' does not exist or is not a file." % database
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
73 sys.exit(1)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
74
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
75 u_week = None
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
76
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
77 c_year = int(date.today().strftime("%Y"))
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
78 c_week = datetime.now().isocalendar()[1]
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
79
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
80 if week is not None and year is None:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
81 year = c_year
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
82
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
83 if not os.path.isfile(database):
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
84 print >> sys.stderr, "'%s' does not exist or is not a file." % database
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
85 sys.exit(1)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
86
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
87 loader = ChoiceLoader([FileSystemLoader(os.path.expanduser(os.path.join(
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
88 "~", ".getan", "templates"))),
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
89 PackageLoader("getan")])
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
90
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
91 env = Environment(loader=loader)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
92 env.filters["human_time"] = human_time
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
93 env.filters["date_format"] = date_format
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
94
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
95 template_name = template or "wochenbericht"
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
96 template = env.get_template(template_name)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
97
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
98 backend = Backend(database)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
99
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
100 if not project:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
101 projects = backend.load_projects()
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
102 else:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
103 project = backend.load_project(project)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
104 projects = [project]
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
105
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
106 if year is not None or week is not None:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
107 u_week = "%02d" % unix_week(week, year)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
108 for project in projects:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
109 project.load_entries(year, u_week)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
110
360
1b190fa27482 Allow to render only projects with entries in templates
Björn Ricks <bjoern.ricks@intevation.de>
parents: 356
diff changeset
111 if not empty_projects:
1b190fa27482 Allow to render only projects with entries in templates
Björn Ricks <bjoern.ricks@intevation.de>
parents: 356
diff changeset
112 projects = [project for project in projects if project.entries]
1b190fa27482 Allow to render only projects with entries in templates
Björn Ricks <bjoern.ricks@intevation.de>
parents: 356
diff changeset
113
356
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
114 context = dict()
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
115 context["project"] = project
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
116 context["projects"] = projects
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
117 context["user"] = user
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
118 context["database"] = database
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
119 context["year"] = year
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
120 context["week"] = week
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
121 context["current_week"] = c_week
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
122 context["current_year"] = c_year
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
123 context["unix_week"] = u_week
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
124 context["total_time"] = total_time(projects)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
125 return template.render(context)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)