diff --git a/advlabdb/scripts/maintain/reset_admin_password.py b/advlabdb/scripts/maintain/reset_admin_password.py index 6f0cf11..18d3bd2 100644 --- a/advlabdb/scripts/maintain/reset_admin_password.py +++ b/advlabdb/scripts/maintain/reset_admin_password.py @@ -1,4 +1,5 @@ from flask_security import admin_change_password +from sqlalchemy import select from ... import app, db from ...independent_funs import randomPassword @@ -12,15 +13,13 @@ def main(): with app.app_context(): with db.session.begin(): - admin_query = Admin.query - admins = admin_query.filter(Admin.user.has(User.active == True)) + admins = db.session.execute(select(Admin).join(User).where(User.active == True)).scalars().all() activate_user = False - if admins.count() == 0: + + if len(admins) == 0: print("There is no admin with an active user. The user of the choosen admin will be activated") - admins = admin_query.all() + admins = db.session.execute(select(Admin)).scalars().all() activate_user = True - else: - admins = admins.all() num_admins = len(admins) @@ -44,6 +43,9 @@ def main(): choosen_admin_user, new_password, notify=False ) # Password is automatically hashed with this function + if activate_user: + choosen_admin_user.active = True + box(new_password, "New password") print("Done!")