Mercurial > getan > getan
annotate getan/template.py @ 536:a59622c06936
Fix generating reports if no week given
unix_week() cannot handle week being None but does so for year. It just
assumes the current year in this case, although this only duplicates the
initialisation of year with c_year in this context. week being None gives
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
load_entries() handles the case with no week given, thus call it anyhow.
This fix makes it possible to invoke scripts/getan-report with a year given
but no week and seeing the expected result (data for a whole year) instead
of a traceback.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Thu, 09 Jan 2020 11:32:34 +0100 |
parents | 31ea48bf8a7c |
children | 28b1c18c900f |
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> |
536
a59622c06936
Fix generating reports if no week given
Tom Gottfried <tom@intevation.de>
parents:
511
diff
changeset
|
4 # 2017, 2018, 2020 Intevation GmbH |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
5 # |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
6 # 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
|
7 # 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
|
8 |
412
cc56bc1fd56b
Code-quality: prepares template.py for logging.
Bernhard Reiter <bernhard@intevation.de>
parents:
389
diff
changeset
|
9 # import logging |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
10 import os.path |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
11 import sys |
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 datetime import date, datetime, timedelta |
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 jinja2 import Environment, ChoiceLoader, FileSystemLoader, PackageLoader |
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 from getan.backend import Backend, DEFAULT_DATABASE |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
18 |
412
cc56bc1fd56b
Code-quality: prepares template.py for logging.
Bernhard Reiter <bernhard@intevation.de>
parents:
389
diff
changeset
|
19 # logging.basicConfig(level='DEBUG') # quick fix until getan-eval.py offers it |
cc56bc1fd56b
Code-quality: prepares template.py for logging.
Bernhard Reiter <bernhard@intevation.de>
parents:
389
diff
changeset
|
20 # logger = logging.getLogger() |
cc56bc1fd56b
Code-quality: prepares template.py for logging.
Bernhard Reiter <bernhard@intevation.de>
parents:
389
diff
changeset
|
21 |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
22 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
23 def human_time(delta): |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
24 days = delta.days |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
25 seconds = days * 3600 * 24 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
26 s = seconds + delta.seconds |
511
31ea48bf8a7c
human_time calculates up to 59 minutes, not 60.
Magnus Schieder <mschieder@intevation.de>
parents:
467
diff
changeset
|
27 h = s // 3600 |
31ea48bf8a7c
human_time calculates up to 59 minutes, not 60.
Magnus Schieder <mschieder@intevation.de>
parents:
467
diff
changeset
|
28 m = (s % 3600) // 60 |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
29 if (s % 60) >= 30: |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
30 m += 1 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
31 if m == 60: |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
32 m = 0 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
33 h += 1 |
370
f4dcfbede99b
Move justification of human_time to templates (only for zeiterfassung)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
360
diff
changeset
|
34 return "%d:%02d" % (h, m) |
356
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 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
37 def date_format(d): |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
38 return d.strftime("%d.%m.%Y") |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
39 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
40 |
389
9a0dba03d16c
Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents:
372
diff
changeset
|
41 def duration(entries): |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
42 total = timedelta(0) |
389
9a0dba03d16c
Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents:
372
diff
changeset
|
43 for entry in entries: |
9a0dba03d16c
Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents:
372
diff
changeset
|
44 total += entry.get_duration() |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
45 return total |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
46 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
47 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
48 def unix_week(week, year=None): |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
49 """Convert iso week to unix week |
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 For unix week see man date "%W" |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
52 Args: |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
53 week: Week number as int to convert |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
54 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
|
55 """ |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
56 if not year: |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
57 year = datetime.now().year |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
58 firstday = date(year, 1, 4) |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
59 isoweek = firstday.isocalendar()[1] |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
60 unixweek = int(firstday.strftime("%W")) |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
61 diff = isoweek - unixweek |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
62 return week - diff |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
63 |
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 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
|
66 user=None, empty_projects=True): |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
67 if not user: |
431
8922713adbe6
getan-eval.py: Improves how 'user' is found.
Bernhard Reiter <bernhard@intevation.de>
parents:
412
diff
changeset
|
68 user = os.getenv("USER") or "USER" |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
69 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
70 if not database: |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
71 if os.path.isfile(DEFAULT_DATABASE): |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
72 database = os.path.abspath(DEFAULT_DATABASE) |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
73 else: |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
74 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
|
75 DEFAULT_DATABASE)) |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
76 if not os.path.isfile(database): |
467
59d9c5840273
Porting Python 2 to Python 3.
Magnus Schieder <mschieder@intevation.de>
parents:
431
diff
changeset
|
77 print("'%s' does not exist or is not a file." % database, file=sys.stderr) |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
78 sys.exit(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 u_week = None |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
81 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
82 c_year = int(date.today().strftime("%Y")) |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
83 c_week = datetime.now().isocalendar()[1] |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
84 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
85 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
|
86 year = c_year |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
87 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
88 if not os.path.isfile(database): |
467
59d9c5840273
Porting Python 2 to Python 3.
Magnus Schieder <mschieder@intevation.de>
parents:
431
diff
changeset
|
89 print("'%s' does not exist or is not a file." % database, file=sys.stderr) |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
90 sys.exit(1) |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
91 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
92 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
|
93 "~", ".getan", "templates"))), |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
94 PackageLoader("getan")]) |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
95 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
96 env = Environment(loader=loader) |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
97 env.filters["human_time"] = human_time |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
98 env.filters["date_format"] = date_format |
389
9a0dba03d16c
Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents:
372
diff
changeset
|
99 env.filters["duration"] = duration |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
100 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
101 template_name = template or "wochenbericht" |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
102 template = env.get_template(template_name) |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
103 |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
104 backend = Backend(database) |
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 not project: |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
107 projects = backend.load_projects() |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
108 else: |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
109 project = backend.load_project(project) |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
110 projects = [project] |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
111 |
536
a59622c06936
Fix generating reports if no week given
Tom Gottfried <tom@intevation.de>
parents:
511
diff
changeset
|
112 if week is not None: |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
113 u_week = "%02d" % unix_week(week, year) |
536
a59622c06936
Fix generating reports if no week given
Tom Gottfried <tom@intevation.de>
parents:
511
diff
changeset
|
114 |
a59622c06936
Fix generating reports if no week given
Tom Gottfried <tom@intevation.de>
parents:
511
diff
changeset
|
115 for project in projects: |
a59622c06936
Fix generating reports if no week given
Tom Gottfried <tom@intevation.de>
parents:
511
diff
changeset
|
116 project.load_entries(year, u_week) |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
117 |
360
1b190fa27482
Allow to render only projects with entries in templates
Björn Ricks <bjoern.ricks@intevation.de>
parents:
356
diff
changeset
|
118 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
|
119 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
|
120 |
389
9a0dba03d16c
Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents:
372
diff
changeset
|
121 entries = [] |
9a0dba03d16c
Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents:
372
diff
changeset
|
122 for project in projects: |
9a0dba03d16c
Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents:
372
diff
changeset
|
123 entries.extend(project.entries) |
9a0dba03d16c
Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents:
372
diff
changeset
|
124 |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
125 context = dict() |
389
9a0dba03d16c
Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents:
372
diff
changeset
|
126 context["entries"] = entries |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
127 context["project"] = project |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
128 context["projects"] = projects |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
129 context["user"] = user |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
130 context["database"] = database |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
131 context["year"] = year |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
132 context["week"] = week |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
133 context["current_week"] = c_week |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
134 context["current_year"] = c_year |
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
135 context["unix_week"] = u_week |
389
9a0dba03d16c
Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents:
372
diff
changeset
|
136 context["total_time"] = duration(entries) |
372
a805aaed97dd
Add current date to getan template context
Björn Ricks <bjoern.ricks@intevation.de>
parents:
370
diff
changeset
|
137 context["today"] = date.today() |
356
45d97d47a9fe
Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
138 return template.render(context) |