Ejemplo n.º 1
0
def get_coach(coach_id) -> (dict, int):
    coach = UserModel.find_coach_by_id(user_id=coach_id)

    # Check if given coach_id exists in DB
    if coach is None:
        raise EntityNotFoundError

    return CoachSchema().dump(coach), 200
Ejemplo n.º 2
0
def get_coach_action_card_batches(coach_id: str) -> (dict, int):
    # Check if given coach_id exists in DB
    coach = UserModel.find_coach_by_id(user_id=coach_id)
    if coach is None:
        raise EntityNotFoundError

    # Retrieve data
    data = ActionCardBatchModel.find_action_card_batches_by_coach(coach_id)
    return ActionCardBatchSchema(many=True).dump(data), 200
Ejemplo n.º 3
0
def delete_coach(coach_id: str) -> (dict, int):
    coach = UserModel.find_coach_by_id(user_id=coach_id)

    # Check if given coach_id exists in DB
    if coach is None:
        raise EntityNotFoundError

    # Delete user in DB
    coach.delete()

    return {}, 204
Ejemplo n.º 4
0
def update_coach_action_card_batches(coach_id: str,
                                     data: bytes) -> (dict, int):
    # Check if given coach_id exists in DB
    coach = UserModel.find_coach_by_id(user_id=coach_id)
    if coach is None:
        raise EntityNotFoundError(msg="Coach does not exist")

    # Prevent another coach from updating another coach action card batches
    if coach_id != get_jwt_identity():
        raise PermissionDeniedError(
            msg="You can't update the action card batches of another coach !")

    # Validate and serialize data
    schema = ActionCardBatchSchema(many=True)
    data, err_msg, err_code = schema.loads_or_400(data)
    if err_msg:
        return err_msg, err_code

    # Retrieve current action card batches
    current_action_card_batches = {
        o.actionCardBatchId: o
        for o in ActionCardBatchModel.find_action_card_batches_by_coach(
            coach_id)
    }

    # Process provided data
    output = []
    for d in data:
        if ("actionCardBatchId" in d
                and d["actionCardBatchId"] in current_action_card_batches):
            # Update action card batch if already existing
            action_card_batch = current_action_card_batches[
                d["actionCardBatchId"]]
            for k, v in d.items():
                setattr(action_card_batch, k, v)
            action_card_batch.updatedAt = datetime.utcnow()
        else:
            # Create new object otherwise
            action_card_batch = ActionCardBatchModel(
                **{k: v
                   for k, v in d.items() if k != "actionCardId"})

        action_card_batch.coachId = coach_id
        action_card_batch.save()
        output.append(action_card_batch)

    new_ids = {o.id: None for o in output}
    # Delete current action card batches that are not in provided data
    for o_id, action_card_batch in current_action_card_batches.items():
        if o_id not in new_ids:
            action_card_batch.delete()

    return ActionCardBatchSchema(many=True).dump(output), 200