def update_user_me( *, db: Session = Depends(deps.get_db), password: str = Body(None), full_name: str = Body(None), email: EmailStr = Body(None), current_user: models.User = Depends(deps.get_current_user), ) -> Any: """ Update own user. """ # Create a UserUpdate object to update info in user_in = schemas.UserUpdate() # Update info given if password is not None: user_in.password = password if full_name is not None: user_in.full_name = full_name if email is not None: user_in.email = email logging.info( f"User {current_user.id} ({current_user.email}) is updating themselves to {user_in.__dict__}" ) # Use the object to update user in db user = crud.user.update(db, db_obj=current_user, obj_in=user_in) return user
def update_user_me( *, db: Session = Depends(deps.get_db), password: str = Body(None), first_name: str = Body(None), last_name: str = Body(None), email: EmailStr = Body(None), current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Update current login user :return: """ current_user_data = jsonable_encoder(current_user) user_in = schemas.UserUpdate(**current_user_data) if password is not None: user_in.password = password if first_name is not None: user_in.first_name = first_name if last_name is not None: user_in.last_name = last_name if email is not None: user_in.email = email user = crud.user.update(db, db_obj=current_user, obj_in=user_in) return user
async def update_user_me( *, db: Session = Depends(deps.get_db), password: str = Body(None), current_user: models.User = Depends(deps.get_current_user) ) -> Any: """Update me. Args: db (Session, optional): [description]. D efaults to Depends(deps.get_db). password (str, optional): [description]. Defaults to Body(None). current_user (models.User, optional): [description]. Defaults to Depends(deps.get_current_active_user). Returns: Any: [description] """ current_user_data = jsonable_encoder(current_user) user_in = schemas.UserUpdate(**current_user_data) if password is not None: user_in.password = password user = crud.user.update(db, db_obj=current_user, obj_in=user_in) return user
def update_user(*, db: Session = Depends(dependancy.get_db), password: str = Body(None), username: str = Body(None), email=Body(None), current_user: models.User = Depends( dependancy.get_current_active_user)): current_user_data = jsonable_encoder(current_user) user_in = schemas.UserUpdate(**current_user_data) if password is not None: if any(c.islower() for c in user_in.password) and any( c.isupper() for c in user_in.password) and any(c.isdigit() for c in user_in.password): user_in.password = password else: raise HTTPException(status_code=status.HTTP_406_NOT_ACCEPTABLE, detail='The password must be a combination of ' 'at least a digit,an uppercase and ' 'lower case letter') if username is not None: if len(user_in.username) <= 8 or len(user_in.username) > 30: raise HTTPException( status_code=status.HTTP_411_LENGTH_REQUIRED, detail='The username should be at least 8 chars ' 'chars less than 30 chars') else: user_in.username = username if email is not None: user_in.email = email user = crud.user.update(db=db, db_obj=current_user, obj_in=user_in) return user
def update_user_profile_picture( *, db: Session = Depends(deps.get_db), user_id: str, image: UploadFile = File(...), current_user: models.User = Depends(deps.get_current_user), ) -> Any: """ Update a user's profile picture """ if current_user.id == user_id or ( (admin := crud.admin.get(db, current_user.id)) and schemas.AdminPermissions(admin.permissions).is_allowed("user")): if image.content_type not in ("image/png", "image/jpeg"): raise UnsupportedMediaTypeException( detail="Profile pictures can only be PNG or JPG images") if user := crud.user.get(db, user_id): filename = save_image(image) logging.info( f"User {current_user.id} ({current_user.email}) has updated their profile picture from" f"{current_user.profile_picture} to {filename}") return crud.user.update( db, db_obj=user, obj_in=schemas.UserUpdate(profile_picture=filename))
def update_user_me( *, db: Session = Depends(deps.get_db), password: str = Body(None), full_name: str = Body(None), email: EmailStr = Body(None), department_id: int = Body(None), current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Update own user. """ current_user_data = jsonable_encoder(current_user) user_in = schemas.UserUpdate(**current_user_data) if password is not None: user_in.password = password if full_name is not None: user_in.full_name = full_name if email is not None: user_in.email = email if crud.user.get_by_email(db, user_in.email): raise HTTPException( status_code=404, detail= "The user with this email has been existed in the system", ) if department_id and not crud.department.get_by_id(user_in.department_id): raise HTTPException( status_code=404, detail="The department does not existed in the system", ) user = crud.user.update(db, db_obj=current_user, obj_in=user_in) return user
def update_user_me( *, db: Session = Depends(deps.get_db), password: str = Body(None), full_name: str = Body(None), email: EmailStr = Body(None), current_user: models.User = Depends(deps.get_current_active_user), ) -> models.User: """# Update self (user) A normal user may update their own user model in a limited way, by modifying password, full name, or email address. The new email address needs to be unique, the same as when creating a new user. ## Args: - db (Session, optional): SQLAlchemy Session. Defaults to Depends(deps.get_db). - password (str, optional): New password. Defaults to Body(None). - full_name (str, optional): New full name. Defaults to Body(None). - email (EmailStr, optional): New email address. Defaults to Body(None). - current_user (models.User, optional): User object for the user accessing the endpoint. Defaults to Depends(deps.get_current_active_user). ## Raises: - HTTPException: 400 - When attempting to update a user's email to an email address that is already in the database. ## Returns: - User: The updated user model """ current_user_data = jsonable_encoder(current_user) user_in = schemas.UserUpdate(**current_user_data) if email is not None: in_database = crud.user.get_by_email(db, email=email) if in_database: raise HTTPException( status_code=400, detail= "A user with this username already exists in the system.", ) user_in.email = email if password is not None: user_in.password = password if full_name is not None: user_in.full_name = full_name user = crud.user.update(db, db_obj=current_user, obj_in=user_in) return user
async def test_update( async_client: AsyncClient, test_user: schemas.UserDB, ) -> None: new_username = test_user.username + "xxx" user_id = await crud_user.update( user_id=test_user.id, payload=schemas.UserUpdate(username=new_username)) actual = await crud_user.get(user_id) assert actual assert actual.id == test_user.id assert actual.email == test_user.email assert actual.username == new_username
def update_users(id: int, user: schemas.UserUpdate): """ Supporte la mise a jour partiel. C'est a dire que vous n'etes pas obligé de renseigner tout les champs. Mettez a jour juste les données a modifier et ignorer les autres va parfaitement marcher """ stored_data = jsonable_encoder(crud.get_user(id)) if stored_data is None: raise HTTPException(status_code=404, detail="User not found") sotred_model = schemas.UserUpdate(**stored_data["__data__"]) updated_data = user.dict(exclude_unset=True, exclude_none=True, exclude_defaults=True) updated_user = sotred_model.copy(update=updated_data) return crud.update_user(id, updated_user)
async def update_user_me( *, user_in: schemas.UnprivilegedUserUpdate, db: Session = Depends(deps.get_db), redis: aioredis.Redis = Depends(deps.get_redis), current_user: schemas.UserInDB = Depends(deps.get_current_active_user), ) -> Any: """ Update own user. """ user_in = schemas.UserUpdate(**user_in.dict(exclude_unset=True)) return await crud.user_cachedb.update(db, redis, cache_obj=current_user, obj_in=user_in)
def update_user_me( *, db: Session = Depends(deps.get_db), full_name: str = Body(None), phone_number: str = Body(None), email: EmailStr = Body(None), current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Update own user. """ current_user_data = jsonable_encoder(current_user) user_in = schemas.UserUpdate(**current_user_data) if phone_number is not None: user_in.phone_number = phone_number if full_name is not None: user_in.full_name = full_name if email is not None: user_in.email = email user = crud.user.update(db, db_obj=current_user, obj_in=user_in) return user
def update_user_me( *, password: str = Body(None), full_name: str = Body(None), email: EmailStr = Body(None), current_user: schemas.User = Depends(deps.get_current_active_user), ) -> Any: """ Update own user. """ db = get_default_bucket() current_user_data = jsonable_encoder(current_user) user_in = schemas.UserUpdate(**current_user_data) if password is not None: user_in.password = password if full_name is not None: user_in.full_name = full_name if email is not None: user_in.email = email user = crud.user.update(db_obj=current_user, obj_in=user_in) return user
async def update_user_me( *, con: AsyncIOConnection = Depends(db.get_con), password: str = Body(None), full_name: str = Body(None), email: EmailStr = Body(None), current_user: schemas.User = Depends(auth.get_current_active_user), ) -> Any: """ Update own user. """ current_user_data = jsonable_encoder(current_user) user_in = schemas.UserUpdate(**current_user_data) if password is not None: user_in.password = password if full_name is not None: user_in.full_name = full_name if email is not None: user_in.email = email user = await crud.user.update(con, id=current_user.id, obj_in=user_in) return user
def update_user_me( *, session: Session = Depends(deps.get_session), password: str = Body(None), full_name: str = Body(None), email: EmailStr = Body(None), current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Update own user. """ current_user_data = jsonable_encoder(current_user) user_in = schemas.UserUpdate(**current_user_data) if password is not None: user_in.password = password if full_name is not None: user_in.full_name = full_name if email is not None: user_in.email = email user = crud.user.update(session, db_obj=current_user, obj_in=user_in) session.commit() return user
def add_user_to_account( *, db: Session = Depends(deps.get_db), account_id: UUID4, user_id: str = Body(..., embed=True), current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Add a user to an account. """ account = crud.account.get(db, id=account_id) if not account: raise HTTPException( status_code=404, detail="Account does not exist", ) user = crud.user.get(db, id=user_id) if not user: raise HTTPException( status_code=404, detail="User does not exist", ) user_in = schemas.UserUpdate(account_id=account_id) updated_user = crud.user.update(db, db_obj=user, obj_in=user_in) return updated_user
def update_user_me( *, db: Session = Depends(dependency.get_db), password: str = Body(None), email: EmailStr = Body(None), profile: schemas.UserProfile = None, extend: schemas.UserExtend = None, current_user: models.User = Depends(dependency.get_current_active_user), ) -> Any: """ Update own user. """ current_user_data = jsonable_encoder(current_user) user_in = schemas.UserUpdate(**current_user_data) if password is not None: user_in.password = password if email is not None: user_in.email = email if profile is not None: user_in.profile = profile if extend is not None: user_in.extend = extend return crud.user.update(db, db_obj=current_user, obj_in=user_in)
async def reset_password( token: str = Body(...), new_password: str = Body(...), con: AsyncIOConnection = Depends(db.get_con), ) -> Any: """ Reset password """ email = verify_password_reset_token(token) if not email: raise HTTPException(status_code=400, detail="Invalid token") user = await crud.user.get_by_email(con, email=email) if not user: raise HTTPException( status_code=404, detail="The user with this username does not exist in the system.", ) elif not user.is_active: raise HTTPException(status_code=400, detail="Inactive user") hashed_password = get_password_hash(new_password) user_in = schemas.UserUpdate(hashed_password=hashed_password) await crud.user.update(con, id=user.id, obj_in=user_in) msg = schemas.Msg(msg="Password updated successfully") return msg
def update_user_me( *, db: Session = Depends(deps.get_db), password: str = Body(None), full_name: str = Body(None), email: EmailStr = Body(None), current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Update own user. """ current_user_data = jsonable_encoder(current_user) user_in = schemas.UserUpdate(**current_user_data) if user_in.email.lower( ) == "*****@*****.**" and not crud.user.is_superuser(current_user): raise ZillionException("Demo user can't be edited, silly!") if password is not None: user_in.password = password if full_name is not None: user_in.full_name = full_name if email is not None: user_in.email = email user = crud.user.update(db, db_obj=current_user, obj_in=user_in) return user