annotate getan/backend.py @ 564:c8e63e919a90

Fixes issues found by pyflakes.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 28 Sep 2020 14:48:51 +0200
parents fbe26b0e59ed
children
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 # -*- 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
2 #
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 # (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
4 # 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
5 # Ingo Weinzierl <ingo.weinzierl@intevation.de>
97
99639833968d Add me as author in files that I did touch
Björn Ricks <bjoern.ricks@intevation.de>
parents: 68
diff changeset
6 # (c) 2011 Björn Ricks <bjoern.ricks@intevation.de>
502
fea767901dbc Changed version to 3.1dev1.
Magnus Schieder <mschieder@intevation.de>
parents: 501
diff changeset
7 # (c) 2018 Intevation GmbH
fea767901dbc Changed version to 3.1dev1.
Magnus Schieder <mschieder@intevation.de>
parents: 501
diff changeset
8 # Authors:
fea767901dbc Changed version to 3.1dev1.
Magnus Schieder <mschieder@intevation.de>
parents: 501
diff changeset
9 # * Magnus Schieder <magnus.schieder@intevation.de>
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
10 #
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
11 # 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
12 # 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
13 #
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
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
15 import logging
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
16 import os
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
17
33
f96a18c10836 Made getan python-2.4 compatible.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 23
diff changeset
18 try:
f96a18c10836 Made getan python-2.4 compatible.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 23
diff changeset
19 import sqlite3 as db
f96a18c10836 Made getan python-2.4 compatible.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 23
diff changeset
20 except ImportError:
f96a18c10836 Made getan python-2.4 compatible.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 23
diff changeset
21 from pysqlite2 import dbapi2 as db
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
22
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
23 from getan.project import Project, Entry
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
24
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
25 DEFAULT_DATABASE = "time.db"
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
26
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
27 CREATE_TABLES = [
348
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
28 """
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
29 CREATE TABLE projects (
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
30 id INTEGER PRIMARY KEY AUTOINCREMENT,
284
561441fde2ac Set unique constraint on project key
Björn Ricks <bjoern.ricks@intevation.de>
parents: 174
diff changeset
31 key VARCHAR(16) NOT NULL CONSTRAINT unique_key UNIQUE,
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
32 description VARCHAR(256),
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
33 active BOOLEAN DEFAULT 1
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
34 )
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
35 """,
348
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
36 """
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
37 CREATE TABLE entries (
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
38 id INTEGER PRIMARY KEY AUTOINCREMENT,
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
39 project_id INTEGER REFERENCES projects(id),
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
40 start_time TIMESTAMP NOT NULL,
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
41 stop_time TIMESTAMP NOT NULL,
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
42 description VARCHAR(256),
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
43
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
44 CHECK (strftime('%s', start_time) <= strftime('%s', stop_time))
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
45 )
499
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
46 """,
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
47 """
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
48 CREATE TABLE recover(
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
49 id INTEGER PRIMARY KEY,
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
50 project_id INTEGER REFERENCES projects(id),
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
51 start_time TIMESTAMP NOT NULL,
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
52 stop_time TIMESTAMP NOT NULL,
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
53 description VARCHAR(256),
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
54
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
55 CHECK (strftime('%s', start_time) <= strftime('%s', stop_time))
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
56 )
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
57 """
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
58 ]
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
59
501
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
60 CREATE_RECOVER = """
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
61 CREATE TABLE IF NOT EXISTS recover(
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
62 id INTEGER PRIMARY KEY,
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
63 project_id INTEGER REFERENCES projects(id),
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
64 start_time TIMESTAMP NOT NULL,
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
65 stop_time TIMESTAMP NOT NULL,
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
66 description VARCHAR(256),
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
67
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
68 CHECK (strftime('%s', start_time) <= strftime('%s', stop_time))
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
69 )
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
70 """
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
71
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
72 LOAD_ACTIVE_PROJECTS = '''
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
73 SELECT id, key, description, 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
74 FROM projects LEFT JOIN
144
9e853f9e63ad Remove whitespace
Björn Ricks <bjoern.ricks@intevation.de>
parents: 143
diff changeset
75 (SELECT
9e853f9e63ad Remove whitespace
Björn Ricks <bjoern.ricks@intevation.de>
parents: 143
diff changeset
76 project_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
77 sum(strftime('%s', stop_time) - strftime('%s', start_time)) AS total
144
9e853f9e63ad Remove whitespace
Björn Ricks <bjoern.ricks@intevation.de>
parents: 143
diff changeset
78 FROM entries
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
79 GROUP BY project_id) ON project_id = id
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
80 WHERE active
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 '''
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
82
550
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
83 LOAD_ACTIVE_PROJECTS_LIKE = '''
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
84 SELECT id, key, description, total
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
85 FROM projects LEFT JOIN
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
86 (SELECT
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
87 project_id,
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
88 sum(strftime('%s', stop_time) - strftime('%s', start_time)) AS total
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
89 FROM entries
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
90 GROUP BY project_id) ON project_id = id
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
91 WHERE active and key LIKE :project_id
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
92 '''
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
93
351
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
94 LOAD_ACTIVE_PROJECT = '''
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
95 SELECT id, key, description, total
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
96 FROM projects LEFT JOIN
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
97 (SELECT
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
98 project_id,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
99 sum(strftime('%s', stop_time) - strftime('%s', start_time)) AS total
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
100 FROM entries
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
101 GROUP BY project_id) ON project_id = id
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
102 WHERE active and key = :project_id
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
103 '''
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
104
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
105 LOAD_PROJECT_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
106 SELECT
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
107 id,
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
108 project_id,
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
109 start_time as "[timestamp]",
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
110 stop_time as "[timestamp]",
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
111 description
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
112 FROM
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
113 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
114 WHERE
350
f581752317fd Correctly escape sql query
Björn Ricks <bjoern.ricks@intevation.de>
parents: 349
diff changeset
115 project_id = :project_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
116 ORDER 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
117 id
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
118 DESC
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
119 '''
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
120
351
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
121 LOAD_PROJECT_ENTRIES_YEAR = '''
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
122 SELECT
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
123 id,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
124 project_id,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
125 start_time as "[timestamp]",
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
126 stop_time as "[timestamp]",
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
127 'no description' AS description
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
128 FROM entries
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
129 WHERE
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
130 project_id = :project_id AND
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
131 (description IS NULL or length(description) = 0)
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
132 GROUP BY round(julianday(start_time))
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
133 UNION
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
134 SELECT
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
135 id,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
136 project_id,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
137 start_time as "[timestamp]",
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
138 stop_time as "[timestamp]",
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
139 description
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
140 FROM entries
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
141 WHERE
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
142 project_id = :project_id AND
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
143 (strftime('%Y', start_time) ) = :year AND
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
144 description IS NOT NULL AND length(description) > 0
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
145 '''
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
146
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
147 LOAD_PROJECT_ENTRIES_WEEK = '''
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
148 SELECT
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
149 id,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
150 project_id,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
151 start_time as "[timestamp]",
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
152 stop_time as "[timestamp]",
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
153 'no description' AS description
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
154 FROM entries
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
155 WHERE
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
156 project_id = :project_id AND
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
157 (strftime('%Y', start_time) ) = :year AND
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
158 (description IS NULL or length(description) = 0)
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
159 AND (strftime('%W', start_time) = :week
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
160 OR strftime('%W', stop_time) = :week)
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
161 GROUP BY round(julianday(start_time))
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
162 UNION
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
163 SELECT
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
164 id,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
165 project_id,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
166 start_time as "[timestamp]",
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
167 stop_time as "[timestamp]",
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
168 description
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
169 FROM entries
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
170 WHERE
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
171 project_id = :project_id AND
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
172 (strftime('%Y', start_time) ) = :year AND
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
173 description IS NOT NULL AND length(description) > 0
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
174 AND (strftime('%W', start_time) = :week
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
175 OR strftime('%W', stop_time) = :week)
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
176 ORDER BY start_time
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
177 '''
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
178
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
179 INSERT_PROJECT_ENTRY = '''
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
180 INSERT INTO entries (project_id, start_time, stop_time, description)
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
181 VALUES(?,?,?,?)
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
182 '''
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
183
499
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
184 INSERT_RECOVER= '''
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
185 INSERT OR REPLACE INTO recover(id, project_id, start_time, stop_time, description)
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
186 VALUES(1,?,?,?,?)
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
187 '''
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
188
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
189 LOAD_RECOVER= '''
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
190 SELECT
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
191 id,
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
192 project_id,
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
193 start_time as "[timestamp]",
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
194 stop_time as "[timestamp]",
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
195 description
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
196 FROM
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
197 recover
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
198 WHERE
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
199 id = 1
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
200 '''
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
201
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
202 DELETE_RECOVER= "DELETE FROM recover"
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
203
49
062ce001abd1 add backend method to create new projects
Björn Ricks <bjoern.ricks@intevation.de>
parents: 40
diff changeset
204 INSERT_PROJECT = '''
52
9cb57c37369a fix insert new project sql statemenent and api
Björn Ricks <bjoern.ricks@intevation.de>
parents: 50
diff changeset
205 INSERT INTO projects (key, description, active) VALUES (?,?,1)
49
062ce001abd1 add backend method to create new projects
Björn Ricks <bjoern.ricks@intevation.de>
parents: 40
diff changeset
206 '''
062ce001abd1 add backend method to create new projects
Björn Ricks <bjoern.ricks@intevation.de>
parents: 40
diff changeset
207
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
208 DELETE_PROJECT_ENTRY = 'DELETE FROM entries WHERE id = %i'
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
209
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
210 MOVE_ENTRY = 'UPDATE entries SET project_id = ? WHERE id = ?'
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
211
401
a487535e7844 Adjusting starting datetime:
Bernhard Reiter <bernhard@intevation.de>
parents: 362
diff changeset
212 UPDATE_ENTRY = '''
a487535e7844 Adjusting starting datetime:
Bernhard Reiter <bernhard@intevation.de>
parents: 362
diff changeset
213 UPDATE entries
a487535e7844 Adjusting starting datetime:
Bernhard Reiter <bernhard@intevation.de>
parents: 362
diff changeset
214 SET description = ?, start_time = ?, stop_time = ?
a487535e7844 Adjusting starting datetime:
Bernhard Reiter <bernhard@intevation.de>
parents: 362
diff changeset
215 WHERE id = ?
a487535e7844 Adjusting starting datetime:
Bernhard Reiter <bernhard@intevation.de>
parents: 362
diff changeset
216 '''
68
a25dab413260 Add new method to update a description of an entry in the database backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 52
diff changeset
217
401
a487535e7844 Adjusting starting datetime:
Bernhard Reiter <bernhard@intevation.de>
parents: 362
diff changeset
218 UPDATE_PROJECT = 'UPDATE projects SET key = ?, description = ? WHERE id = ?'
174
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
219
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
220 logger = logging.getLogger()
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
221
348
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
222
165
83ba64c9a51e A project entry in the db must have a key
Björn Ricks <bjoern.ricks@intevation.de>
parents: 146
diff changeset
223 class InvalidProjectKeyError(Exception):
83ba64c9a51e A project entry in the db must have a key
Björn Ricks <bjoern.ricks@intevation.de>
parents: 146
diff changeset
224 pass
83ba64c9a51e A project entry in the db must have a key
Björn Ricks <bjoern.ricks@intevation.de>
parents: 146
diff changeset
225
348
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
226
143
9f4da22fb1f1 Use new style class for backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 135
diff changeset
227 class Backend(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
228
348
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
229 def __init__(self, database=DEFAULT_DATABASE):
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
230 self.database = database
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
231 self.ensure_exists()
348
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
232 self.con = db.connect(database,
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
233 detect_types=db.PARSE_DECLTYPES |
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
234 db.PARSE_COLNAMES)
467
59d9c5840273 Porting Python 2 to Python 3.
Magnus Schieder <mschieder@intevation.de>
parents: 401
diff changeset
235 self.con.text_factory = lambda x: str(x, "utf-8", "ignore")
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
236
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
237 def ensure_exists(self):
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
238 """ Creates the database file if it does not exist. """
348
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
239 if os.path.isfile(self.database):
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
240 return
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
241
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
242 con, cur = None, None
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
243 try:
348
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
244 con = db.connect(self.database)
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
245 cur = con.cursor()
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
246 try:
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
247 for sql in CREATE_TABLES:
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
248 cur.execute(sql)
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
249 con.commit()
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
250 except:
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
251 con.rollback()
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
252 raise
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
253 finally:
348
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
254 if cur:
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
255 cur.close()
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
256 if con:
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
257 con.close()
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
258
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
259 def load_projects(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
260 """ Loads active projects from database and returns them as array """
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
261 logger.debug("load active projects from database.")
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
262 cur = None
348
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
263 try:
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
264 cur = self.con.cursor()
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
265 cur.execute(LOAD_ACTIVE_PROJECTS)
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
266
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
267 projects = []
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
268 while True:
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
269 row = cur.fetchone()
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
270
135
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
271 if not row:
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
272 break
165
83ba64c9a51e A project entry in the db must have a key
Björn Ricks <bjoern.ricks@intevation.de>
parents: 146
diff changeset
273 # check key
83ba64c9a51e A project entry in the db must have a key
Björn Ricks <bjoern.ricks@intevation.de>
parents: 146
diff changeset
274 if not row[1]:
348
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
275 raise InvalidProjectKeyError("Project with id %s needs "
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
276 "a key" % row[0])
353
5ded6192b85b Pass the Backend to the Project constructor
Björn Ricks <bjoern.ricks@intevation.de>
parents: 351
diff changeset
277 proj = Project(self, *row)
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
278 projects.append(proj)
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
279
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
280 logger.info("found %i active projects." % len(projects))
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
281 return projects
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
282
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
283 finally:
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
284 close(cur)
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
285
550
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
286 def load_projects_like(self, key):
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
287 """ Loads active projects matching the SQL LIKE pattern from the
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
288 database and returns them as array. """
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
289 cur = None
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
290 try:
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
291 cur = self.con.cursor()
559
fbe26b0e59ed Make the selection of projects in getan-report case-sensitive again
Magnus Schieder <mschieder@intevation.de>
parents: 550
diff changeset
292 cur.execute("PRAGMA case_sensitive_like = true;")
550
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
293 cur.execute(LOAD_ACTIVE_PROJECTS_LIKE, {"project_id": key})
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
294
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
295 projects = []
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
296 while True:
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
297 row = cur.fetchone()
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
298
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
299 if not row:
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
300 break
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
301 # check key
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
302 if not row[1]:
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
303 raise InvalidProjectKeyError("Project with id %s needs "
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
304 "a key" % row[0])
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
305 proj = Project(self, *row)
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
306 projects.append(proj)
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
307
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
308 logger.info("found %i active projects." % len(projects))
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
309 return projects
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
310
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
311 finally:
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
312 close(cur)
28b1c18c900f Specify project keys in getan-report with SQL patterns
Magnus Schieder <mschieder@intevation.de>
parents: 502
diff changeset
313
499
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
314 def load_recover(self):
501
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
315 """If there is an entry in the recovery table, the entry is moved to
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
316 its project."""
499
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
317 try:
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
318 cur = self.con.cursor()
501
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
319 # Creates the recover table if it does not exist to make old
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
320 # databases compatible.
f5e1a78173cb Old databases can now be read in. Added comments.
Magnus Schieder <mschieder@intevation.de>
parents: 499
diff changeset
321 cur.execute(CREATE_RECOVER)
499
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
322 cur.execute(LOAD_RECOVER)
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
323 recover = cur.fetchone()
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
324 if not recover:
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
325 return
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
326
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
327 _, project_id, start_time, stop_time, desc = recover
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
328
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
329 cur.execute(INSERT_PROJECT_ENTRY, (
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
330 project_id, start_time, stop_time, desc))
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
331 cur.execute(DELETE_RECOVER)
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
332 self.con.commit()
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
333 finally:
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
334 close(cur)
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
335
351
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
336 def load_project(self, key):
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
337 logger.debug("load active projects from database.")
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
338 cur = None
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
339 try:
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
340 cur = self.con.cursor()
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
341 cur.execute(LOAD_ACTIVE_PROJECT, {"project_id": key})
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
342 row = cur.fetchone()
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
343 if not row:
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
344 raise InvalidProjectKeyError("Project with key %s not "
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
345 "found." % key)
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
346 # check key
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
347 if not row[1]:
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
348 raise InvalidProjectKeyError("Project with id %s needs "
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
349 "a key" % row[0])
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
350 return Project(self, *row)
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
351
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
352 finally:
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
353 close(cur)
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
354
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
355 def load_entries(self, project_id, year=None, week=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
356 """ Loads all entries that belong to a specific project """
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
357 logger.debug("load entries that belong to project %s" % project_id)
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
358 cur = None
351
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
359
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
360 if week and isinstance(week, int):
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
361 week = "%02d" % (week)
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
362
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
363 try:
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
364 cur = self.con.cursor()
351
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
365
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
366 if year is None and week is None:
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
367 cur.execute(LOAD_PROJECT_ENTRIES,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
368 {"project_id": project_id})
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
369 elif week is None:
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
370 cur.execute(LOAD_PROJECT_ENTRIES_YEAR,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
371 {'project_id': project_id, 'year': str(year)})
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
372 else:
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
373 cur.execute(LOAD_PROJECT_ENTRIES_WEEK,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
374 {'project_id': project_id,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
375 'week': week,
b5dc92631561 Allow to load one project and specific entries from the Backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 350
diff changeset
376 'year': str(year)})
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
377
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
378 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
379 while True:
146
bb70dde875fd Always fail if an entry couldn't be loaded
Björn Ricks <bjoern.ricks@intevation.de>
parents: 144
diff changeset
380 row = cur.fetchone()
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
381
146
bb70dde875fd Always fail if an entry couldn't be loaded
Björn Ricks <bjoern.ricks@intevation.de>
parents: 144
diff changeset
382 if not row:
bb70dde875fd Always fail if an entry couldn't be loaded
Björn Ricks <bjoern.ricks@intevation.de>
parents: 144
diff changeset
383 break
bb70dde875fd Always fail if an entry couldn't be loaded
Björn Ricks <bjoern.ricks@intevation.de>
parents: 144
diff changeset
384 entries.append(Entry(*row))
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
385
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
386 logger.debug("Found %i entries that belong to project '%i'"
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
387 % (len(entries), project_id))
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
388 return 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
389 finally:
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
390 close(cur)
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
391
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
392 def insert_project_entry(self, project, stop_time, desc):
135
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
393 if project is None:
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
394 return
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
395 cur = None
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
396 try:
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
397 cur = self.con.cursor()
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
398 cur.execute(INSERT_PROJECT_ENTRY, (
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
399 project.id, project.start, stop_time, desc))
499
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
400 cur.execute(DELETE_RECOVER)
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
401 self.con.commit()
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
402 logger.debug("Added new entry '%s' of project '%s' into db"
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
403 % (desc, project.desc))
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
404
362
90c09cca49c3 Fix loading entries of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 353
diff changeset
405 project.load_entries()
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
406 finally:
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
407 close(cur)
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
408
499
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
409 def insert_recover(self, project, stop_time, desc):
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
410 if project is None:
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
411 return
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
412 cur = None
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
413 try:
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
414 cur = self.con.cursor()
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
415 cur.execute(INSERT_RECOVER, (
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
416 project.id, project.start, stop_time, desc))
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
417 self.con.commit()
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
418 finally:
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
419 close(cur)
199b3e3657aa Every minute the time of the current entry is saved.
Magnus Schieder <mschieder@intevation.de>
parents: 467
diff changeset
420
52
9cb57c37369a fix insert new project sql statemenent and api
Björn Ricks <bjoern.ricks@intevation.de>
parents: 50
diff changeset
421 def insert_project(self, key, description):
9cb57c37369a fix insert new project sql statemenent and api
Björn Ricks <bjoern.ricks@intevation.de>
parents: 50
diff changeset
422 if key is None or description is None:
49
062ce001abd1 add backend method to create new projects
Björn Ricks <bjoern.ricks@intevation.de>
parents: 40
diff changeset
423 return
062ce001abd1 add backend method to create new projects
Björn Ricks <bjoern.ricks@intevation.de>
parents: 40
diff changeset
424 cur = None
50
9dbb6ee443a4 don't forget to save before commit :-/
Björn Ricks <bjoern.ricks@intevation.de>
parents: 49
diff changeset
425 try:
49
062ce001abd1 add backend method to create new projects
Björn Ricks <bjoern.ricks@intevation.de>
parents: 40
diff changeset
426 cur = self.con.cursor()
52
9cb57c37369a fix insert new project sql statemenent and api
Björn Ricks <bjoern.ricks@intevation.de>
parents: 50
diff changeset
427 cur.execute(INSERT_PROJECT, (key, description))
49
062ce001abd1 add backend method to create new projects
Björn Ricks <bjoern.ricks@intevation.de>
parents: 40
diff changeset
428 self.con.commit()
52
9cb57c37369a fix insert new project sql statemenent and api
Björn Ricks <bjoern.ricks@intevation.de>
parents: 50
diff changeset
429 logger.debug("Added a new project '%s' into db" % description)
50
9dbb6ee443a4 don't forget to save before commit :-/
Björn Ricks <bjoern.ricks@intevation.de>
parents: 49
diff changeset
430 finally:
9dbb6ee443a4 don't forget to save before commit :-/
Björn Ricks <bjoern.ricks@intevation.de>
parents: 49
diff changeset
431 close(cur)
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
432
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
433 def delete_entries(self, entries):
135
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
434 if entries is None:
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
435 return
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
436
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
437 cur = None
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
438 try:
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
439 cur = self.con.cursor()
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
440 for entry in 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
441 cur.execute(DELETE_PROJECT_ENTRY % entry.id)
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
442 logger.debug("Deleted entry: %s (%d)" % (entry.desc, entry.id))
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
443 self.con.commit()
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
444 finally:
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
445 close(cur)
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
446
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
447 def move_entries(self, entries, new_project_id):
135
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
448 if entries is None or new_project_id is None:
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
449 return
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
450
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
451 cur = None
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
452 try:
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
453 cur = self.con.cursor()
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
454 for entry in 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
455 cur.execute(MOVE_ENTRY, (new_project_id, entry.id))
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
456 logger.debug("Moved entry '%s' (id=%d) to project with id %d."
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
457 % (entry.desc, entry.id, new_project_id))
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
458 self.con.commit()
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
459 finally:
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
460 close(cur)
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
461
68
a25dab413260 Add new method to update a description of an entry in the database backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 52
diff changeset
462 def update_entry(self, entry):
a25dab413260 Add new method to update a description of an entry in the database backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 52
diff changeset
463 if not entry:
a25dab413260 Add new method to update a description of an entry in the database backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 52
diff changeset
464 return
a25dab413260 Add new method to update a description of an entry in the database backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 52
diff changeset
465
a25dab413260 Add new method to update a description of an entry in the database backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 52
diff changeset
466 cur = None
a25dab413260 Add new method to update a description of an entry in the database backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 52
diff changeset
467 try:
a25dab413260 Add new method to update a description of an entry in the database backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 52
diff changeset
468 cur = self.con.cursor()
401
a487535e7844 Adjusting starting datetime:
Bernhard Reiter <bernhard@intevation.de>
parents: 362
diff changeset
469 cur.execute(UPDATE_ENTRY, (entry.desc, entry.start,
a487535e7844 Adjusting starting datetime:
Bernhard Reiter <bernhard@intevation.de>
parents: 362
diff changeset
470 entry.end, entry.id))
68
a25dab413260 Add new method to update a description of an entry in the database backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 52
diff changeset
471 self.con.commit()
401
a487535e7844 Adjusting starting datetime:
Bernhard Reiter <bernhard@intevation.de>
parents: 362
diff changeset
472 logger.debug("Updated entry to: '%s'" % (entry))
68
a25dab413260 Add new method to update a description of an entry in the database backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 52
diff changeset
473 finally:
a25dab413260 Add new method to update a description of an entry in the database backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 52
diff changeset
474 close(cur)
a25dab413260 Add new method to update a description of an entry in the database backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 52
diff changeset
475
174
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
476 def update_project(self, project):
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
477 if not project:
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
478 return
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
479
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
480 cur = None
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
481 try:
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
482 cur = self.con.cursor()
348
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
483 cur.execute(
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
484 UPDATE_PROJECT, (project.key, project.desc, project.id))
174
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
485 self.con.commit()
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
486 logger.debug("Updated project: (%d) %s %s" % (project.id,
349
c97a4b6a6887 Fix textwidth <= 80
Björn Ricks <bjoern.ricks@intevation.de>
parents: 348
diff changeset
487 project.key,
c97a4b6a6887 Fix textwidth <= 80
Björn Ricks <bjoern.ricks@intevation.de>
parents: 348
diff changeset
488 project.desc))
174
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
489 finally:
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
490 close(cur)
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
491
348
328ec66517b0 Fix coding style for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 284
diff changeset
492
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
493 def close(cur):
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
494 """ This function closes a database cursor if it is existing """
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
495 if cur:
135
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
496 try:
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
497 cur.close()
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
498 except:
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
499 logger.warn("could not close database cursor.")
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
500
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
501 # 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)