Ejemplo n.º 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
Ejemplo n.º 2
0
def create_user(
    data: UserCreate,
    background_tasks: BackgroundTasks,
    db: Session = Depends(deps.get_db),
    rdc: RedisCache = Depends(deps.get_redis)
) -> Any:
    """
    Endpoint for create user
    """
    user = crud_user.get_user_by_email(db, data.email)
    if user:
        raise HTTPException(status_code=400,
                            detail='Endereço de email já registrador!')

    user = crud_user.create(db, data)
    rdc.invalidate_cache_prefix("providers-list")
    token = security.generate_token(str(user.id), "activate",
                                    datetime.utcnow() + timedelta(days=31))
    background_tasks.add_task(mail.send_account_activation_email, user.name,
                              user.email, token)

    return {
        'detail':
        'Enviamos um e-mail para você confirma seu cadastro, por favor verifique sua caixa de entrada.'
    }
Ejemplo n.º 3
0
def test_invalidate_cache_prefix(rdc: RedisCache) -> None:
    suffix = utils.random_lower_string()
    prefix = "teste:5"
    key = f"{prefix}:{suffix}"
    value = 30
    rdc.setex(key, timedelta(seconds=3600), value=pickle.dumps(value))
    cache = rdc.get(key)
    assert cache
    rdc.invalidate_cache_prefix(prefix)
    cache = rdc.get(key)
    assert cache is None
Ejemplo n.º 4
0
async def upload_avatar(user: UserModel = Depends(deps.get_user),
                        file: UploadFile = File(...),
                        db: Session = Depends(deps.get_db),
                        rdc: RedisCache = Depends(deps.get_redis)) -> Any:
    """
    Endpoint for upload user avatar
    """
    fileRead = await file.read()
    if user.avatar:
        avatar = media.update(fileRead, user.avatar)

    else:
        avatar = media.save(fileRead)

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

    return user