mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-12-20 23:41:20 +00:00
Done init_db script
This commit is contained in:
parent
3fd804d175
commit
7c9493facc
1 changed files with 74 additions and 55 deletions
129
init_db.py
129
init_db.py
|
@ -1,6 +1,7 @@
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from email_validator import validate_email
|
||||||
from flask_security import hash_password
|
from flask_security import hash_password
|
||||||
|
|
||||||
from advlabdb import app, db, user_datastore
|
from advlabdb import app, db, user_datastore
|
||||||
|
@ -14,43 +15,53 @@ sys.path.insert(0, str(scripts_dir))
|
||||||
from shared import box
|
from shared import box
|
||||||
|
|
||||||
|
|
||||||
def validating_input(prompt, options):
|
def validating_input(
|
||||||
prompt += " ["
|
prompt,
|
||||||
for opt in options[:-1]:
|
options=None,
|
||||||
prompt += opt + "/"
|
format_function=lambda ans: ans,
|
||||||
|
check_constraints_function=lambda ans: True,
|
||||||
|
):
|
||||||
|
if options is not None:
|
||||||
|
prompt += " ["
|
||||||
|
|
||||||
prompt += options[-1] + "]: "
|
for opt in options[:-1]:
|
||||||
|
prompt += opt + "/"
|
||||||
|
|
||||||
lowered_options = [opt.lower() for opt in options]
|
prompt += options[-1] + "]: "
|
||||||
|
|
||||||
ans = input(prompt).lower()
|
lowered_options = [opt.lower() for opt in options]
|
||||||
while ans not in lowered_options:
|
|
||||||
print("Invalid input!")
|
|
||||||
ans = input(prompt).lower()
|
|
||||||
|
|
||||||
return ans
|
def adj_check_constraints_function(ans):
|
||||||
|
return ans.lower() in lowered_options and check_constraints_function(ans)
|
||||||
|
|
||||||
|
else:
|
||||||
|
prompt += ": "
|
||||||
|
adj_check_constraints_function = check_constraints_function
|
||||||
|
|
||||||
def validating_formatting_input(prompt, format_function, check_constraints_function):
|
|
||||||
ans = None
|
ans = None
|
||||||
first_run = True
|
first_run = True
|
||||||
|
|
||||||
while ans is None or not check_constraints_function(ans):
|
while ans is None or not adj_check_constraints_function(ans):
|
||||||
if not first_run:
|
if not first_run:
|
||||||
print("Invalid input!")
|
print("Invalid input!")
|
||||||
else:
|
else:
|
||||||
first_run = False
|
first_run = False
|
||||||
|
|
||||||
|
ans = input(prompt)
|
||||||
try:
|
try:
|
||||||
ans = format_function(input(prompt))
|
ans = format_function(ans)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
pass
|
ans = None
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
def confirm(prompt):
|
def confirm(prompt):
|
||||||
ans = validating_input(prompt, ("y", "n"))
|
ans = validating_input(
|
||||||
|
prompt,
|
||||||
|
options=("y", "n"),
|
||||||
|
format_function=lambda ans: ans.lower(),
|
||||||
|
)
|
||||||
|
|
||||||
if ans == "y":
|
if ans == "y":
|
||||||
return True
|
return True
|
||||||
|
@ -65,58 +76,66 @@ def main():
|
||||||
print("Aborted!")
|
print("Aborted!")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Delete old database
|
with app.app_context():
|
||||||
db.drop_all()
|
with db.session.begin():
|
||||||
# Create new database
|
# Delete old database
|
||||||
db.create_all()
|
db.drop_all()
|
||||||
|
# Create new database
|
||||||
|
db.create_all()
|
||||||
|
|
||||||
semester_label = validating_input("Enter the label of the current semester", ("SS", "WS"))
|
semester_label = validating_input(
|
||||||
|
"Enter the label of the current semester",
|
||||||
|
options=("SS", "WS"),
|
||||||
|
format_function=lambda ans: ans.upper(),
|
||||||
|
)
|
||||||
|
|
||||||
semester_year = validating_formatting_input(
|
semester_year = validating_input(
|
||||||
"Enter the year of the current semester:",
|
f"Enter the year of the current semester (between {MIN_YEAR} and {MAX_YEAR})",
|
||||||
lambda n_str: int(n_str),
|
format_function=lambda n_str: int(n_str),
|
||||||
lambda n: MIN_YEAR <= n <= MAX_YEAR,
|
check_constraints_function=lambda n: MIN_YEAR <= n <= MAX_YEAR,
|
||||||
)
|
)
|
||||||
|
|
||||||
semester = Semester.customInit(label=semester_label, year=semester_year)
|
semester = Semester.customInit(label=semester_label, year=semester_year)
|
||||||
|
|
||||||
db.session.add(semester)
|
db.session.add(semester)
|
||||||
|
|
||||||
adminRole = user_datastore.create_role(name="admin")
|
adminRole = user_datastore.create_role(name="admin")
|
||||||
assistantRole = user_datastore.create_role(name="assistant")
|
assistantRole = user_datastore.create_role(name="assistant")
|
||||||
|
|
||||||
box("The first admin account will now be created")
|
box("The first admin account will now be created")
|
||||||
|
|
||||||
admin_email = input("Enter the admin's email address: ")
|
admin_email = validating_input(
|
||||||
admin_first_name = input("Enter the admin's first name: ")
|
"Enter the admin's email address",
|
||||||
admin_last_name = input("Enter the admin's last name: ")
|
format_function=lambda ans: validate_email(ans).email,
|
||||||
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_first_name = input("Enter the admin's first name: ")
|
||||||
admin_hashed_password = hash_password(admin_password)
|
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_user = user_datastore.create_user(
|
admin_password = randomPassword()
|
||||||
email=admin_email.strip(),
|
admin_hashed_password = hash_password(admin_password)
|
||||||
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)
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
db.session.add(admin)
|
admin = Admin(user=admin_user)
|
||||||
|
|
||||||
db.session.commit()
|
db.session.add(admin)
|
||||||
|
|
||||||
box("Admin password:", admin_password)
|
box(admin_password, "Admin password")
|
||||||
|
|
||||||
print("Done! Successfull database initialization.")
|
print("Done! Successfull database initialization.")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue