1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-20 23:41:20 +00:00

Add init_db script

This commit is contained in:
Mo 2022-05-07 21:05:29 +02:00
parent 06a782ef66
commit b9d5aad18d

125
init_db.py Normal file
View file

@ -0,0 +1,125 @@
import sys
from pathlib import Path
from flask_security import hash_password
from advlabdb import app, db, user_datastore
from advlabdb.independent_funs import randomPassword
from advlabdb.models import MAX_YEAR, MIN_YEAR, Admin, Semester
scripts_dir = Path(__file__).parent.absolute() / "scripts"
sys.path.insert(0, str(scripts_dir))
from shared import box
def validating_input(prompt, options):
prompt += " ["
for opt in options[:-1]:
prompt += opt + "/"
prompt += options[-1] + "]: "
lowered_options = [opt.lower() for opt in options]
ans = input(prompt).lower()
while ans not in lowered_options:
print("Invalid input!")
ans = input(prompt).lower()
return ans
def validating_formatting_input(prompt, format_function, check_constraints_function):
ans = None
first_run = True
while ans is None or not check_constraints_function(ans):
if not first_run:
print("Invalid input!")
else:
first_run = False
try:
ans = format_function(input(prompt))
except Exception as ex:
pass
return ans
def confirm(prompt):
ans = validating_input(prompt, ("y", "n"))
if ans == "y":
return True
else:
return False
def main():
print("This script should only be used to initialize the database after setting up a server")
print("The old database will be DELETED and a new database will be created.")
if not confirm("Are you sure that you want to continue?"):
print("Aborted!")
return
# Delete old database
db.drop_all()
# Create new database
db.create_all()
semester_label = validating_input("Enter the label of the current semester", ("SS", "WS"))
semester_year = validating_formatting_input(
"Enter the year of the current semester:",
lambda n_str: int(n_str),
lambda n: MIN_YEAR <= n <= MAX_YEAR,
)
semester = Semester.customInit(label=semester_label, year=semester_year)
db.session.add(semester)
adminRole = user_datastore.create_role(name="admin")
assistantRole = user_datastore.create_role(name="assistant")
box("The first admin account will now be created")
admin_email = input("Enter the admin's email address: ")
admin_first_name = input("Enter the admin's first name: ")
admin_last_name = input("Enter the admin's last name: ")
admin_phone_number = input("Enter the admin's phone number (optional): ")
admin_mobile_phone_number = input("Enter the admin's mobile phone number (optional): ")
admin_building = input("Enter the admin's building (optional): ")
admin_room = input("Enter the admin's room (optional): ")
admin_password = randomPassword()
admin_hashed_password = hash_password(admin_password)
admin_user = user_datastore.create_user(
email=admin_email.strip(),
password=admin_hashed_password,
roles=[adminRole],
first_name=admin_first_name.strip(),
last_name=admin_last_name.strip(),
phone_number=admin_phone_number.strip() or None,
mobile_phone_number=admin_mobile_phone_number.strip() or None,
building=admin_building.strip() or None,
room=admin_room.strip() or None,
)
admin = Admin(user=admin_user)
db.session.add(admin)
db.session.commit()
box("Admin password:", admin_password)
print("Done! Successfull database initialization.")
if __name__ == "__main__":
main()