def generate_users(): users = list() for item in fake_users: user = User.objects(username=item['username']).first() if user: users.append(user) continue user = User() role = Role.objects(name=item['role']).first() user.roles.append(role) user.username = item['username'] user.password = bcrypt.generate_password_hash("password") users.append(user) try: user.save() except Exception as e: log.error("Eror inserting user: %s, %s" % (str(user), e)) return users
def generate_users(): users = list() for item in fake_users: user = User.query.filter_by(username=item['username']).first() if user: users.append(user) continue user = User() poster = Role.query.filter_by(name=item['role']).one() user.roles.append(poster) user.username = item['username'] user.password = bcrypt.generate_password_hash("password") users.append(user) try: db.session.add(user) db.session.commit() except Exception as e: log.error("Eror inserting user: %s, %s" % (str(user), e)) db.session.rollback() return users
def authors_generator(): """Add authors to database""" users = list() for fake_user in fake_authors: user = User.query.filter_by(email=fake_user["email"]).first() if user: users.append(user) continue user = User(email=fake_user["email"], f_name=fake_user["f_name"]) role = Role.query.filter_by(name=fake_user["role"]).first() user.roles.append(role) user.password = bcrypt.generate_password_hash("12345678") try: db.session.add(user) db.session.commit() users.append(user) except Exception as err: print(f"Fail to add user {user} with error {err}") db.session.rollback() return users