def add_user(): username = request.json['username'] email = request.json['email'] new_user = User(username, email) db.session.add(new_user) db.session.commit() return user_schema.dump(new_user)
def register(): try: registration = registration_schema.load(request.json) except ValidationError as exc: logger.info('Не получилось зарегистрироваться {}'.format(exc)) return bad_request('Не хватает полей') u = create_user(registration) token = u.get_token() db_session.add(token) db_session.commit() return jsonify(user=user_schema.dump(u), token=token_schema.dump(token))
def get_token(): try: p = pass_auth_schema.load(request.json) except ValidationError as exc: return bad_request() u = User.query.filter_by(username=p['username']).first() if u is None: return unauthorized('Не получилось залогиниться') if u.check_password(p['password']): token = u.get_token() token.expire_in_2_weeks() db_session.add(token) db_session.add(u) db_session.commit() return jsonify(token=token_schema.dump(token), username=u.username, user=user_schema.dump(u)) return unauthorized('Не получилось залогиниться')
def me(): u = g.current_user return jsonify({'user': user_schema.dump(u)})
def search(): name = request.args.get("q") users = User.query.filter(User.username.like(f"%{name}%")).all() users_list = user_schema.dump(users) return {"users": users_list}