allow manage.py to alter table and delete/list jobs

This commit is contained in:
HappyZ 2023-05-05 22:12:59 -07:00
parent bc3060085b
commit 7332b2ad7c
1 changed files with 100 additions and 18 deletions

118
manage.py
View File

@ -41,6 +41,23 @@ def create_table_history(c):
)
def modify_table(c, table_name, operation, column_name=None, data_type=None):
"""Add or drop a column in the table"""
if operation == "add" and column_name and data_type:
c.execute(f"ALTER TABLE {table_name} ADD COLUMN {column_name} {data_type}")
elif operation == "drop" and column_name:
c.execute(f"ALTER TABLE {table_name} DROP COLUMN {column_name}")
elif operation == "show":
rows = c.execute(f"PRAGMA table_info({table_name})")
# Print the column names and types
for row in rows:
column_name = row[1]
column_type = row[2]
print(f"{column_name}: {column_type}")
else:
raise ValueError("Invalid operation or missing column name/data type")
def create_user(c, username, apikey):
"""Create a user with the given username and apikey, or update the apikey if the username already exists"""
c.execute("SELECT * FROM users WHERE username=?", (username,))
@ -48,7 +65,9 @@ def create_user(c, username, apikey):
if result is not None:
raise ValueError(f"found exisitng user {username}, please use update")
else:
c.execute("INSERT INTO users (username, apikey) VALUES (?, ?)", (username, apikey))
c.execute(
"INSERT INTO users (username, apikey) VALUES (?, ?)", (username, apikey)
)
def update_user(c, username, apikey):
@ -65,9 +84,21 @@ def update_user(c, username, apikey):
def delete_user(c, username):
"""Delete the user with the given username, or ignore the operation if the user does not exist"""
c.execute("DELETE FROM history WHERE apikey=(SELECT apikey FROM users WHERE username=?)", (username,))
c.execute(
"DELETE FROM history WHERE apikey=(SELECT apikey FROM users WHERE username=?)",
(username,),
)
c.execute("DELETE FROM users WHERE username=?", (username,))
def delete_job(c, uuid):
"""Delete the job with the given uuid, or ignore the operation if the uuid does not exist"""
c.execute(
"DELETE FROM history WHERE uuid=?",
(uuid,),
)
def show_users(c, username="", details=False):
"""Print all users in the users table if username is not specified,
or only the user with the given username otherwise"""
@ -79,9 +110,13 @@ def show_users(c, username="", details=False):
count = c.fetchone()[0]
print(f"Username: {user[0]}, API Key: {user[1]}, Number of jobs: {count}")
if details:
c.execute("SELECT uuid, created_at, type, status, width, height, steps FROM history WHERE apikey=?", (user[1],))
result = c.fetchall()
print(result)
c.execute(
"SELECT uuid, created_at, type, status, width, height, steps FROM history WHERE apikey=?",
(user[1],),
)
rows = c.fetchall()
for row in rows:
print(row)
else:
print(f"No user with username '{username}' found")
else:
@ -92,9 +127,13 @@ def show_users(c, username="", details=False):
count = c.fetchone()[0]
print(f"Username: {user[0]}, API Key: {user[1]}, Number of jobs: {count}")
if details:
c.execute("SELECT uuid, created_at, type, status, width, height, steps FROM history WHERE apikey=?", (user[1],))
result = c.fetchall()
print(result)
c.execute(
"SELECT uuid, created_at, type, status, width, height, steps FROM history WHERE apikey=?",
(user[1],),
)
rows = c.fetchall()
for row in rows:
print(row)
def main():
@ -109,17 +148,52 @@ def main():
# Sub-parser for the "update" action
update_parser = subparsers.add_parser("update")
update_parser.add_argument("username")
update_parser.add_argument("apikey")
update_subparsers = update_parser.add_subparsers(dest="update_type")
# Sub-parser for updating a user
update_user_parser = update_subparsers.add_parser("user")
update_user_parser.add_argument("username")
update_user_parser.add_argument("apikey")
# Sub-parser for updating a table
update_table_parser = update_subparsers.add_parser("table")
update_table_subparsers = update_table_parser.add_subparsers(dest="table_action")
# Sub-parser for adding a column to a table
table_add_parser = update_table_subparsers.add_parser("add")
table_add_parser.add_argument("table_name")
table_add_parser.add_argument("column_name")
table_add_parser.add_argument("column_type")
# Sub-parser for dropping a column from a table
table_drop_parser = update_table_subparsers.add_parser("drop")
table_drop_parser.add_argument("table_name")
table_drop_parser.add_argument("column_name")
# Sub-parser for showing a table
table_drop_parser = update_table_subparsers.add_parser("show")
table_drop_parser.add_argument("table_name")
# Sub-parser for the "delete" action
delete_parser = subparsers.add_parser("delete")
delete_parser.add_argument("username")
delete_subparsers = delete_parser.add_subparsers(dest="delete_type")
user_parser = delete_subparsers.add_parser("user")
user_parser.add_argument("username")
job_parser = delete_subparsers.add_parser("job")
job_parser.add_argument("job_id")
# Sub-parser for the "delete" action
# Sub-parser for the "list" action
list_parser = subparsers.add_parser("list")
list_parser.add_argument("username", nargs="?", default="")
list_parser.add_argument("--details", action="store_true", help="Showing more details")
list_parser.add_argument(
"--details", action="store_true", help="Showing more details"
)
# Sub-parser for the "modify" action
modify_parser = subparsers.add_parser("modify")
modify_parser.add_argument("table")
modify_parser.add_argument("--add-column")
modify_parser.add_argument("--drop-column")
args = parser.parse_args()
@ -134,13 +208,21 @@ def main():
# Perform the requested action
if args.action == "create":
create_user(c, args.username, args.apikey)
print("User created")
elif args.action == "update":
update_user(c, args.username, args.apikey)
print("User updated")
if args.update_type == "user":
update_user(c, args.username, args.apikey)
elif args.update_type == "table":
if args.table_action == "add":
modify_table(c, args.table_name, args.table_action, args.column_name, args.column_type)
elif args.table_action == "drop":
modify_table(c, args.table_name, args.table_action, args.column_name)
elif args.table_action == "show":
modify_table(c, args.table_name, args.table_action)
elif args.action == "delete":
delete_user(c, args.username)
print("User deleted")
if args.delete_type == "user":
delete_user(c, args.username)
elif args.delete_type == "job":
delete_job(c, args.job_id)
elif args.action == "list":
show_users(c, args.username, args.details)