176 lines
4.1 KiB
Python
176 lines
4.1 KiB
Python
|
|
import sqlite3
|
|
import os
|
|
from datetime import datetime
|
|
|
|
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 projects (
|
|
id INTEGER PRIMARY KEY,
|
|
name TEXT
|
|
)''')
|
|
c.execute('''
|
|
CREATE TABLE tasks (
|
|
id INTEGER PRIMARY KEY,
|
|
projectid INTEGER,
|
|
name TEXT,
|
|
UNIQUE (projectid, name),
|
|
FOREIGN KEY (projectid) REFERENCES projects(id)
|
|
)''')
|
|
c.execute('''
|
|
CREATE TABLE hours (
|
|
date TEXT,
|
|
taskid INTEGER,
|
|
seconds INTEGER,
|
|
PRIMARY KEY (date, taskid),
|
|
FOREIGN KEY (taskid) REFERENCES tasks(id)
|
|
)''')
|
|
conn.commit()
|
|
c.close()
|
|
conn.close()
|
|
|
|
def getProjects(self):
|
|
c = self.conn.cursor()
|
|
c.execute('''
|
|
SELECT id, name
|
|
FROM projects
|
|
''')
|
|
projects = {}
|
|
for row in c:
|
|
projects[row[0]] = row[1]
|
|
c.close()
|
|
self.conn.commit()
|
|
return projects
|
|
|
|
def getTasks(self, projectid):
|
|
c = self.conn.cursor()
|
|
c.execute('''
|
|
SELECT id, name
|
|
FROM tasks
|
|
WHERE projectid = ?
|
|
''', (projectid,))
|
|
tasks = {}
|
|
for row in c:
|
|
tasks[row[0]] = row[1]
|
|
c.close()
|
|
self.conn.commit()
|
|
return tasks
|
|
|
|
def createProject(self, projectname):
|
|
c = self.conn.cursor()
|
|
projects = self.getProjects()
|
|
if projectname in projects.values():
|
|
return 0
|
|
nextid = self.getNextId('projects')
|
|
c.execute('''
|
|
INSERT INTO projects (id, name)
|
|
VALUES (?, ?)
|
|
''', (nextid, projectname))
|
|
c.close()
|
|
self.conn.commit()
|
|
return nextid
|
|
|
|
def createTask(self, projectid, taskname):
|
|
c = self.conn.cursor()
|
|
tasks = self.getTasks(projectid)
|
|
if taskname in tasks.values():
|
|
return False
|
|
projects = self.getProjects()
|
|
if not projectid in projects:
|
|
return False
|
|
nextid = self.getNextId('tasks')
|
|
c.execute('''
|
|
INSERT INTO tasks (id, projectid, name)
|
|
VALUES (?, ?, ?)
|
|
''', (nextid, projectid, taskname))
|
|
c.close()
|
|
self.conn.commit()
|
|
return nextid
|
|
|
|
def getNextId(self, table):
|
|
c = self.conn.cursor()
|
|
nextid = 1
|
|
c.execute('''
|
|
SELECT MAX(id)
|
|
FROM %s
|
|
''' % table)
|
|
for row in c:
|
|
if row[0]:
|
|
nextid = row[0] + 1
|
|
c.close()
|
|
return nextid
|
|
|
|
def getDailyHours(self, date):
|
|
hours = {}
|
|
c = self.conn.cursor()
|
|
c.execute('''
|
|
SELECT projects.id, tasks.id, hours.seconds
|
|
FROM projects, tasks, hours
|
|
WHERE hours.date = ?
|
|
AND hours.taskid = tasks.id
|
|
AND tasks.projectid = projects.id
|
|
''', (date,))
|
|
for row in c:
|
|
pid, tid, secs = row
|
|
if not pid in hours:
|
|
hours[pid] = {}
|
|
hours[pid][tid] = secs
|
|
c.close()
|
|
self.conn.commit()
|
|
return hours
|
|
|
|
def getProjectDailyHours(self, date):
|
|
hours = {}
|
|
c = self.conn.cursor()
|
|
c.execute('''
|
|
SELECT projects.id, SUM(hours.seconds)
|
|
FROM projects, tasks, hours
|
|
WHERE hours.date = ?
|
|
AND hours.taskid = tasks.id
|
|
AND tasks.projectid = projects.id
|
|
GROUP BY projects.id
|
|
''', (date,))
|
|
for row in c:
|
|
pid, secs = row
|
|
hours[pid] = secs
|
|
c.close()
|
|
self.conn.commit()
|
|
return hours
|
|
|
|
def setTaskHours(self, taskid, date, secs):
|
|
c = self.conn.cursor()
|
|
c.execute('''
|
|
UPDATE hours
|
|
SET seconds = ?
|
|
WHERE taskid = ?
|
|
AND date = ?
|
|
''', (secs, taskid, str(date)))
|
|
c.close()
|
|
self.conn.commit()
|
|
return secs
|
|
|
|
def addTaskHours(self, taskid, date, secs):
|
|
c = self.conn.cursor()
|
|
c.execute('''
|
|
SELECT seconds
|
|
FROM hours
|
|
WHERE taskid = ?
|
|
AND date = ?
|
|
''', (taskid, date))
|
|
for row in c:
|
|
secs += row[0]
|
|
c.close()
|
|
self.conn.commit()
|
|
return self.setTaskHours(taskid, date, secs)
|