upd history

This commit is contained in:
Андрей Владимирович 2016-03-12 18:25:16 +03:00
parent 465bcaf125
commit f1d6bbdb1f

View file

@ -4,6 +4,12 @@ from settings import Settings
import os import os
MESSAGE_OWNER = {
'ME': 0,
'FRIEND': 1
}
class History(object): class History(object):
def __init__(self, name): def __init__(self, name):
self._name = name self._name = name
@ -16,9 +22,9 @@ class History(object):
db.close() db.close()
def add_friend_to_db(self, tox_id): def add_friend_to_db(self, tox_id):
try:
os.chdir(Settings.get_default_path()) os.chdir(Settings.get_default_path())
db = sqlite3.connect(self._name + '.hstr') db = sqlite3.connect(self._name + '.hstr')
try:
cursor = db.cursor() cursor = db.cursor()
cursor.execute('INSERT INTO friends VALUES (?);', (tox_id, )) cursor.execute('INSERT INTO friends VALUES (?);', (tox_id, ))
cursor.execute('CREATE TABLE id' + tox_id + '(' cursor.execute('CREATE TABLE id' + tox_id + '('
@ -36,9 +42,9 @@ class History(object):
db.close() db.close()
def delete_friend_from_db(self, tox_id): def delete_friend_from_db(self, tox_id):
try:
os.chdir(Settings.get_default_path()) os.chdir(Settings.get_default_path())
db = sqlite3.connect(self._name + '.hstr') db = sqlite3.connect(self._name + '.hstr')
try:
cursor = db.cursor() cursor = db.cursor()
cursor.execute('DELETE FROM friends WHERE tox_id=?;', (tox_id, )) cursor.execute('DELETE FROM friends WHERE tox_id=?;', (tox_id, ))
cursor.execute('DROP TABLE id' + tox_id + ';') cursor.execute('DROP TABLE id' + tox_id + ';')
@ -50,9 +56,9 @@ class History(object):
db.close() db.close()
def save_messages_to_db(self, tox_id, messages_iter): def save_messages_to_db(self, tox_id, messages_iter):
try:
os.chdir(Settings.get_default_path()) os.chdir(Settings.get_default_path())
db = sqlite3.connect(self._name + '.hstr') db = sqlite3.connect(self._name + '.hstr')
try:
cursor = db.cursor() cursor = db.cursor()
cursor.executemany('INSERT INTO id' + tox_id + '(message, owner, unix_time) ' cursor.executemany('INSERT INTO id' + tox_id + '(message, owner, unix_time) '
'VALUES (?, ?, ?);', messages_iter) 'VALUES (?, ?, ?);', messages_iter)
@ -64,10 +70,9 @@ class History(object):
db.close() db.close()
def messages_getter(self, tox_id): def messages_getter(self, tox_id):
return MessageGetter(self._name, tox_id) return History.MessageGetter(self._name, tox_id)
class MessageGetter(object):
class MessageGetter(object):
def __init__(self, name, tox_id): def __init__(self, name, tox_id):
os.chdir(Settings.get_default_path()) os.chdir(Settings.get_default_path())
self._db = sqlite3.connect(name + '.hstr') self._db = sqlite3.connect(name + '.hstr')
@ -92,3 +97,4 @@ if __name__ == '__main__':
h = History('test') h = History('test')
getter = h.messages_getter('42') getter = h.messages_getter('42')
print getter.get_all() print getter.get_all()
print getter.get(5)