diff options
-rw-r--r-- | item.py | 23 | ||||
-rw-r--r-- | itemList.py | 2 | ||||
-rw-r--r-- | storage/factory.py | 2 | ||||
-rw-r--r-- | storage/sqlite.py | 62 | ||||
-rw-r--r-- | storage/storageBase.py | 4 | ||||
-rwxr-xr-x | todolist.py | 12 |
6 files changed, 88 insertions, 17 deletions
@@ -1,16 +1,25 @@ +# -*- coding: utf-8 -*- + class item: readOnly = ['id'] - def __init__(self, title, created, priority): - self.id = -1 - self.title = title - self.created = created - self.priority = priority + def __init__(self, title=None, created=None, priority=None, row=None): + if row == None: + self.__dict__['id'] = -1 + self.title = title + self.created = created + self.priority = priority + else: + self.__init__(row[1], row[2], row[3]) + self.__dict__['id'] = row[0] def setId(self, id): if self.id == -1: - self.id = id - + self.__dict__['id'] = id + + def getId(self): + return self.id + def getTitle(self): return self.title diff --git a/itemList.py b/itemList.py index 527c837..6252946 100644 --- a/itemList.py +++ b/itemList.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + class itemList: def __init__(self, observer): self.data = [] diff --git a/storage/factory.py b/storage/factory.py index c1fa049..024c045 100644 --- a/storage/factory.py +++ b/storage/factory.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + from sqlite import * def getStorage(): diff --git a/storage/sqlite.py b/storage/sqlite.py index 8a24858..be3db7f 100644 --- a/storage/sqlite.py +++ b/storage/sqlite.py @@ -1,17 +1,75 @@ +# -*- coding: utf-8 -*- + from storageBase import storageBase from itemList import itemList +from item import item from pysqlite2 import dbapi2 as sqliteBackend class sqlite(storageBase): + dbVersion = '0.1' + def __init__(self): self.con = sqliteBackend.connect(self.getConfigDir() + '/data.sqlite') + self.cur = self.con.cursor() + + # wenn todo tabelle noch nicht exsistiert, dann sollten wir sie anlegen + cur = self.cur.execute("select name from sqlite_master where type='table';") + tables = cur.fetchall() + if ('todo',) not in tables or ('control',) not in tables: + self.init_db() + + # überprüfung ob die db-version identisch ist + version = self.cur.execute("select value from control where setting='db-version'").fetchone() + if (sqlite.dbVersion,) != version: + # TODO: update not init + self.init_db() + + + + def init_db(self): + print '(re)create todo table...' + self.cur.execute('drop table if exists todo') + self.cur.execute('''create table todo ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT(255), + createdAt INTEGER, + priority INTEGER, + desc BLOB + )''') + + self.cur.execute('drop table if exists control') + self.cur.execute('''create table control ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + setting TEXT, + value TEXT + )''') + + self.cur.execute("insert into control (setting, value) VALUES ('db-version', ?)", (sqlite.dbVersion,)) + self.con.commit() def __del__(self): self.con.close() def load(self): - return itemList(self) + items = itemList(self) + todos = self.cur.execute('select * from todo').fetchall() + for todo in todos: + items += item(row=todo) + + return items def notifyChange(self, sender): - print '%s %s' % ('Save changes:', sender.getCreatedAt()) + if sender.getId() >= 0: + print sender.getId() + self.cur.execute('update todo set title=?, createdAt=?, priority=? where id=?', + (sender.getTitle(), sender.getCreatedAt(), sender.getPriority(), sender.getId())) + self.con.commit() + else: + self.cur.execute('insert into todo (title, createdAt, priority) VALUES (?, ?, ?)', + (sender.getTitle(), sender.getCreatedAt(), sender.getPriority())) + self.con.commit() + sender.setId(self.cur.execute('select last_insert_rowid()').fetchone()[0]) + + + print '%s %s' % ('Save changes:', sender.getId()) return diff --git a/storage/storageBase.py b/storage/storageBase.py index 24abd2c..064c8a3 100644 --- a/storage/storageBase.py +++ b/storage/storageBase.py @@ -1,10 +1,12 @@ +# -*- coding: utf-8 -*- + import os class storageBase: def notifyChange(self, sender): abstract def getConfigDir(self): - dir = os.path.expanduser('~/.todolist/blub') + dir = os.path.expanduser('~/.todolist/') if not os.path.exists(dir): os.makedirs(dir) diff --git a/todolist.py b/todolist.py index 141f5d1..c062bbf 100755 --- a/todolist.py +++ b/todolist.py @@ -1,27 +1,25 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- import storage.factory import item +import random from time import time def main(): store = storage.factory.getStorage() items = store.load() - items += item.item("Test1", time(), 0) - items += item.item("Test1", time(), 0) - items += item.item("Test1", time(), 0) + items += item.item('asda', 12345678, 231) for i in items: print i.getTitle() print i.getCreatedAt() print i.getPriority() print '========================================' + i.priority = random.randint(0,10) - items[len(items)-1].id = 'new Title' - items[len(items)-1].id = 'new Title' - return - + if __name__ == '__main__': main() |