Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
def get_user(id):
    '''Get a single user. '''
    if auth.current_user and auth.current_user.id == id:
        user = auth.current_user
        serializer = Serializer(ProfileSchema, request.args)
    else:
        user = get_or_404(User, id)
        serializer = Serializer(UserSchema, request.args)

    return serializer.dump(user)
Esempio n. 4
0
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)
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
def get_questionnaire(id):
    '''Get questionnaire.'''

    questionnaire = Questionnaire.query.\
        outerjoin(QuestionnaireResponse, and_(
            QuestionnaireResponse.user_id == auth.current_user.id,
            QuestionnaireResponse.questionnaire_id == Questionnaire.id
        )).\
        filter(Questionnaire.id == id).\
        options(contains_eager(Questionnaire.responses)).first()

    if not questionnaire:
        abort(404)

    serializer = Serializer(QuestionnaireSchema, request.args)
    return serializer.dump(questionnaire)
Esempio n. 8
0
def get_exercise(id):
    '''Get an exercise.'''
    query = Exercise.query.filter(Exercise.id == id)

    if auth.current_user:
        result = query.add_entity(Rating).\
            join(Rating).\
            filter(Rating.user_id == auth.current_user.id).\
            first()

        try:
            exercise = setattr_and_return(result[0], 'user_rating', result[1])
        except TypeError:
            exercise = None
    else:
        exercise = query.first()

    if not exercise:
        abort(404)

    serializer = Serializer(ExerciseSchema, request.args)
    return serializer.dump(exercise)