allow manage.py to alter table and delete/list jobs
This commit is contained in:
parent
bc3060085b
commit
7332b2ad7c
118
manage.py
118
manage.py
|
|
@ -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):
|
def create_user(c, username, apikey):
|
||||||
"""Create a user with the given username and apikey, or update the apikey if the username already exists"""
|
"""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,))
|
c.execute("SELECT * FROM users WHERE username=?", (username,))
|
||||||
|
|
@ -48,7 +65,9 @@ def create_user(c, username, apikey):
|
||||||
if result is not None:
|
if result is not None:
|
||||||
raise ValueError(f"found exisitng user {username}, please use update")
|
raise ValueError(f"found exisitng user {username}, please use update")
|
||||||
else:
|
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):
|
def update_user(c, username, apikey):
|
||||||
|
|
@ -65,9 +84,21 @@ def update_user(c, username, apikey):
|
||||||
|
|
||||||
def delete_user(c, username):
|
def delete_user(c, username):
|
||||||
"""Delete the user with the given username, or ignore the operation if the user does not exist"""
|
"""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,))
|
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):
|
def show_users(c, username="", details=False):
|
||||||
"""Print all users in the users table if username is not specified,
|
"""Print all users in the users table if username is not specified,
|
||||||
or only the user with the given username otherwise"""
|
or only the user with the given username otherwise"""
|
||||||
|
|
@ -79,9 +110,13 @@ def show_users(c, username="", details=False):
|
||||||
count = c.fetchone()[0]
|
count = c.fetchone()[0]
|
||||||
print(f"Username: {user[0]}, API Key: {user[1]}, Number of jobs: {count}")
|
print(f"Username: {user[0]}, API Key: {user[1]}, Number of jobs: {count}")
|
||||||
if details:
|
if details:
|
||||||
c.execute("SELECT uuid, created_at, type, status, width, height, steps FROM history WHERE apikey=?", (user[1],))
|
c.execute(
|
||||||
result = c.fetchall()
|
"SELECT uuid, created_at, type, status, width, height, steps FROM history WHERE apikey=?",
|
||||||
print(result)
|
(user[1],),
|
||||||
|
)
|
||||||
|
rows = c.fetchall()
|
||||||
|
for row in rows:
|
||||||
|
print(row)
|
||||||
else:
|
else:
|
||||||
print(f"No user with username '{username}' found")
|
print(f"No user with username '{username}' found")
|
||||||
else:
|
else:
|
||||||
|
|
@ -92,9 +127,13 @@ def show_users(c, username="", details=False):
|
||||||
count = c.fetchone()[0]
|
count = c.fetchone()[0]
|
||||||
print(f"Username: {user[0]}, API Key: {user[1]}, Number of jobs: {count}")
|
print(f"Username: {user[0]}, API Key: {user[1]}, Number of jobs: {count}")
|
||||||
if details:
|
if details:
|
||||||
c.execute("SELECT uuid, created_at, type, status, width, height, steps FROM history WHERE apikey=?", (user[1],))
|
c.execute(
|
||||||
result = c.fetchall()
|
"SELECT uuid, created_at, type, status, width, height, steps FROM history WHERE apikey=?",
|
||||||
print(result)
|
(user[1],),
|
||||||
|
)
|
||||||
|
rows = c.fetchall()
|
||||||
|
for row in rows:
|
||||||
|
print(row)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
@ -109,17 +148,52 @@ def main():
|
||||||
|
|
||||||
# Sub-parser for the "update" action
|
# Sub-parser for the "update" action
|
||||||
update_parser = subparsers.add_parser("update")
|
update_parser = subparsers.add_parser("update")
|
||||||
update_parser.add_argument("username")
|
update_subparsers = update_parser.add_subparsers(dest="update_type")
|
||||||
update_parser.add_argument("apikey")
|
|
||||||
|
# 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
|
# Sub-parser for the "delete" action
|
||||||
delete_parser = subparsers.add_parser("delete")
|
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 = subparsers.add_parser("list")
|
||||||
list_parser.add_argument("username", nargs="?", default="")
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
@ -134,13 +208,21 @@ def main():
|
||||||
# Perform the requested action
|
# Perform the requested action
|
||||||
if args.action == "create":
|
if args.action == "create":
|
||||||
create_user(c, args.username, args.apikey)
|
create_user(c, args.username, args.apikey)
|
||||||
print("User created")
|
|
||||||
elif args.action == "update":
|
elif args.action == "update":
|
||||||
update_user(c, args.username, args.apikey)
|
if args.update_type == "user":
|
||||||
print("User updated")
|
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":
|
elif args.action == "delete":
|
||||||
delete_user(c, args.username)
|
if args.delete_type == "user":
|
||||||
print("User deleted")
|
delete_user(c, args.username)
|
||||||
|
elif args.delete_type == "job":
|
||||||
|
delete_job(c, args.job_id)
|
||||||
elif args.action == "list":
|
elif args.action == "list":
|
||||||
show_users(c, args.username, args.details)
|
show_users(c, args.username, args.details)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue