annotate getan/template.py @ 412:cc56bc1fd56b

Code-quality: prepares template.py for logging.
author Bernhard Reiter <bernhard@intevation.de>
date Wed, 03 May 2017 12:59:37 +0200
parents 9a0dba03d16c
children 8922713adbe6
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
412
cc56bc1fd56b Code-quality: prepares template.py for logging.
Bernhard Reiter <bernhard@intevation.de>
parents: 389
diff changeset
8 # import logging
356
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
9 import os.path
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
10 import sys
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
11
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
12 from datetime import date, datetime, timedelta
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
13
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
14 from jinja2 import Environment, ChoiceLoader, FileSystemLoader, PackageLoader
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
15
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
16 from getan.backend import Backend, DEFAULT_DATABASE
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
17
412
cc56bc1fd56b Code-quality: prepares template.py for logging.
Bernhard Reiter <bernhard@intevation.de>
parents: 389
diff changeset
18 # 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
19 # logger = logging.getLogger()
cc56bc1fd56b Code-quality: prepares template.py for logging.
Bernhard Reiter <bernhard@intevation.de>
parents: 389
diff changeset
20
356
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
21
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
22 def human_time(delta):
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
23 days = delta.days
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
24 seconds = days * 3600 * 24
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
25 s = seconds + delta.seconds
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
26 h = s / 3600
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
27 m = (s % 3600) / 60
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
28 if (s % 60) >= 30:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
29 m += 1
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
30 if m == 60:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
31 m = 0
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
32 h += 1
370
f4dcfbede99b Move justification of human_time to templates (only for zeiterfassung)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 360
diff changeset
33 return "%d:%02d" % (h, m)
356
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 date_format(d):
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
37 return d.strftime("%d.%m.%Y")
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
38
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
39
389
9a0dba03d16c Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents: 372
diff changeset
40 def duration(entries):
356
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
41 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
42 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
43 total += entry.get_duration()
356
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
44 return total
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
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
47 def unix_week(week, year=None):
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
48 """Convert iso week to unix week
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
49
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
50 For unix week see man date "%W"
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
51 Args:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
52 week: Week number as int to convert
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
53 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
54 """
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
55 if not year:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
56 year = datetime.now().year
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
57 firstday = date(year, 1, 4)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
58 isoweek = firstday.isocalendar()[1]
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
59 unixweek = int(firstday.strftime("%W"))
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
60 diff = isoweek - unixweek
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
61 return week - diff
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
62
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 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
65 user=None, empty_projects=True):
356
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
66 if not user:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
67 user = os.getenv("USER")
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
68
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
69 if not database:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
70 if os.path.isfile(DEFAULT_DATABASE):
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
71 database = os.path.abspath(DEFAULT_DATABASE)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
72 else:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
73 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
74 DEFAULT_DATABASE))
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
75 if not os.path.isfile(database):
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
76 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
77 sys.exit(1)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
78
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
79 u_week = None
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
80
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
81 c_year = int(date.today().strftime("%Y"))
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
82 c_week = datetime.now().isocalendar()[1]
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
83
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
84 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
85 year = c_year
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 if not os.path.isfile(database):
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
88 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
89 sys.exit(1)
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 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
92 "~", ".getan", "templates"))),
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
93 PackageLoader("getan")])
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 env = Environment(loader=loader)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
96 env.filters["human_time"] = human_time
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
97 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
98 env.filters["duration"] = duration
356
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 template_name = template or "wochenbericht"
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
101 template = env.get_template(template_name)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
102
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
103 backend = Backend(database)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
104
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
105 if not project:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
106 projects = backend.load_projects()
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
107 else:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
108 project = backend.load_project(project)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
109 projects = [project]
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
110
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
111 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
112 u_week = "%02d" % unix_week(week, year)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
113 for project in projects:
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
114 project.load_entries(year, u_week)
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
115
360
1b190fa27482 Allow to render only projects with entries in templates
Björn Ricks <bjoern.ricks@intevation.de>
parents: 356
diff changeset
116 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
117 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
118
389
9a0dba03d16c Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents: 372
diff changeset
119 entries = []
9a0dba03d16c Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents: 372
diff changeset
120 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
121 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
122
356
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
123 context = dict()
389
9a0dba03d16c Add list of all entries to the template context
Björn Ricks <bjoern.ricks@intevation.de>
parents: 372
diff changeset
124 context["entries"] = entries
356
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
125 context["project"] = project
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
126 context["projects"] = projects
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
127 context["user"] = user
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
128 context["database"] = database
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
129 context["year"] = year
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
130 context["week"] = week
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
131 context["current_week"] = c_week
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
132 context["current_year"] = c_year
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
133 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
134 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
135 context["today"] = date.today()
356
45d97d47a9fe Add a template module to getan
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
136 return template.render(context)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)