upd history

This commit is contained in:
Андрей Владимирович 2016-03-12 20:37:54 +03:00
parent fb26b02cd2
commit 31ccd99d0e

View file

@ -1,7 +1,7 @@
# coding=utf-8 # coding=utf-8
import sqlite3 from sqlite3 import connect
from settings import Settings from settings import Settings
import os from os import chdir
MESSAGE_OWNER = { MESSAGE_OWNER = {
@ -13,8 +13,8 @@ MESSAGE_OWNER = {
class History(object): class History(object):
def __init__(self, name): def __init__(self, name):
self._name = name self._name = name
os.chdir(Settings.get_default_path()) chdir(Settings.get_default_path())
db = sqlite3.connect(name + '.hstr') db = connect(name + '.hstr')
cursor = db.cursor() cursor = db.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS friends(' cursor.execute('CREATE TABLE IF NOT EXISTS friends('
' tox_id TEXT PRIMARY KEY' ' tox_id TEXT PRIMARY KEY'
@ -22,8 +22,8 @@ class History(object):
db.close() db.close()
def add_friend_to_db(self, tox_id): def add_friend_to_db(self, tox_id):
os.chdir(Settings.get_default_path()) chdir(Settings.get_default_path())
db = sqlite3.connect(self._name + '.hstr') db = connect(self._name + '.hstr')
try: try:
cursor = db.cursor() cursor = db.cursor()
cursor.execute('INSERT INTO friends VALUES (?);', (tox_id, )) cursor.execute('INSERT INTO friends VALUES (?);', (tox_id, ))
@ -31,7 +31,7 @@ class History(object):
' id INTEGER PRIMARY KEY,' ' id INTEGER PRIMARY KEY,'
' message TEXT,' ' message TEXT,'
' owner INTEGER,' ' owner INTEGER,'
' unix_time INTEGER' ' unix_time INTEGER,'
' message_type INTEGER' ' message_type INTEGER'
')') ')')
db.commit() db.commit()
@ -42,8 +42,8 @@ class History(object):
db.close() db.close()
def delete_friend_from_db(self, tox_id): def delete_friend_from_db(self, tox_id):
os.chdir(Settings.get_default_path()) chdir(Settings.get_default_path())
db = sqlite3.connect(self._name + '.hstr') db = connect(self._name + '.hstr')
try: 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, ))
@ -56,8 +56,8 @@ class History(object):
db.close() db.close()
def friend_exists_in_db(self, tox_id): def friend_exists_in_db(self, tox_id):
os.chdir(Settings.get_default_path()) chdir(Settings.get_default_path())
db = sqlite3.connect(self._name + '.hstr') db = connect(self._name + '.hstr')
cursor = db.cursor() cursor = db.cursor()
cursor.execute('SELECT 0 FROM friends WHERE tox_id=?', (tox_id, )) cursor.execute('SELECT 0 FROM friends WHERE tox_id=?', (tox_id, ))
result = cursor.fetchone() result = cursor.fetchone()
@ -65,12 +65,25 @@ class History(object):
return result is not None return result is not None
def save_messages_to_db(self, tox_id, messages_iter): def save_messages_to_db(self, tox_id, messages_iter):
os.chdir(Settings.get_default_path()) chdir(Settings.get_default_path())
db = sqlite3.connect(self._name + '.hstr') db = connect(self._name + '.hstr')
try: 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, message_type) '
'VALUES (?, ?, ?);', messages_iter) 'VALUES (?, ?, ?, ?);', messages_iter)
db.commit()
except:
db.rollback()
raise
finally:
db.close()
def delete_messages(self, tox_id):
chdir(Settings.get_default_path())
db = connect(self._name + '.hstr')
try:
cursor = db.cursor()
cursor.execute('DELETE FROM id' + tox_id + ';')
db.commit() db.commit()
except: except:
db.rollback() db.rollback()
@ -83,10 +96,10 @@ class History(object):
class MessageGetter(object): class MessageGetter(object):
def __init__(self, name, tox_id): def __init__(self, name, tox_id):
os.chdir(Settings.get_default_path()) chdir(Settings.get_default_path())
self._db = sqlite3.connect(name + '.hstr') self._db = connect(name + '.hstr')
self._cursor = self._db.cursor() self._cursor = self._db.cursor()
self._cursor.execute('SELECT message, owner, unix_time FROM id' + tox_id + self._cursor.execute('SELECT message, owner, unix_time, message_type FROM id' + tox_id +
' ORDER BY unix_time DESC;') ' ORDER BY unix_time DESC;')
def get_one(self): def get_one(self):
@ -104,5 +117,7 @@ class History(object):
if __name__ == '__main__': if __name__ == '__main__':
h = History('test') h = History('test')
getter = h.messages_getter('42') if not h.friend_exists_in_db('42'):
print h.friend_exists_in_db('42'), type(h.friend_exists_in_db('42')) h.add_friend_to_db('42')
h.save_messages_to_db('42', [('bugara', 1, 123412413, 1)])
h.delete_messages('42')