def register(): """Функция-обработчик для регистрации пользователя""" register_form = RegisterForm() # необходимая форма login_form = LoginForm() # необходимая форма # проверка на корректность пароля (повтор ввода) if register_form.password.data != register_form.password_again.data: return render_template('login.html', form=login_form, form2=register_form, message="Пароли не совпадают", title="Авторизация") db_sess = db_session.create_session() # проверка на уникальность почты if db_sess.query(User).filter( User.email == register_form.email.data).first(): return render_template('login.html', form=login_form, form2=register_form, message="Такой пользователь уже есть", title="Авторизация") # создание пользователя user = User(name=register_form.name.data, surname=register_form.surname.data, email=register_form.email.data, liked_shops=",", liked_goods=",", shops_created=",") user.set_password(register_form.password.data) # задание пароля db_sess.add(user) # добавление пользователя db_sess.commit() return redirect('/access')
def post(self): args = parser.parse_args() abort_if_user_exists(args['email']) session = db_session.create_session() user = User( name=args['name'], role=args['role'], email=args['email'] ) user.set_password(args['password']) session.add(user) session.commit() return jsonify({'success': 'OK'})
def post(self): args = UserParser.parse_args() session = db_session.create_session() user = User() user.surname = args["surname"] user.name = args["name"] user.age = args["age"] user.position = args["position"] user.speciality = args["speciality"] user.address = args["address"] user.email = args["email"] session.add(user) session.commit() return jsonify({'success': 'OK'})
def post(self): args = parser.parse_args() session = DataBase.create_session() user = User() user.name = args["name"] user.surname = args["surname"] user.email = args["email"] user.hashed_password = args["hashed_password"] user.salt = args["salt"] user.confirmed = args['confirmed'] user.money = args['money'] session.add(user) session.commit() return jsonify({'success': 'OK'})
def reqister(): form = RegisterForm() if form.validate_on_submit(): if form.password.data != form.password_again.data: form.password_again.errors.append("Пароли не совпадают") return render_template('register.html', title='Регистрация', form=form) session = db_session.create_session() if session.query(User).filter(User.email == form.email.data).first(): form.email.errors.append("Такой пользователь уже есть") return render_template('register.html', title='Регистрация', form=form) user = User(name=form.name.data, email=form.email.data, is_teacher=form.is_teacher.data) user.set_password(form.password.data) session.add(user) session.commit() return redirect('/login') return render_template('register.html', title='Регистрация', form=form)
def create_user(): try: if not flask.request.json: return flask.jsonify({'error': 'Empty request'}) elif not all(key in flask.request.json for key in list( filter(lambda x: "__" not in x and "_sa" not in x, list(vars(User))))): return flask.jsonify({'error': 'Bad request'}) db_sess = db_session.create_session() user_id = db_sess.query(User).get(flask.request.json['id']) if user_id: return flask.jsonify({'error': 'Id already exists'}) m_data = list( filter(lambda x: "__" not in x and "_sa" not in x, list(vars(User)))) user = User() for elm in m_data: exec(f'user.{elm} = flask.request.json["{elm}"]') db_sess.add(user) db_sess.commit() return flask.jsonify({'success': 'OK'}) except BaseException as error: with open("logs/api.txt", "w") as file: file.write(error)
def set_password(user: User, password: str): user.hashed_password = generate_password_hash(password)