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
def test_invalidate_cache_user(db: Session, rdc: RedisCache) -> None: user = utils.create_random_user(db) provider = utils.create_random_provider(db) appointment = utils.create_random_appointment(db, provider, user) provider.provider_appointments.append(appointment) key = f"user-appointments:{str(user.id)}" value = 40 rdc.setex(key, timedelta(seconds=3600), value=pickle.dumps(value)) cache = rdc.get(key) assert cache rdc.invalidate_cache_user(provider) cache = rdc.get(key) assert cache is None
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