67 lines
1.4 KiB
Python
67 lines
1.4 KiB
Python
|
|
import sqlite3
|
|
import os
|
|
from datetime import datetime
|
|
|
|
HISTORY_DT_FMT = '%Y-%m-%d %H:%M:%S'
|
|
|
|
class TaskRef:
|
|
def __init__(self, taskid, dt):
|
|
self.taskid = taskid
|
|
self.dt = dt
|
|
|
|
class DataStore:
|
|
def __init__(self, dbfile):
|
|
if not os.path.exists(dbfile):
|
|
self.createdb(dbfile)
|
|
self.conn = sqlite3.connect(dbfile)
|
|
|
|
def __del__(self):
|
|
self.conn.close()
|
|
|
|
def createdb(self, dbfile):
|
|
conn = sqlite3.connect(dbfile)
|
|
c = conn.cursor()
|
|
c.execute('''
|
|
CREATE TABLE tasks (
|
|
id INTEGER PRIMARY KEY,
|
|
name TEXT,
|
|
longname TEXT,
|
|
parentid INTEGER,
|
|
FOREIGN KEY (parentid) REFERENCES tasks(id)
|
|
)''')
|
|
c.execute('''
|
|
CREATE TABLE entries (
|
|
date TEXT,
|
|
taskid INTEGER,
|
|
seconds INTEGER,
|
|
PRIMARY KEY (date, taskid),
|
|
FOREIGN KEY (taskid) REFERENCES tasks(id)
|
|
)''')
|
|
c.execute('''
|
|
CREATE TABLE history (
|
|
id INTEGER PRIMARY KEY,
|
|
taskid INTEGER,
|
|
datetime TEXT,
|
|
FOREIGN KEY (taskid) REFERENCES tasks(id)
|
|
)''')
|
|
conn.commit()
|
|
c.close()
|
|
conn.close()
|
|
|
|
def getCurrentTask(self):
|
|
c = self.conn.cursor()
|
|
c.execute('''
|
|
SELECT taskid, datetime
|
|
FROM history
|
|
WHERE id = 0
|
|
''')
|
|
ct = None
|
|
for row in c:
|
|
taskid = row[0]
|
|
dt = datetime.strptime(row[1], HISTORY_DT_FMT)
|
|
ct = TaskRef(taskid, dt)
|
|
c.close()
|
|
self.conn.commit()
|
|
return ct
|