annotate getan/project.py @ 354:4f5094f3f615

Add method to calculate the total duration of a project
author Björn Ricks <bjoern.ricks@intevation.de>
date Mon, 03 Mar 2014 14:27:36 +0100
parents 2e7885dc6669
children bc12acbff143
rev   line source
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
1 #!/usr/bin/env python
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
3 #
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
4 # (c) 2008, 2009, 2010 by
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
5 # Sascha L. Teichmann <sascha.teichmann@intevation.de>
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
6 # Ingo Weinzierl <ingo.weinzierl@intevation.de>
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
7 #
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
8 # This is Free Software licensed unter the terms of GPLv3 or later.
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
9 # For details see LICENSE coming with the source of 'getan'.
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
10
145
ccd47a2d37a6 Carefully handle non unicode strings for urwid
Björn Ricks <bjoern.ricks@intevation.de>
parents: 137
diff changeset
11 import locale
ccd47a2d37a6 Carefully handle non unicode strings for urwid
Björn Ricks <bjoern.ricks@intevation.de>
parents: 137
diff changeset
12
354
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
13 from datetime import datetime, timedelta
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
14
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
15
136
92b7582b8f44 Use new style classes
Björn Ricks <bjoern.ricks@intevation.de>
parents: 135
diff changeset
16 class Project(object):
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
17
352
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
18 def __init__(self, backend, id, key, desc, total):
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
19 self.backend = backend
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
20 self.id = id
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
21 self.key = key
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
22 self.desc = desc
352
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
23 self._entries = None
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
24 self.total = total
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
25 self.start = None
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
26 self.stop = None
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
27
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
28 def year(self):
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
29 total = 0
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
30 now = datetime.now()
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
31 for entry in self.entries:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
32 start = entry.start
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
33 if start.year == now.year:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
34 total += (entry.end - start).seconds
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
35 return total
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
36
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
37 def month(self):
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
38 total = 0
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
39 now = datetime.now()
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
40 for entry in self.entries:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
41 start = entry.start
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
42 if start.month == now.month and start.year == now.year:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
43 total += (entry.end - start).seconds
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
44 return total
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
45
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
46 def week(self):
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
47 total = 0
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
48 now = datetime.now()
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
49 tweek = now.strftime('%W')
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
50 for entry in self.entries:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
51 start = entry.start
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
52 if start.strftime('%W') == tweek and start.year == now.year:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
53 total += (entry.end - start).seconds
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
54 return total
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
55
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
56 def day(self):
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
57 total = 0
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
58 now = datetime.now()
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
59 for entry in self.entries:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
60 start = entry.start
135
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 23
diff changeset
61 if start.month == now.month and start.year == now.year \
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
62 and start.day == now.day:
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
63 total += (entry.end - start).seconds
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
64 return total
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
65
352
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
66 def load_entries(self, year=None, week=None):
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
67 self._entries = self.backend.load_entries(self.id, year, week)
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
68
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
69 @property
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
70 def entries(self):
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
71 if self._entries is None:
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
72 self.load_entries()
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
73 return self._entries
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
74
354
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
75 def get_total_duration(self):
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
76 dur = timedelta(0)
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
77 for entry in self.entries:
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
78 dur += entry.get_duration()
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
79 return dur
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
80
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
81
136
92b7582b8f44 Use new style classes
Björn Ricks <bjoern.ricks@intevation.de>
parents: 135
diff changeset
82 class Entry(object):
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
83
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
84 def __init__(self, id, project_id, start, end, desc):
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
85 self.id = id
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
86 self.project_id = project_id
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
87 self.start = start
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
88 self.end = end
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
89 self.desc = desc
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
90
145
ccd47a2d37a6 Carefully handle non unicode strings for urwid
Björn Ricks <bjoern.ricks@intevation.de>
parents: 137
diff changeset
91 # carefully handle non unicode string
ccd47a2d37a6 Carefully handle non unicode strings for urwid
Björn Ricks <bjoern.ricks@intevation.de>
parents: 137
diff changeset
92 # urwid seems to have some issue with plain str
147
8cc1f83d32ee Description may be not set
Björn Ricks <bjoern.ricks@intevation.de>
parents: 145
diff changeset
93 if self.desc and not isinstance(self.desc, unicode):
145
ccd47a2d37a6 Carefully handle non unicode strings for urwid
Björn Ricks <bjoern.ricks@intevation.de>
parents: 137
diff changeset
94 self.desc = unicode(self.desc, locale.getpreferredencoding())
ccd47a2d37a6 Carefully handle non unicode strings for urwid
Björn Ricks <bjoern.ricks@intevation.de>
parents: 137
diff changeset
95
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
96 def duration(self):
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
97 return (self.end - self.start)
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
98
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
99 def __str__(self):
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
100 return ("[%s | %s | %s | %s | %s]" %
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
101 (self.id, self.project_id, self.start, self.end, self.desc))
137
5a4946fd9e6d Update whitespace
Björn Ricks <bjoern.ricks@intevation.de>
parents: 136
diff changeset
102
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
103 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)