def get_the_user(id): """ возвращает данные пользователя по полученному ID""" if User.query.get(id): user = User.query.get(id) result = user_schema.dump(user) return jsonify({'user': result}) abort(404)
def create_user(data): """Crea un usuario.""" if create_user_validator.validate(data): # Check if user exists if User.query.filter_by(email=data['email']).first(): return jsonify({'message': 'User already exists'}), 409 hashed_password = generate_password_hash(data['password'], method='sha256') new_user = User(name=data['name'], email=data['email'], password=hashed_password, admin=False) db.session.add(new_user) db.session.commit() return jsonify({ 'message': 'New user created!', 'user': user_schema.dump(new_user).data }), 201 else: return jsonify({ 'message': 'User not created!', 'errors': create_user_validator.errors }), 400
def update_user(id): """ обновляет данные пользователя """ current_user = User.query.get(id) if not current_user: abort(404) if not request.json: abort(400) current_user.user_name = request.json['user_name'] current_user.country = request.json['country'] db.session.commit() current_user = user_schema.dump(current_user) return jsonify({'user': current_user})
def update_firebase_token(data, current_user): """Convierte un usuario a administrador.""" if user_firebase_validator.validate(data): current_user.firebase_token = data["firebase_token"] db.session.commit() return jsonify({ 'message': 'User firebase token updated!', 'user': user_schema.dump(current_user).data }), 200 else: return jsonify({ 'message': 'User token not updated!', 'errors': user_firebase_validator.errors }), 400
def add_user(): """ добавляет нового пользователя в базу """ if not request.json or not 'user_name' in request.json: abort(400) user_exists = User.query.filter(User.user_name == request.json['user_name']).count() if not user_exists: user_name = request.json['user_name'] country = request.json['country'] new_user = User(user_name, country) # добавляем пользователя в базу db.session.add(new_user) db.session.commit() new_user = user_schema.dump(new_user) return jsonify({'user': new_user}), 201 else: return jsonify({'error': 'user exists'}), 409