Example #1
0
def update_user(
    data: UserUpdate,
    user: UserModel = Depends(deps.get_user),
    db: Session = Depends(deps.get_db),
    rdc: RedisCache = Depends(deps.get_redis)
) -> Any:
    """
    Endpoint for update current user
    """
    db_user = crud_user.get_user_by_email(db, data.email)
    if db_user and db_user.id != user.id:
        raise HTTPException(status_code=400,
                            detail='Endereço de email já registrador!')

    data.avatar = user.avatar
    data.is_active = user.is_active
    if not data.address and user.address:
        data.address = user.address

    user = crud_user.update(db, user, data)
    rdc.invalidate_cache_provider(user)
    if user.address:  #<-- if have an address, is a provider
        rdc.invalidate_cache_user(user)
        rdc.invalidate_cache_prefix("providers-list")

    return user
Example #2
0
def request_user_activate(
    data: UserActive, db: Session = Depends(deps.get_db)) -> Any:
    """
    Endpoint for user activate
    """
    user_id = security.verify_token(data.token, 'activate')
    user = crud_user.get_user_by_id(db, user_id)
    if not user:
        raise HTTPException(status_code=404, detail="Usuário não encontrado!")

    if user.is_active:
        raise HTTPException(status_code=400, detail="Conta já verificada")

    user_in = UserUpdate(**jsonable_encoder(user))
    user_in.is_active = True
    crud_user.update(db, user, user_in)
    if user.address:
        return {"type": "provider"}

    return {"type": "user"}