From 684b126e35bde3c56d3b94bcab5a4a61f335adf0 Mon Sep 17 00:00:00 2001 From: AlexTheGeek <28763288+AlexTheGeek@users.noreply.github.com> Date: Wed, 31 Jan 2024 19:02:27 +0100 Subject: [PATCH] =?UTF-8?q?am=C3=A9lioration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 64 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/main.py b/main.py index fd18092..d7d10a3 100644 --- a/main.py +++ b/main.py @@ -18,7 +18,6 @@ import logging import os - ################ ### Vars APP ### ################ @@ -32,6 +31,7 @@ db = SQLAlchemy(app) migrate = Migrate(app, db) +db.init_app(app) login_manager = LoginManager(app) # Openstack API Connection @@ -775,35 +775,37 @@ def get_template_info(uuid): logger.info('Creating DB') db.create_all() db.session.commit() - # Create default admin user - if not User.query.filter_by(email="admin@admin.fr").first(): - logger.info('Creating default admin user') - random_password = "admin" - hashed_password = PasswordHasher().hash(random_password) - new_user = User(id="2", email="admin@admin.fr", first_name="Admin", last_name="VDI", - password=hashed_password, role="admin", cas=False) - db.session.add(new_user) - db.session.commit() - logger.info("Default admin user created with password: "+random_password) - # Create default prof user - if not User.query.filter_by(email="prof@prof.fr").first(): - logger.info('Creating default prof user') - random_password = "prof" - hashed_password = PasswordHasher().hash(random_password) - new_user = User(id="3", email="prof@prof.fr", first_name="Prof", last_name="VDI", - password=hashed_password, role="prof", cas=False) - db.session.add(new_user) - db.session.commit() - logger.info("Default prof user created with password: "+random_password) - # Create default etudiant user - if not User.query.filter_by(email="etudiant@etudiant.fr").first(): - logger.info('Creating default etudiant user') - random_password = "etudiant" - hashed_password = PasswordHasher().hash(random_password) - new_user = User(id="4", email="etudiant@etudiant.fr", first_name="Etudiant", last_name="VDI", - password=hashed_password, role="user", cas=False) - db.session.add(new_user) - db.session.commit() - logger.info("Default etudiant user created with password: "+random_password) + user = User.query.first() + if not user: + # Create default admin user + if not User.query.filter_by(email="admin@admin.fr").first(): + logger.info('Creating default admin user') + random_password = "admin" + hashed_password = PasswordHasher().hash(random_password) + new_user = User(id="2", email="admin@admin.fr", first_name="Admin", last_name="VDI", + password=hashed_password, role="admin", cas=False) + db.session.add(new_user) + db.session.commit() + logger.info("Default admin user created with password: "+random_password) + # Create default prof user + if not User.query.filter_by(email="prof@prof.fr").first(): + logger.info('Creating default prof user') + random_password = "prof" + hashed_password = PasswordHasher().hash(random_password) + new_user = User(id="3", email="prof@prof.fr", first_name="Prof", last_name="VDI", + password=hashed_password, role="prof", cas=False) + db.session.add(new_user) + db.session.commit() + logger.info("Default prof user created with password: "+random_password) + # Create default etudiant user + if not User.query.filter_by(email="etudiant@etudiant.fr").first(): + logger.info('Creating default etudiant user') + random_password = "etudiant" + hashed_password = PasswordHasher().hash(random_password) + new_user = User(id="4", email="etudiant@etudiant.fr", first_name="Etudiant", last_name="VDI", + password=hashed_password, role="user", cas=False) + db.session.add(new_user) + db.session.commit() + logger.info("Default etudiant user created with password: "+random_password) logger.info('API started') app.run(debug=True, host="0.0.0.0", port=5001) \ No newline at end of file