def get_session(id): db = Database() db.query_params('SELECT * FROM sessions WHERE facebook_id = %s', (id, )) rs = db.result_set() db.close() if len(rs) != 0: session = rs[0] return Session(session[1], session[2], session[3], session[4], session[5], session[6], session[7]) else: return None
def get_client(id): db = Database() db.query_params('SELECT client_context, last_message FROM clients WHERE facebook_id = %s', (id,)) rs = db.result_set() db.close() if len(rs) != 0: if rs[0][0] is None: return Client(id, None, None) else: return Client(id, json.loads(rs[0][0]), rs[0][1]) else: return None
def update_session(id, credentials): db = Database() db.query_params( 'UPDATE sessions SET ' + 'token = %s, token_uri = %s, ' + 'client_id = %s, client_secret = %s ' + 'WHERE facebook_id = %s', ( credentials.token, credentials.token_uri, credentials.client_id, credentials.client_secret, id, )) db.close()
def insert_session(id, state, credentials): db = Database() db.query_params( 'INSERT INTO sessions (' + 'facebook_id, state, token, refresh_token, ' + 'token_uri, client_id, client_secret) ' + 'VALUES(%s, %s, %s, %s, %s, %s, %s)', ( id, state, credentials.token, credentials.refresh_token, credentials.token_uri, credentials.client_id, credentials.client_secret, )) db.close()
from src.DB.Database import Database def create_tables(db): db.query('CREATE TABLE IF NOT EXISTS clients (' + 'client_id SERIAL PRIMARY KEY, ' + 'facebook_id BIGINT NOT NULL, ' + 'client_context VARCHAR(1024), ' + 'last_message VARCHAR(128));') db.query('CREATE TABLE IF NOT EXISTS sessions (' + 'id SERIAL PRIMARY KEY, ' 'facebook_id BIGINT NOT NULL, ' + 'state VARCHAR(512) NOT NULL, ' + 'token VARCHAR(512), ' + 'refresh_token VARCHAR(512), ' + 'token_uri VARCHAR(512), ' + 'client_id VARCHAR(512), ' + 'client_secret VARCHAR(512));') if __name__ == "__main__": db = Database() create_tables(db) db.close()
def end_context(id): db = Database() db.query_params('UPDATE clients SET client_context = NULL WHERE facebook_id = %s', (id,)) db.close()
def update_client_last_msg(id, msg): db = Database() db.query_params('UPDATE clients SET last_message = %s WHERE facebook_id = %s', (msg, id,)) db.close()
def update_client_context(id, context): db = Database() db.query_params('UPDATE clients SET client_context = %s WHERE facebook_id = %s', (context, id,)) db.close()
def insert_client(id, context): db = Database() db.query_params('INSERT INTO clients (facebook_id, client_context) VALUES (%s, %s)', (id, context,)) db.close() return Client(id, context, None)