From 465bcaf1255408ba6cd387cf301496e796614394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=92=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Sat, 12 Mar 2016 14:18:00 +0300 Subject: [PATCH 1/2] history --- src/history.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/history.py diff --git a/src/history.py b/src/history.py new file mode 100644 index 0000000..2208b63 --- /dev/null +++ b/src/history.py @@ -0,0 +1,94 @@ +# coding=utf-8 +import sqlite3 +from settings import Settings +import os + + +class History(object): + def __init__(self, name): + self._name = name + os.chdir(Settings.get_default_path()) + db = sqlite3.connect(name + '.hstr') + cursor = db.cursor() + cursor.execute('CREATE TABLE IF NOT EXISTS friends(' + ' tox_id TEXT PRIMARY KEY' + ')') + db.close() + + def add_friend_to_db(self, tox_id): + try: + os.chdir(Settings.get_default_path()) + db = sqlite3.connect(self._name + '.hstr') + cursor = db.cursor() + cursor.execute('INSERT INTO friends VALUES (?);', (tox_id, )) + cursor.execute('CREATE TABLE id' + tox_id + '(' + ' id INTEGER PRIMARY KEY,' + ' message TEXT,' + ' owner INTEGER,' + ' unix_time INTEGER' + ' message_type INTEGER' + ')') + db.commit() + except Exception as e: + db.rollback() + raise e + finally: + db.close() + + def delete_friend_from_db(self, tox_id): + try: + os.chdir(Settings.get_default_path()) + db = sqlite3.connect(self._name + '.hstr') + cursor = db.cursor() + cursor.execute('DELETE FROM friends WHERE tox_id=?;', (tox_id, )) + cursor.execute('DROP TABLE id' + tox_id + ';') + db.commit() + except Exception as e: + db.rollback() + raise e + finally: + db.close() + + def save_messages_to_db(self, tox_id, messages_iter): + try: + os.chdir(Settings.get_default_path()) + db = sqlite3.connect(self._name + '.hstr') + cursor = db.cursor() + cursor.executemany('INSERT INTO id' + tox_id + '(message, owner, unix_time) ' + 'VALUES (?, ?, ?);', messages_iter) + db.commit() + except Exception as e: + db.rollback() + raise e + finally: + db.close() + + def messages_getter(self, tox_id): + return MessageGetter(self._name, tox_id) + + +class MessageGetter(object): + def __init__(self, name, tox_id): + os.chdir(Settings.get_default_path()) + self._db = sqlite3.connect(name + '.hstr') + self._cursor = self._db.cursor() + self._cursor.execute('SELECT message, owner, unix_time FROM id' + tox_id + + ' ORDER BY unix_time DESC;') + + def get_one(self): + return self._cursor.fetchone() + + def get_all(self): + return self._cursor.fetchall() + + def get(self, count): + return self._cursor.fetchmany(count) + + def __del__(self): + self._db.close() + + +if __name__ == '__main__': + h = History('test') + getter = h.messages_getter('42') + print getter.get_all() From f1d6bbdb1ff73a5bce26f8dd09b4df07401fd597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=92=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Sat, 12 Mar 2016 18:25:16 +0300 Subject: [PATCH 2/2] upd history --- src/history.py | 52 ++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/history.py b/src/history.py index 2208b63..b04c918 100644 --- a/src/history.py +++ b/src/history.py @@ -4,6 +4,12 @@ from settings import Settings import os +MESSAGE_OWNER = { + 'ME': 0, + 'FRIEND': 1 +} + + class History(object): def __init__(self, name): self._name = name @@ -16,9 +22,9 @@ class History(object): db.close() def add_friend_to_db(self, tox_id): + os.chdir(Settings.get_default_path()) + db = sqlite3.connect(self._name + '.hstr') try: - os.chdir(Settings.get_default_path()) - db = sqlite3.connect(self._name + '.hstr') cursor = db.cursor() cursor.execute('INSERT INTO friends VALUES (?);', (tox_id, )) cursor.execute('CREATE TABLE id' + tox_id + '(' @@ -36,9 +42,9 @@ class History(object): db.close() def delete_friend_from_db(self, tox_id): + os.chdir(Settings.get_default_path()) + db = sqlite3.connect(self._name + '.hstr') try: - os.chdir(Settings.get_default_path()) - db = sqlite3.connect(self._name + '.hstr') cursor = db.cursor() cursor.execute('DELETE FROM friends WHERE tox_id=?;', (tox_id, )) cursor.execute('DROP TABLE id' + tox_id + ';') @@ -50,9 +56,9 @@ class History(object): db.close() def save_messages_to_db(self, tox_id, messages_iter): + os.chdir(Settings.get_default_path()) + db = sqlite3.connect(self._name + '.hstr') try: - os.chdir(Settings.get_default_path()) - db = sqlite3.connect(self._name + '.hstr') cursor = db.cursor() cursor.executemany('INSERT INTO id' + tox_id + '(message, owner, unix_time) ' 'VALUES (?, ?, ?);', messages_iter) @@ -64,31 +70,31 @@ class History(object): db.close() def messages_getter(self, tox_id): - return MessageGetter(self._name, tox_id) + return History.MessageGetter(self._name, tox_id) + class MessageGetter(object): + def __init__(self, name, tox_id): + os.chdir(Settings.get_default_path()) + self._db = sqlite3.connect(name + '.hstr') + self._cursor = self._db.cursor() + self._cursor.execute('SELECT message, owner, unix_time FROM id' + tox_id + + ' ORDER BY unix_time DESC;') -class MessageGetter(object): - def __init__(self, name, tox_id): - os.chdir(Settings.get_default_path()) - self._db = sqlite3.connect(name + '.hstr') - self._cursor = self._db.cursor() - self._cursor.execute('SELECT message, owner, unix_time FROM id' + tox_id + - ' ORDER BY unix_time DESC;') + def get_one(self): + return self._cursor.fetchone() - def get_one(self): - return self._cursor.fetchone() + def get_all(self): + return self._cursor.fetchall() - def get_all(self): - return self._cursor.fetchall() + def get(self, count): + return self._cursor.fetchmany(count) - def get(self, count): - return self._cursor.fetchmany(count) - - def __del__(self): - self._db.close() + def __del__(self): + self._db.close() if __name__ == '__main__': h = History('test') getter = h.messages_getter('42') print getter.get_all() + print getter.get(5)