Fixed some issues with (auto) beginning and ending.
2 Database lib for cronrec.
3 Author: Eugen Sawin (sawine@me73.com)
4 Dependencies: libsqlite3-dev
9 ACTIVITIES_TABLE = """activities(
13 COMPANIES_TABLE = """companies(
21 PROJECTS_TABLE = """projects(
25 constraint customer_fk foreign key(customer) references companies(rowid) on delete
28 RATES_TABLE = """rates(
33 constraint project_fk foreign key(project) references projects(rowid) on delete
35 constraint activity_fk foreign key(activity) references activities(rowid) on delete
38 TASKS_TABLE = """tasks(
44 constraint project_fk foreign key(project) references projects(rowid) on delete
46 constraint activity_fk foreign key(activity) references activities(rowid) on delete
49 TABLES = (ACTIVITIES_TABLE, COMPANIES_TABLE, PROJECTS_TABLE, RATES_TABLE,
52 DEF_PROJECT = "default"
53 DEF_ACTIVITY = "default"
56 con = sqlite3.connect(db_file)
57 return con, con.cursor()
60 con, cur = session(db_file)
61 cur.execute("select * from projects")
65 cur.execute("select * from activities")
69 cur.execute("select * from tasks")
77 con, cur = session(db_file)
79 sql = "create table " + table
84 def find_last_project(db_file, time):
85 con, cur = session(db_file)
87 sql = "select project from tasks where begin < ? and end is null order by \
89 cur.execute(sql, values)
93 sql = "select name from projects where rowid=?"
94 cur.execute(sql, values)
95 project = cur.fetchone()[0]
97 project = None#DEF_PROJECT
102 def find_last_activity(db_file, time):
103 con, cur = session(db_file)
105 sql = "select activity from tasks where begin < ? and end is null order by \
107 cur.execute(sql, values)
111 sql = "select name from activities where rowid=?"
112 cur.execute(sql, values)
113 activity = cur.fetchone()[0]
115 activity = None#DEF_ACTIVITY
120 def row_id(db_file, table, name):
121 con, cur = session(db_file)
123 sql = "select rowid from %s where name=?" % table
124 cur.execute(sql, values)
127 sql = "insert into %s(name) values(?)" % table
128 cur.execute(sql, values)
131 return row_id(db_file, table, name)
136 def project_id(db_file, name):
137 return row_id(db_file, "projects", name)
139 def activity_id(db_file, name):
140 return row_id(db_file, "activities", name)
142 def end(db_file, project, activity, time):
143 print "ending: %s:%s" % (project, activity)
144 if activity or project:
146 activity = find_last_activity(db_file, time)
148 project = find_last_project(db_file, time)
149 con, cur = session(db_file)
150 values = (time, project_id(db_file, project), activity_id(db_file, activity))
151 sql = "update tasks set end=? where project=? and activity=?"
152 cur.execute(sql, values)
157 def begin(db_file, project, activity, time):
158 print "beginning: %s:%s" % (project, activity)
159 last_project = find_last_project(db_file, time)
160 last_activity = find_last_activity(db_file, time)
162 project = last_project
164 activity = DEF_ACTIVITY
165 if activity != last_activity or project != last_project:
166 end(db_file, last_project, last_activity, time)
167 con, cur = session(db_file)
168 values = (time, None, project_id(db_file, project),
169 activity_id(db_file, activity), None)
170 sql = "insert into tasks values(?, ?, ?, ?, ?)"
171 cur.execute(sql, values)