code cleanup

This commit is contained in:
Mateusz779 2023-04-18 14:51:04 +02:00
parent b5d0cc390a
commit 6bc3ba9ad7

132
db.py
View File

@ -2,6 +2,7 @@ import psycopg2
import config
import utils
def connect():
global cur, conn
try:
@ -48,12 +49,15 @@ def connect():
);""")
conn.commit()
def get_cur():
return conn.cursor()
def get_conn():
return conn
def get_one(sql, value):
connect()
with get_cur() as cur:
@ -63,6 +67,7 @@ def get_one(sql, value):
except:
return None
def add_conf_image(name, token):
connect()
with get_cur() as cur:
@ -72,50 +77,18 @@ def add_conf_image(name, token):
""", (name, token,))
conn.commit()
# def get_conf(sql,value):
# connect()
# with get_cur() as cur:
# cur.execute(sql,(value,))
# try:
# return cur.fetchone()[0]
# except:
# return None
def get_conf_image(token):
return get_one("SELECT image_name FROM image WHERE token = %s", token)
# connect()
# with get_cur() as cur:
# cur.execute("""
# SELECT image_name FROM image WHERE token = %s
# """,(token,))
# try:
# return cur.fetchone()[0]
# except:
# return None
def get_conf_id(token):
return get_one("SELECT id FROM image WHERE token = %s", token)
# connect()
# with get_cur() as cur:
# cur.execute("""
# SELECT id FROM image WHERE token = %s
# """,(token,))
# try:
# return cur.fetchone()[0]
# except:
# return None
def get_conf_id_name(name):
return get_one("SELECT id FROM image WHERE image_name = %s", name)
# connect()
# with get_cur() as cur:
# cur.execute("""
# SELECT id FROM image WHERE image_name = %s
# """,(name+".squashfs",))
# try:
# return cur.fetchone()[0]
# except:
# return None
def add_user(username, password):
connect()
@ -126,6 +99,7 @@ def add_user(username, password):
""", (username, utils.hash_password(password),))
conn.commit()
def get_user_pass(username, password):
connect()
with get_cur() as cur:
@ -137,30 +111,14 @@ def get_user_pass(username, password):
except:
return None
def get_user_byid(id):
return get_one("SELECT id FROM users WHERE id = %s", id)
# connect()
# with get_cur() as cur:
# cur.execute("""
# SELECT id FROM users WHERE id = %s
# """,(id,))
# try:
# return cur.fetchone()[0]
# except:
# return None
def get_user_bytoken(token):
return get_one("SELECT user_id FROM auth_tokens WHERE token = %s", token)
# connect()
# with get_cur() as cur:
# cur.execute("""
# SELECT user_id FROM auth_tokens WHERE token = %s
# """,(token,))
# try:
# return cur.fetchone()[0]
# except:
# return None
def add_auth_token(user_id):
token = utils.generate_auth_token()
@ -173,6 +131,7 @@ def add_auth_token(user_id):
conn.commit()
return token
def login(username, password):
user_id = get_user_pass(username, password)
if user_id is not None:
@ -202,35 +161,17 @@ def get_image_allocation(sql, value):
except:
return None
def get_image_allocation_time(token):
image_id = get_conf_id(token)
if image_id is None:
return None
return get_one("SELECT last_access_time FROM image_allocation WHERE image_id = %s", image_id)
# def get_image_allocation_time_imageid(image_id):
# get_image_allocation("SELECT last_access_time FROM image_allocation WHERE image_id = %s", image_id)
# connect()
# with get_cur() as cur:
# cur.execute("""
# SELECT last_access_time FROM image_allocation WHERE image_id = %s
# """,(image_id,))
# try:
# return cur.fetchone()[0]
# except:
# return None
def get_image_allocation_time_id(id):
get_one("SELECT last_access_time FROM image_allocation WHERE id = %s", id)
# connect()
# with get_cur() as cur:
# cur.execute("""
# SELECT last_access_time FROM image_allocation WHERE id = %s
# """,(id,))
# try:
# return cur.fetchone()[0]
# except:
# return None
def get_image_allocation_clientip(token):
id_image = get_conf_id(token)
@ -238,27 +179,11 @@ def get_image_allocation_clientip(token):
return None
return get_one("SELECT last_access_time FROM image_allocation WHERE id = %s", id_image)
# connect()
# with get_cur() as cur:
# cur.execute("""
# SELECT client_ip FROM image_allocation WHERE image_id = %s
# """,(id_image,))
# try:
# return cur.fetchone()[0]
# except:
# return None
def get_image_allocation_clientip_id(id):
return get_one("SELECT client_ip FROM image_allocation WHERE id = %s", id)
# connect()
# with get_cur() as cur:
# cur.execute("""
# SELECT client_ip FROM image_allocation WHERE id = %s
# """,(id,))
# try:
# return cur.fetchone()[0]
# except:
# return None
def set_image_allocation(token, client_ip):
id_image = get_conf_id(token)
@ -274,6 +199,7 @@ def set_image_allocation(token, client_ip):
conn.commit()
return token
def del_image_allocation_token(token):
id_image = get_conf_id(token)
if id_image is None:
@ -281,6 +207,7 @@ def del_image_allocation_token(token):
return del_image_allocation_id_image(id_image)
def del_image_allocation(sql, value):
connect()
with get_cur() as cur:
@ -291,31 +218,14 @@ def del_image_allocation(sql, value):
except:
return None
def del_image_allocation_id_image(image_id):
return del_image_allocation("DELETE FROM image_allocation WHERE image_id = %s", image_id)
# connect()
# with get_cur() as cur:
# cur.execute("""
# DELETE FROM image_allocation WHERE image_id = %s
# """,(id_image,))
# try:
# conn.commit()
# return True
# except:
# return None
def del_image_allocation_id(id):
return del_image_allocation("DELETE FROM image_allocation WHERE id = %s", id)
# connect()
# with get_cur() as cur:
# cur.execute("""
# DELETE FROM image_allocation WHERE id = %s
# """,(id,))
# try:
# conn.commit()
# return True
# except:
# return None
def update_image_allocation_time(id):
connect()