def post_exercises(): '''Post new exercise.''' serializer = Serializer(ExerciseSchema, request.args) data = dict(author_id=auth.current_user.id, **serializer.load(request.get_json())) exercise = Exercise.create(db.session, data) rv = serializer.dump(exercise) return rv, 201, get_location_header('.get_exercise', id=exercise.id)
def put_exercise(id): '''Update an exercise.''' exercise = get_or_404(Exercise, id) if auth.current_user.id != exercise.author_id: raise AuthorizationError serializer = Serializer(ExerciseSchema, request.args) exercise.update(db.session, serializer.load(request.get_json())) return serializer.dump(exercise)
def post_users(): '''Register a user.''' serializer = Serializer(ProfileSchema, request.args) user = User.create(db.session, serializer.load(request.get_json()), commit=False) default_exercises = Exercise.query.filter(Exercise.default).all() user.favorite_exercises = default_exercises db.session.commit() rv = serializer.dump(user) return rv, 201, get_location_header('.get_user', id=user.id)
def post_response(id): '''Post response.''' questionnaire = get_or_404(Questionnaire, id) serializer = Serializer(QuestionnaireResponseSchema, request.args, context=dict(questionnaire=questionnaire)) data = serializer.load(request.get_json()) data.update(dict(user_id=auth.current_user.id, questionnaire_id=id)) response = QuestionnaireResponse.create(db.session, data) db.session.add(response) db.session.commit() return serializer.dump(response)
def put_user(id): '''Update a user.''' user = get_or_404(User, id) if user.id != auth.current_user.id: raise AuthorizationError serializer = Serializer(ProfileSchema, request.args) # This lets the schema validator know about the user to be updated for # validating unique columns. So it can ignore false positives. serializer.context = dict(update_id=user.id) data = serializer.load(request.get_json(), exclude=('password', )) user.update(db.session, data) return serializer.dump(user)