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