add refresh-milestones to cli. fix cli errors

This commit is contained in:
Lili (Tlapka) 2023-03-15 14:11:11 +01:00
parent c203ce132f
commit cf8376eebc
3 changed files with 29 additions and 20 deletions

View File

@ -52,6 +52,7 @@ def create_app(test_config=None):
app.cli.add_command(db.clear_queue_command) app.cli.add_command(db.clear_queue_command)
app.cli.add_command(db.refresh_counters_command) app.cli.add_command(db.refresh_counters_command)
app.cli.add_command(db.refresh_and_clear_command) app.cli.add_command(db.refresh_and_clear_command)
app.cli.add_command(db.refresh_milestones_command)
# scheduler job for giving points to users # scheduler job for giving points to users
def proxy_job(): def proxy_job():

View File

@ -32,7 +32,7 @@ def insert_counters(db):
(redeem,) (redeem,)
) )
db.commit() db.commit()
except Error as e: except sqlite3.Error as e:
print("Failed inserting counters to db:", e.args[0]) print("Failed inserting counters to db:", e.args[0])
@ -56,7 +56,7 @@ def clear_redeem_queue():
"""UPDATE counters SET count = 0""" """UPDATE counters SET count = 0"""
) )
db.commit() db.commit()
except Error as e: except sqlite3.Error as e:
print("Error occured deleting redeem queue:", e.args[0]) print("Error occured deleting redeem queue:", e.args[0])
@ -66,7 +66,7 @@ def refresh_counters():
try: try:
db.execute("DELETE FROM counters") db.execute("DELETE FROM counters")
db.commit() db.commit()
except Error as e: except sqlite3.Error as e:
print("Error occured deleting old counters:", e.args[0]) print("Error occured deleting old counters:", e.args[0])
for redeem, redeem_info in current_app.config['REDEEMS'].items(): for redeem, redeem_info in current_app.config['REDEEMS'].items():
@ -77,7 +77,7 @@ def refresh_counters():
(redeem,) (redeem,)
) )
db.commit() db.commit()
except Error as e: except sqlite3.Error as e:
print("Failed inserting counters to db:", e.args[0]) print("Failed inserting counters to db:", e.args[0])
@ -86,17 +86,19 @@ def refresh_milestones():
# delete old milestones # delete old milestones
try: try:
cursor = db.execute("SELECT name FROM milestones") cursor = db.execute("SELECT name FROM milestones")
counters = cursor.fetchall() milestones = cursor.fetchall()
to_delete = [] to_delete = []
for counter in counters: for milestone in milestones:
print(f"checking counter {counter}") milestone = milestone[0]
if counter not in current_app.config['REDEEMS'].items(): print(f"Checking milestone {milestone}")
to_delete.append(counter) if milestone not in current_app.config['REDEEMS'].items():
elif current_app.config['REDEEMS'][counter]['type'] != "milestone": to_delete.append(milestone)
to_delete.append(counter) elif current_app.config['REDEEMS'][milestone]['type'] != "milestone":
cursor.execute("DELETE FROM counters WHERE name IN ?", (to_delete,)) to_delete.append(milestone)
for milestone in to_delete:
cursor.execute("DELETE FROM counters WHERE name = ?", (milestone,))
db.commit() db.commit()
except Error as e: except sqlite3.Error as e:
print("Failed deleting old milestones from db:", e.args[0]) print("Failed deleting old milestones from db:", e.args[0])
# add new milestones # add new milestones
@ -104,11 +106,16 @@ def refresh_milestones():
for redeem, redeem_info in current_app.config['REDEEMS'].items(): for redeem, redeem_info in current_app.config['REDEEMS'].items():
if redeem_info["type"] == "milestone": if redeem_info["type"] == "milestone":
cursor = db.execute( cursor = db.execute(
"INSERT INTO milestones(name, progress, goal) VALUES(?, 0, 0)", "SELECT goal FROM milestones WHERE name = ?",
(redeem, 0, redeem_info['goal']) (redeem,)
)
if cursor.fetchone() is None:
cursor.execute(
"INSERT INTO milestones(name, progress, goal) VALUES(?, 0, ?)",
(redeem, redeem_info['goal'])
) )
db.commit() db.commit()
except Error as e: except sqlite3.Error as e:
print("Failed inserting milestones to db:", e.args[0]) print("Failed inserting milestones to db:", e.args[0])

View File

@ -1,5 +1,6 @@
DROP TABLE IF EXISTS counters; DROP TABLE IF EXISTS counters;
DROP TABLE IF EXISTS redeem_queue; DROP TABLE IF EXISTS redeem_queue;
DROP TABLE IF EXISTS milestones;
CREATE TABLE IF NOT EXISTS points ( CREATE TABLE IF NOT EXISTS points (
id TEXT PRIMARY KEY, id TEXT PRIMARY KEY,