annotate getan/backend.py @ 284:561441fde2ac

Set unique constraint on project key Thanks to Tom Gottfried
author Björn Ricks <bjoern.ricks@intevation.de>
date Tue, 16 Apr 2013 11:51:35 +0200
parents 36b9f2d31810
children 328ec66517b0
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>
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
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 #
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
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 import logging
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
13 import os
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
14
33
f96a18c10836 Made getan python-2.4 compatible.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 23
diff changeset
15 try:
f96a18c10836 Made getan python-2.4 compatible.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 23
diff changeset
16 import sqlite3 as db
f96a18c10836 Made getan python-2.4 compatible.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 23
diff changeset
17 except ImportError:
f96a18c10836 Made getan python-2.4 compatible.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 23
diff changeset
18 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
19
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
20 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
21
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 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
23
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
24 CREATE_TABLES = [
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
25 """
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
26 CREATE TABLE projects (
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
27 id INTEGER PRIMARY KEY AUTOINCREMENT,
284
561441fde2ac Set unique constraint on project key
Björn Ricks <bjoern.ricks@intevation.de>
parents: 174
diff changeset
28 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
29 description VARCHAR(256),
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
30 active BOOLEAN DEFAULT 1
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
31 )
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
32 """,
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
33 """
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
34 CREATE TABLE entries (
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
35 id INTEGER PRIMARY KEY AUTOINCREMENT,
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
36 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
37 start_time TIMESTAMP NOT NULL,
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
38 stop_time TIMESTAMP NOT NULL,
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
39 description VARCHAR(256),
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
41 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
42 )
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 ]
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
45
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
46 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
47 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
48 FROM projects LEFT JOIN
144
9e853f9e63ad Remove whitespace
Björn Ricks <bjoern.ricks@intevation.de>
parents: 143
diff changeset
49 (SELECT
9e853f9e63ad Remove whitespace
Björn Ricks <bjoern.ricks@intevation.de>
parents: 143
diff changeset
50 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
51 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
52 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
53 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
54 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
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
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 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
58 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
59 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
60 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
61 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
62 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
63 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
64 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
65 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
66 WHERE
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
67 project_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
68 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
69 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
70 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
71 '''
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
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 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
74 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
75 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
76 '''
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
49
062ce001abd1 add backend method to create new projects
Björn Ricks <bjoern.ricks@intevation.de>
parents: 40
diff changeset
78 INSERT_PROJECT = '''
52
9cb57c37369a fix insert new project sql statemenent and api
Björn Ricks <bjoern.ricks@intevation.de>
parents: 50
diff changeset
79 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
80 '''
062ce001abd1 add backend method to create new projects
Björn Ricks <bjoern.ricks@intevation.de>
parents: 40
diff changeset
81
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
82 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
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 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
85
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
86 UPDATE_ENTRY = 'UPDATE entries SET description = ? WHERE id = ?'
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
87
174
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
88 UPDATE_PROJECT = "UPDATE projects SET key = ?, description = ? WHERE id = ?"
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
89
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 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
91
165
83ba64c9a51e A project entry in the db must have a key
Björn Ricks <bjoern.ricks@intevation.de>
parents: 146
diff changeset
92 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
93 pass
83ba64c9a51e A project entry in the db must have a key
Björn Ricks <bjoern.ricks@intevation.de>
parents: 146
diff changeset
94
143
9f4da22fb1f1 Use new style class for backend
Björn Ricks <bjoern.ricks@intevation.de>
parents: 135
diff changeset
95 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
96
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 def __init__(self, database = DEFAULT_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
98 self.database = database
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
99 self.ensure_exists()
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
100 self.con = db.connect(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
101 detect_types=db.PARSE_DECLTYPES |
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
102 db.PARSE_COLNAMES)
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 self.con.text_factory = lambda x: unicode(x, "utf-8", "ignore")
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
104
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
40
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
106 def ensure_exists(self):
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
107 """ Creates the database file if it does not exist. """
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
108 if os.path.isfile(self.database): return
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
109
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
110 con, cur = None, None
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
111 try:
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
112 con = db.connect(self.database);
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
113 cur = con.cursor()
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
114 try:
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
115 for sql in CREATE_TABLES:
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
116 cur.execute(sql)
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
117 con.commit()
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
118 except:
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
119 con.rollback()
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
120 raise
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
121 finally:
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
122 if cur: cur.close()
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
123 if con: con.close()
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
124
e4759cc8f5e7 Create database file if it does not exist.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 33
diff changeset
125
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
126 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
127 """ 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
128 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
129 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
130 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
131 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
132 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
133
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
134 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
135 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
136 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
137
135
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
138 if not row:
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
139 break
165
83ba64c9a51e A project entry in the db must have a key
Björn Ricks <bjoern.ricks@intevation.de>
parents: 146
diff changeset
140 # check key
83ba64c9a51e A project entry in the db must have a key
Björn Ricks <bjoern.ricks@intevation.de>
parents: 146
diff changeset
141 if not row[1]:
83ba64c9a51e A project entry in the db must have a key
Björn Ricks <bjoern.ricks@intevation.de>
parents: 146
diff changeset
142 raise InvalidProjectKeyError("Project with id %s needs " \
83ba64c9a51e A project entry in the db must have a key
Björn Ricks <bjoern.ricks@intevation.de>
parents: 146
diff changeset
143 "a key" % row[0])
135
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
144 proj = Project(*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
145 proj.entries = self.load_entries(proj.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
146 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
147
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
148 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
149 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
150
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
151 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
152 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
153
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
154
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
155 def load_entries(self, 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
156 """ 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
157 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
158 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
159 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
160 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
161 cur.execute(LOAD_PROJECT_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
162
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
163 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
164 while True:
146
bb70dde875fd Always fail if an entry couldn't be loaded
Björn Ricks <bjoern.ricks@intevation.de>
parents: 144
diff changeset
165 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
166
146
bb70dde875fd Always fail if an entry couldn't be loaded
Björn Ricks <bjoern.ricks@intevation.de>
parents: 144
diff changeset
167 if not row:
bb70dde875fd Always fail if an entry couldn't be loaded
Björn Ricks <bjoern.ricks@intevation.de>
parents: 144
diff changeset
168 break
bb70dde875fd Always fail if an entry couldn't be loaded
Björn Ricks <bjoern.ricks@intevation.de>
parents: 144
diff changeset
169 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
170
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
171 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
172 % (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
173 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
174 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
175 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
176
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
177
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
178 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
179 if project is None:
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
180 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
181 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
182 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
183 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
184 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
185 project.id, project.start, stop_time, 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
186 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
187 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
188 % (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
189
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
190 project.entries = self.load_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
191 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
192 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
193
52
9cb57c37369a fix insert new project sql statemenent and api
Björn Ricks <bjoern.ricks@intevation.de>
parents: 50
diff changeset
194 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
195 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
196 return
062ce001abd1 add backend method to create new projects
Björn Ricks <bjoern.ricks@intevation.de>
parents: 40
diff changeset
197 cur = None
50
9dbb6ee443a4 don't forget to save before commit :-/
Björn Ricks <bjoern.ricks@intevation.de>
parents: 49
diff changeset
198 try:
49
062ce001abd1 add backend method to create new projects
Björn Ricks <bjoern.ricks@intevation.de>
parents: 40
diff changeset
199 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
200 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
201 self.con.commit()
52
9cb57c37369a fix insert new project sql statemenent and api
Björn Ricks <bjoern.ricks@intevation.de>
parents: 50
diff changeset
202 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
203 finally:
9dbb6ee443a4 don't forget to save before commit :-/
Björn Ricks <bjoern.ricks@intevation.de>
parents: 49
diff changeset
204 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
205
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
206 def delete_entries(self, entries):
135
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
207 if entries is None:
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
208 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
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 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
211 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
212 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
213 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
214 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
215 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
216 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
217 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
218 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
219
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
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 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
222 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
223 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
224
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
225 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
226 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
227 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
228 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
229 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
230 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
231 % (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
232 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
233 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
234 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
235
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
236 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
237 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
238 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
239
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
240 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
241 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
242 cur = self.con.cursor()
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
243 cur.execute(UPDATE_ENTRY, (entry.desc, entry.id))
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
244 self.con.commit()
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
245 logger.debug("Updated entry: '%s' (%d)" % (entry.desc, entry.id))
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
246 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
247 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
248
174
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
249 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
250 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
251 return
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
252
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
253 cur = None
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
254 try:
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
255 cur = self.con.cursor()
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
256 cur.execute(UPDATE_PROJECT, (project.key, project.desc, project.id))
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
257 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
258 logger.debug("Updated project: (%d) %s %s" % (project.id,
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
259 project.key, project.desc))
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
260 finally:
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
261 close(cur)
36b9f2d31810 Add backend method to update a project in the database
Björn Ricks <bjoern.ricks@intevation.de>
parents: 165
diff changeset
262
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
263 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
264 """ 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
265 if cur:
135
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
266 try:
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 97
diff changeset
267 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
268 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
269 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
270
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
271 # 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)