haldis/app/create_database.py

84 lines
2.3 KiB
Python
Raw Normal View History

2022-04-19 21:31:40 +02:00
"""Script for interaction and changes to the database"""
2019-09-08 02:02:16 +02:00
import add_admins
2022-04-19 21:31:40 +02:00
2022-04-19 22:03:00 +02:00
from app import app_manager, db
2019-06-28 18:57:41 +02:00
entry_sets = {
2019-12-01 21:05:19 +01:00
"admins": add_admins.add,
2019-06-28 18:57:41 +02:00
}
yes = ["yes", "y"]
no = ["no", "n"]
2019-09-07 15:05:24 +02:00
def commit() -> None:
2022-04-19 21:31:40 +02:00
"""Commit all the things to the database"""
db.session.commit()
print("Committing successful")
2019-09-07 15:05:24 +02:00
def check_if_overwrite() -> bool:
2022-04-19 21:31:40 +02:00
"""Check if the user wants to overwrite the previous database"""
2018-03-09 00:59:14 +01:00
answer = input("Do you want to overwrite the previous database? (y/N) ")
return answer.lower() in yes
2019-09-07 15:05:24 +02:00
def add_all() -> None:
2019-09-09 23:19:21 +02:00
"Add all possible entries in the entry_sets to the database"
for entry_set, function in entry_sets.items():
2022-04-19 22:03:00 +02:00
print(f"Adding {entry_set}.")
2019-09-09 23:19:21 +02:00
function()
2019-09-07 15:05:24 +02:00
def recreate_from_scratch() -> None:
2022-04-19 21:31:40 +02:00
"""Recreate a completely new database"""
print("Resetting the database!")
db.drop_all()
db.create_all()
add_to_current()
2019-09-07 15:05:24 +02:00
def add_to_current() -> None:
2022-04-19 21:31:40 +02:00
"""Add things to the current database"""
2015-04-04 14:26:09 +02:00
available = [entry_set for entry_set in entry_sets]
2019-09-07 15:05:24 +02:00
def add_numbers() -> str:
2019-09-05 03:33:29 +02:00
return " ".join(
2022-04-19 22:03:00 +02:00
[f"{loc}({i}), " for i, loc in enumerate(available)]
2019-09-05 03:33:29 +02:00
).rstrip(", ")
while input("Do you still want to add something? (Y/n) ").lower() not in no:
2019-09-05 03:33:29 +02:00
print(
"What do you want to add? (Use numbers, or A for all, or C for cancel) "
)
2022-04-19 22:03:00 +02:00
answer = input(f"Available: {add_numbers()} : ")
if answer.lower() == "a":
add_all()
available = []
elif answer.lower() == "c":
pass
2019-09-07 15:05:24 +02:00
elif answer.isnumeric() and answer in [str(x) for x in range(len(available))]:
answer_index = int(answer)
2022-04-19 22:03:00 +02:00
print(f"Adding {available[answer_index]}.")
2019-09-07 15:05:24 +02:00
entry_sets[str(available[answer_index])]()
del available[answer_index]
else:
print("Not a valid answer.")
print("Thank you for adding, come again!")
@app_manager.command
2020-07-17 11:40:15 +02:00
def setup_database(): # type: None
2022-04-19 21:31:40 +02:00
"""Start the database interaction script"""
2019-09-05 03:33:29 +02:00
print("Database modification script!")
print("=============================\n\n")
if (not db.engine.table_names()) or check_if_overwrite():
recreate_from_scratch()
else:
add_to_current()
2015-04-04 14:36:19 +02:00
commit()
app_manager.run()