def check_db(): tried_times = 0 while tried_times < 30: try: with db.engine.connect() as conn: stdout('db is ready.') pass except Exception: time.sleep(1) tried_times += 1 stdout('waiting for db', tried_times) sys.stdout.flush() continue return stdout('db is not ready, cannot start') raise Exception()
admin, user = init_roles() users = [ ('*****@*****.**', '111111', 'chenzhao', [admin, user]), ('*****@*****.**', '111111', 'chengpeng', [admin, user]), ('*****@*****.**', '111111', 'test1', [user]), ('*****@*****.**', '111111', 'test2', [user]), ('*****@*****.**', '111111', 'test3', [user]), ('*****@*****.**', '111111', 'test4', [user]), ('*****@*****.**', '111111', 'test5', [user]), ] for email, password, username, roles in users: user = get_or_create(User, username=username, email=email, source="python") user.hash_password(password) for role in roles: user_datastore.add_role_to_user(user, role) db.session.commit() if __name__ == '__main__': stdout('<<<<<<init db begin.') check_db() # drop_all_table() db.create_all() init_users() stdout('>>>>>>init db done.') # clear_and_import_all() pass
def init_users(): admin, user = init_roles() users = [ ('*****@*****.**', '111111', 'chenzhao', [admin, user]), ('*****@*****.**', '111111', 'chengpeng', [admin, user]), ('*****@*****.**', '111111', 'test1', [user]), ('*****@*****.**', '111111', 'test2', [user]), ('*****@*****.**', '111111', 'test3', [user]), ('*****@*****.**', '111111', 'test4', [user]), ('*****@*****.**', '111111', 'test5', [user]), ] for email, password, username, roles in users: user = get_or_create(User, username=username, email=email, source="python") user.hash_password(password) for role in roles: user_datastore.add_role_to_user(user, role) db.session.commit() if __name__ == '__main__': stdout('<<<<<<init db begin.') check_db() # drop_all_table() db.create_all() init_users() stdout('>>>>>>init db done.') # clear_and_import_all() pass