def _insert_user(self, username, password, role_name): test_role = Role(role_name) db.session.add(test_role) db.session.commit() test_user = User(username) test_user.set_password(password) db.session.add(test_user) db.session.commit()
def process_reg(): form = RegistrationForm() if form.validate_on_submit(): new_user = User(login=form.username.data) new_user.set_password(form.password.data) db.session.add(new_user) db.session.commit() flash('Вы успешно зарегистрировались!') return redirect(url_for('auth.login')) flash('Исправьте пожалуйста ошибки в форме.') return redirect(url_for('auth.register'))
def setUp(self): admin._views = [] self.app = create_app('config.TestingConfig') self.app_context = self.app.app_context() self.app_context.push() self.client = self.app.test_client() #db.app = app db.create_all() Clinic.insert_clinic() Role.insert_roles() User.insert_users()
def _insert_user(self, username, password, rolename): role = Role.query.filter_by(description=rolename).first() user = User.query.filter_by(username=username) if user is None: user = User() user.username = username user.set_password(password) if role: user.roles.append(role) db.session.add(user) db.session.commit()
def _insert_user(self, email, f_name, password, role): """ Create role and user :param email: User's email :param f_name: User's first name :param password: User's account password :param role: User's blog role """ with self.app.app_context(): test_role = Role(role) db.session.add(test_role) db.session.commit() test_user = User(email=email, f_name=f_name) test_user.set_password(password) db.session.add(test_user) db.session.commit()
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
def generate_users(): users = list() for item in fake_users: user = User(username=item['username']) if user: users.append(user) # continue user = User() role = Role.objects(name=item['role']).first() user.roles.append(role) user.username = item['username'] user.set_password("password") users.append(user) try: user.save() except Exception as e: log.error("Error inserting user: %s, %s" % (str(user), e)) return users
from webapp import create_app, db from webapp.auth.models import User, Role app = create_app('config.TestConfig') db.app = app db.create_all() with app.app_context(): user_role = Role("user") author_role = Role("author") db.session.add(user_role) db.session.add(author_role) db.session.commit() test_user = User(email="*****@*****.**", f_name="test") test_user.set_password("test") test_user.roles.append(author_role) db.session.add(test_user) db.session.commit() app.run()
from webapp import create_app, db from webapp.auth.models import User, Role app = create_app("config.TestConfig") db.app = app db.create_all() default = Role("default") poster = Role("poster") db.session.add(default) db.session.add(poster) db.session.commit() test_user = User("test") test_user.set_password("test") test_user.roles.append(poster) db.session.add(test_user) db.session.commit() app.run()