Example #1
0
    def post(self):
        # create a new day
        data = request.get_json()
        if not data:
            ret = {
                'message': 'Provide data.',
            }
            return jsonify(ret), 422
        exercises = []
        if data.get('exercises'):
            exercises = [
                Exercise.get(each_exercise)
                for each_exercise in data.get('exercises')
            ]

        day = Day(name=data['name'], exercises=filter(None, exercises))
        db.session.add(day)
        db.session.commit()
        db.session.refresh(day)
        ret = {
            'message': 'Day created successfully.',
            'id': day.id,
            'name': data['name']
        }
        return jsonify(ret), 200
Example #2
0
def delete_exercise(id):
    if request.method == 'OPTIONS':
        return Response()

    user_id = session.get('user_id', None)
    if user_id is None:
        return jsonify({'success': False}), 403

    ex = Exercise.get(id)
    if ex.day.workout.user.id != user_id:
        return jsonify({'success': False}), 403

    Exercise.delete(ex.id)
    return jsonify({'success': True})
Example #3
0
def update_exercise(id):

    if request.method == 'OPTIONS':
        return Response()

    user_id = session.get('user_id', None)
    if user_id is None:
        return jsonify({'success': False}), 403

    ex = Exercise.get(id)
    if ex.day.workout.user.id != user_id:
        return jsonify({'success': False}), 403

    sets = request.get_json().get('sets')
    reps = request.get_json().get('reps')
    Exercise.update(id, sets=sets, reps=reps)

    return jsonify({'success': True})
Example #4
0
def move_exercise(id):

    if request.method == 'OPTIONS':
        return Response()

    user_id = session.get('user_id', None)
    if user_id is None:
        return jsonify({'success': False}), 403

    ex = Exercise.get(id)
    user = ex.day.workout.user
    if user_id != user.id:
        return jsonify({'success': False}), 403

    direction = request.get_json().get('direction')
    if (direction == 'up'):
        Exercise.move_up(id)
    else:
        Exercise.move_down(id)

    return jsonify({'success': True})