code cleanup

This commit is contained in:
Mateusz779 2023-04-18 14:30:56 +02:00
parent ccb1d4bc19
commit b5d0cc390a

102
db.py
View File

@ -54,6 +54,15 @@ def get_cur():
def get_conn(): def get_conn():
return conn return conn
def get_one(sql, value):
connect()
with get_cur() as cur:
cur.execute(sql,(value,))
try:
return cur.fetchone()[0]
except:
return None
def add_conf_image(name, token): def add_conf_image(name, token):
connect() connect()
with get_cur() as cur: with get_cur() as cur:
@ -63,17 +72,17 @@ def add_conf_image(name, token):
""",(name, token,)) """,(name, token,))
conn.commit() conn.commit()
def get_conf(sql,value): # def get_conf(sql,value):
connect() # connect()
with get_cur() as cur: # with get_cur() as cur:
cur.execute(sql,(value,)) # cur.execute(sql,(value,))
try: # try:
return cur.fetchone()[0] # return cur.fetchone()[0]
except: # except:
return None # return None
def get_conf_image(token): def get_conf_image(token):
return get_conf("SELECT image_name FROM image WHERE token = %s", token) return get_one("SELECT image_name FROM image WHERE token = %s", token)
# connect() # connect()
# with get_cur() as cur: # with get_cur() as cur:
# cur.execute(""" # cur.execute("""
@ -85,7 +94,7 @@ def get_conf_image(token):
# return None # return None
def get_conf_id(token): def get_conf_id(token):
return get_conf("SELECT id FROM image WHERE token = %s", token) return get_one("SELECT id FROM image WHERE token = %s", token)
# connect() # connect()
# with get_cur() as cur: # with get_cur() as cur:
# cur.execute(""" # cur.execute("""
@ -97,7 +106,7 @@ def get_conf_id(token):
# return None # return None
def get_conf_id_name(name): def get_conf_id_name(name):
return get_conf("SELECT id FROM image WHERE image_name = %s", name) return get_one("SELECT id FROM image WHERE image_name = %s", name)
# connect() # connect()
# with get_cur() as cur: # with get_cur() as cur:
# cur.execute(""" # cur.execute("""
@ -129,26 +138,29 @@ def get_user_pass(username, password):
return None return None
def get_user_byid(id): def get_user_byid(id):
connect() return get_one("SELECT id FROM users WHERE id = %s", id)
with get_cur() as cur: # connect()
cur.execute(""" # with get_cur() as cur:
SELECT id FROM users WHERE id = %s # cur.execute("""
""",(id,)) # SELECT id FROM users WHERE id = %s
try: # """,(id,))
return cur.fetchone()[0] # try:
except: # return cur.fetchone()[0]
return None # except:
# return None
def get_user_bytoken(token): def get_user_bytoken(token):
connect() return get_one("SELECT user_id FROM auth_tokens WHERE token = %s", token)
with get_cur() as cur:
cur.execute(""" # connect()
SELECT user_id FROM auth_tokens WHERE token = %s # with get_cur() as cur:
""",(token,)) # cur.execute("""
try: # SELECT user_id FROM auth_tokens WHERE token = %s
return cur.fetchone()[0] # """,(token,))
except: # try:
return None # return cur.fetchone()[0]
# except:
# return None
def add_auth_token(user_id): def add_auth_token(user_id):
token = utils.generate_auth_token() token = utils.generate_auth_token()
@ -194,11 +206,10 @@ def get_image_allocation_time(token):
image_id = get_conf_id(token) image_id = get_conf_id(token)
if image_id is None: if image_id is None:
return None return None
return get_one("SELECT last_access_time FROM image_allocation WHERE image_id = %s", image_id)
return get_image_allocation_time_imageid(image_id)
def get_image_allocation_time_imageid(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) # get_image_allocation("SELECT last_access_time FROM image_allocation WHERE image_id = %s", image_id)
# connect() # connect()
# with get_cur() as cur: # with get_cur() as cur:
# cur.execute(""" # cur.execute("""
@ -210,7 +221,7 @@ def get_image_allocation_time_imageid(image_id):
# return None # return None
def get_image_allocation_time_id(id): def get_image_allocation_time_id(id):
get_image_allocation("SELECT last_access_time FROM image_allocation WHERE id = %s", id) get_one("SELECT last_access_time FROM image_allocation WHERE id = %s", id)
# connect() # connect()
# with get_cur() as cur: # with get_cur() as cur:
# cur.execute(""" # cur.execute("""
@ -226,7 +237,7 @@ def get_image_allocation_clientip(token):
if id_image is None: if id_image is None:
return None return None
get_image_allocation("SELECT last_access_time FROM image_allocation WHERE id = %s", id_image) return get_one("SELECT last_access_time FROM image_allocation WHERE id = %s", id_image)
# connect() # connect()
# with get_cur() as cur: # with get_cur() as cur:
# cur.execute(""" # cur.execute("""
@ -237,16 +248,17 @@ def get_image_allocation_clientip(token):
# except: # except:
# return None # return None
def get_image_allocation_clientip_id(id): def get_image_allocation_clientip_id(id):
connect() return get_one("SELECT client_ip FROM image_allocation WHERE id = %s", id)
with get_cur() as cur: # connect()
cur.execute(""" # with get_cur() as cur:
SELECT client_ip FROM image_allocation WHERE id = %s # cur.execute("""
""",(id,)) # SELECT client_ip FROM image_allocation WHERE id = %s
try: # """,(id,))
return cur.fetchone()[0] # try:
except: # return cur.fetchone()[0]
return None # except:
# return None
def set_image_allocation(token, client_ip): def set_image_allocation(token, client_ip):
id_image = get_conf_id(token) id_image = get_conf_id(token)