Пример #1
0
def trainer_propose(body):
    user_id = check_session(request.cookies)

    trainer = SESSION.query(models.Trainer).filter(
        models.Trainer.user_id == user_id).one_or_none()
    if not trainer:
        return jsonify({"code": 401, "error": "You are not trainer."}), 401

    user = SESSION.query(models.User).filter(
        models.User.nickname == body["nickname"]).one_or_none()
    if not user:
        return jsonify({"code": 500, "error": "User does not exists"}), 500

    propose = (SESSION.query(models.TrainerPropose).filter(
        models.TrainerPropose.user_id == user.id,
        models.TrainerPropose.trainer_id == trainer.id).one_or_none())

    retval = None
    if propose:
        retval = jsonify({"error": "Propose already exists"}), 400

    if trainer in user.trainers:
        retval = jsonify({"error": "You are already training this user"}), 400
    if int(user_id) == user.id:
        retval = jsonify({"error": "You cant propose yourself"}), 400
    if retval:
        return retval

    propose = models.TrainerPropose(user_id=user.id, trainer_id=trainer.id)
    SESSION.add(propose)

    if commit_session(SESSION, raise_exc=False):
        return 200
    return jsonify({"error": "Failed to send proposal."}), 500
Пример #2
0
def add_user(body):
    body["password"] = hash_password(body["password"])

    user = get_user(body["nickname"]).one_or_none()
    if user:
        return jsonify({
            "code": 409,
            "error": "User with this nickname already exists."
        }), 409

    user = models.User(**body)
    user.active = True
    SESSION.add(user)

    if commit_session(SESSION, raise_exc=True):
        session = secrets.token_urlsafe(32)
        REDIS.set(session, user.id, ex=60 * 30)
        return (
            jsonify({"session": session}),
            200,
            {
                "Set-Cookie":
                f"FITSESSIONID={session}; Max-Age={60 * 30}; Path=/"
            },
        )

    return jsonify({"error": "Failed to register. Please try again"}), 500
Пример #3
0
def submit_body_weight(body):
    user_id = check_session(request.cookies)

    body.update({"user_id": user_id})

    body_weight = models.BodyWeight(**body)
    SESSION.add(body_weight)

    if commit_session(SESSION, raise_exc=False):
        return 200

    return jsonify({"error": "Failed to submit body weight"}), 500
Пример #4
0
def add_exercise(body):
    if SESSION.query(models.Exercise).filter(
            models.Exercise.name == body["name"]).one_or_none():
        return jsonify({"error":
                        "Exercise with same name is already in DB."}), 409

    exercise = models.Exercise(**body)
    SESSION.add(exercise)
    if commit_session(SESSION, raise_exc=False):
        return 200

    return jsonify({"error": "Failed to add exercise. Please try again."}), 400
Пример #5
0
def add_training(body):
    if SESSION.query(models.Training).filter(
            models.Training.name == body["training"]["name"]).one_or_none():
        return jsonify({
            "code": 409,
            "error": "Training with specified name already exists."
        }), 409

    training = models.Training(name=body["training"]["name"],
                               description=body["training"]["description"],
                               type=body["training"]["type"])

    for ser in body["series"]:
        exercise_name = ser["exercise"]["name"]
        exercise = SESSION.query(models.Exercise).filter(
            models.Exercise.name == exercise_name).one_or_none()
        if not exercise:
            return jsonify({
                "code":
                404,
                "error":
                "Exercise with specified name does not exists."
            }), 409

        serie = (SESSION.query(models.Series).filter(
            models.Series.break_duration_in_seconds ==
            ser["break_duration_in_seconds"],
            models.Series.repetitions == ser["repetitions"],
            models.Series.exercise_duration_in_seconds ==
            ser["exercise_duration_in_seconds"],
            models.Series.weight == ser["weight"],
            models.Series.exercise_id == exercise.id,
        ).one_or_none())

        if not serie:
            serie = models.Series(
                exercise_duration_in_seconds=ser[
                    "exercise_duration_in_seconds"],
                repetitions=ser["repetitions"],
                break_duration_in_seconds=ser["break_duration_in_seconds"],
                weight=ser["weight"],
                exercise_id=exercise.id,
            )

        training.series.append(serie)

    SESSION.add(training)
    if commit_session(SESSION, raise_exc=False):
        return 200
    return jsonify({"error": "Failed to add training."}), 500
Пример #6
0
def make_trainer():
    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

    if SESSION.query(models.Trainer).filter(
            models.Trainer.user_id == user_id).one_or_none():
        return 200

    trainer = models.Trainer(user_id=user_id)
    SESSION.add(trainer)
    if commit_session(SESSION, raise_exc=False):
        return 200
    return jsonify({"error": "Failed to submit for trainer."}), 500