Ejemplo n.º 1
0
def register():
    session = current_app.db.session

    res = request.get_json()
    nickname = res.get("nickname")
    first_name = res.get("first_name")
    last_name = res.get("last_name")
    biography = res.get("biography")
    created_at = res.get("created_at")
    email = res.get("email")
    password = res.get("password")

    verify_user: UserModel = UserModel.query.filter_by(email=email).first()

    if verify_user:
        return {"error": f"{email} already exists"}, HTTPStatus.FORBIDDEN

    new_user = UserModel(
        nickname=nickname,
        email=email,
        first_name=first_name,
        last_name=last_name,
        biography=biography,
        created_at=created_at,
    )
    new_user.password = password
    session.add(new_user)

    session.commit()

    access_token = create_access_token(
        identity=new_user.id, expires_delta=timedelta(days=7)
    )

    return {
        "user": {
            "email": new_user.email,
            "nickname": new_user.nickname,
            "first_name": new_user.first_name,
            "last_name": new_user.last_name,
            "biography": new_user.biography,
            "created_at": new_user.created_at,
            "access_token": access_token,
        }
    }, HTTPStatus.CREATED
Ejemplo n.º 2
0
def create_coach(data: bytes) -> (dict, int):
    schema = CoachSchema()
    data, err_msg, err_code = schema.loads_or_400(data)
    if err_msg:
        return err_msg, err_code

    role = data.pop("role")

    # Check if user already exist
    user = UserModel.find_by_email(data["email"])
    if user is not None:
        if Roles.COACH.value in user.role:
            # raise exception if user is already a coach
            raise UserAlreadyExistsError
        else:
            # Otherwise, update role
            user.role.append(Roles.COACH.value)
    else:
        user = UserModel(**data)
        user.role = [Roles.COACH.value]

    # Setting admin role if needed
    if role == "admin":
        user.role.append(Roles.ADMIN.value)

    # Encrypt password
    if user.password and user.password != "":
        user.password = generate_password_hash(user.password)

    # Create user in DB
    user.save()

    # Generate default action card batches
    default_action_card_batches = ActionCardBatchModel.find_default_batches()
    for default_action_card_batch in default_action_card_batches:
        ActionCardBatchModel(
            coachId=user.id,
            name=default_action_card_batch.name,
            actionCardIds=default_action_card_batch.actionCardIds,
            type=default_action_card_batch.type,
        ).save()

    return schema.dump(user), 200
Ejemplo n.º 3
0
def create_user():
    session = current_app.db.session

    body = request.get_json()
    username = body.get("username")
    email = body.get("email")
    password = body.get("password")

    user_existence = UserModel.query.filter_by(email=email).first()

    if user_existence != None:
        return {"msg": "User already exists!"}, HTTPStatus.UNPROCESSABLE_ENTITY

    new_user = UserModel(username=username, email=email)
    new_user.password = password

    user_schema = UserSchema()

    session.add(new_user)
    session.commit()

    return user_schema.dump(new_user), HTTPStatus.CREATED