示例#1
0
def add_trainer(body):
    user_id = check_session(request.cookies)
    user = SESSION.query(
        models.User).filter(models.User.id == user_id).one_or_none()
    if not user:
        return jsonify({"code": 500, "error": "User does not exists."}), 500

    # Trainer
    trainers_user_profile = (SESSION.query(models.User).filter(
        models.User.nickname == body["trainer_nickname"]).one_or_none())
    retval = None

    if not trainers_user_profile:
        retval = jsonify({
            "code": 500,
            "error": "Trainer not in database"
        }), 500

    elif trainers_user_profile.id == user.id:
        retval = jsonify({
            "code": 409,
            "error": "user and trainer are the same"
        }), 409

    if retval:
        return retval

    trainer = SESSION.query(models.Trainer).filter(
        models.Trainer.user_id == trainers_user_profile.id).one_or_none()
    if not trainer:
        return jsonify({
            "code":
            404,
            "error":
            "{} is not trainer.".format(trainers_user_profile.nickname)
        }), 404
    propose = (SESSION.query(models.TrainerPropose).filter(
        models.TrainerPropose.user_id == user_id,
        models.TrainerPropose.trainer_id == trainer.id).one_or_none())
    if not propose:
        return jsonify({"code": 404, "error": "No proposal found"}), 404
    SESSION.delete(propose)
    if body["accept"]:
        if trainer not in user.trainers:
            user.trainers.append(trainer)

    if commit_session(SESSION):
        return 200

    return jsonify({"error": "Failed to add trainer to user."}), 500
示例#2
0
def delete_plan(plan_id):
    user_id = check_session(request.cookies)
    user = SESSION.query(
        models.User).filter(models.User.id == user_id).one_or_none()
    if not user:
        return jsonify({"code": 501, "error": "User does not exists"}), 501
    for plan in user.plans:
        if plan.id == plan_id:
            SESSION.delete(plan)
            if commit_session(SESSION, raise_exc=True):
                return 200
    return jsonify({
        "code": 404,
        "error": "Failed to delete plan. Plan not found"
    }), 404
示例#3
0
def delete_body_weight(body_weight_id):
    user_id = check_session(request.cookies)
    if not user_id:
        return jsonify({"error": "Unauthorized"}), 401

    body_weights = SESSION.query(
        models.BodyWeight).filter(models.BodyWeight.user_id == user_id).all()
    if not body_weights:
        return jsonify({"error": "Found none body weight for given user"}), 404

    for body_weight in body_weights:
        if body_weight.id == body_weight_id:
            SESSION.delete(body_weight)
            commit_session(SESSION)
            return 200
    return jsonify({"error": "Found none body weight with given id"}), 404
示例#4
0
def delete_body_size(body_size_id):
    user_id = check_session(request.cookies)
    if not user_id:
        return jsonify({"error": "Unauthorized"}), 401

    body_sizes = SESSION.query(
        models.BodySize).filter(models.BodySize.user_id == user_id).all()
    if not body_sizes:
        return jsonify({"error": "Found none body sizes for given user"}), 404

    for body_size in body_sizes:
        if body_size.id == body_size_id:
            SESSION.delete(body_size)
            commit_session(SESSION)
            return 200
    return jsonify({"error": "Found none body size with given id"}), 404
示例#5
0
def delete_exercise(exercise_id):
    user_id = check_session(cookies=request.cookies)
    if not user_id:
        return jsonify({"error": "Not authorized"}), 401
    user = SESSION.query(
        models.User).filter(models.User.id == user_id).one_or_none()
    if not user:
        return jsonify({"error": "User not exist"}), 404
    if not user.role == "admin":
        return jsonify({"error": "User not admin"}), 401
    exercise = SESSION.query(models.Exercise).filter(
        models.Exercise.id == exercise_id).one_or_none()
    if not exercise:
        return jsonify({"error": "Training not exist"}), 404
    SESSION.delete(exercise)
    if not commit_session(SESSION):
        return jsonify({"error": "Change was not done"}), 404
    else:
        return "Exercise deleted"