From af7fcaa59c503620751aa4e3bc7f5e3bcf01cc3b Mon Sep 17 00:00:00 2001 From: Mo8it Date: Mon, 16 May 2022 18:12:53 +0200 Subject: [PATCH] Migrate all() --- advlabdb/scripts/maintain/reset_admin_password.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) 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!")