from flask_security import admin_change_password from sqlalchemy import select from ... import app, db from ...model_independent_funs import randomPassword from ...models import Admin, User from ..terminal_utils import box, spaced_hl, validating_input def main(): print("This script will generate a new random password for a chosen admin.") print() with app.app_context(): with db.session.begin(): admins = db.session.execute(select(Admin).join(User).where(User.active == True)).scalars().all() activate_user = False if len(admins) == 0: print("There is no admin with an active user. The user of the chosen admin will be activated") admins = db.session.execute(select(Admin)).scalars().all() activate_user = True num_admins = len(admins) prompt = "Admins:\n" for ind, admin in enumerate(admins): user = admin.user prompt += f"[{ind}] {user.first_name} {user.last_name}: {user.email}\n" prompt += "Enter number" admin_index = validating_input( prompt, options=[str(ind) for ind in range(num_admins)], format_function=lambda ans: int(ans), ) chosen_admin_user = admins[admin_index].user new_password = randomPassword() admin_change_password( chosen_admin_user, new_password, notify=False ) # Password is automatically hashed with this function if activate_user: chosen_admin_user.active = True box(new_password, "New password") print("Done!") if __name__ == "__main__": main()