2022-05-07 21:54:12 +00:00
|
|
|
from email_validator import validate_email
|
2022-05-07 19:05:29 +00:00
|
|
|
from flask_security import hash_password
|
|
|
|
|
2022-05-08 19:26:25 +00:00
|
|
|
from ... import app, db, user_datastore
|
2022-05-29 17:03:54 +00:00
|
|
|
from ...model_independent_funs import randomPassword
|
2022-05-08 19:26:25 +00:00
|
|
|
from ...models import MAX_YEAR, MIN_YEAR, Admin, Semester
|
|
|
|
from ..terminal_utils import box, confirm, validating_input
|
2022-05-07 19:05:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2022-05-17 11:24:16 +00:00
|
|
|
print("\n")
|
2022-05-07 19:05:29 +00:00
|
|
|
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.")
|
2022-05-08 15:29:45 +00:00
|
|
|
|
2022-05-07 19:05:29 +00:00
|
|
|
if not confirm("Are you sure that you want to continue?"):
|
|
|
|
print("Aborted!")
|
|
|
|
return
|
|
|
|
|
2022-05-07 21:54:12 +00:00
|
|
|
with app.app_context():
|
|
|
|
with db.session.begin():
|
|
|
|
# Delete old database
|
|
|
|
db.drop_all()
|
|
|
|
# Create new database
|
|
|
|
db.create_all()
|
|
|
|
|
|
|
|
semester_label = validating_input(
|
|
|
|
"Enter the label of the current semester",
|
|
|
|
options=("SS", "WS"),
|
|
|
|
format_function=lambda ans: ans.upper(),
|
|
|
|
)
|
|
|
|
|
|
|
|
semester_year = validating_input(
|
|
|
|
f"Enter the year of the current semester (between {MIN_YEAR} and {MAX_YEAR})",
|
|
|
|
format_function=lambda n_str: int(n_str),
|
|
|
|
check_constraints_function=lambda n: MIN_YEAR <= n <= MAX_YEAR,
|
|
|
|
)
|
|
|
|
|
2022-05-15 17:07:53 +00:00
|
|
|
semester = Semester(label=semester_label, year=semester_year)
|
2022-05-07 21:54:12 +00:00
|
|
|
|
|
|
|
db.session.add(semester)
|
|
|
|
|
|
|
|
adminRole = user_datastore.create_role(name="admin")
|
2022-07-03 15:11:33 +00:00
|
|
|
user_datastore.create_role(name="assistant")
|
2022-05-07 21:54:12 +00:00
|
|
|
|
|
|
|
box("The first admin account will now be created")
|
|
|
|
|
|
|
|
admin_email = validating_input(
|
|
|
|
"Enter the admin's email address",
|
|
|
|
format_function=lambda ans: validate_email(ans).email,
|
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
2022-05-16 23:11:47 +00:00
|
|
|
active_semester=semester,
|
2022-05-07 21:54:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
admin = Admin(user=admin_user)
|
|
|
|
|
|
|
|
db.session.add(admin)
|
|
|
|
|
|
|
|
box(admin_password, "Admin password")
|
2022-05-07 19:05:29 +00:00
|
|
|
|
2022-05-08 15:29:45 +00:00
|
|
|
print("Done database initialization!")
|
2022-05-07 19:05:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|