Esempio n. 1
0
 def update(self, db: Session, *, db_obj: User, obj_in: UserUpdate) -> User:
     update_data = obj_in.dict(exclude_unset=True)
     if update_data.get("password"):
         hashed_password = get_password_hash(update_data["password"])
         del update_data["password"]
         update_data["hashed_password"] = hashed_password
     return super().update(db, db_obj=db_obj, obj_in=update_data)
Esempio n. 2
0
 def update(self, db_session: Session, *, db_obj: User, obj_in: UserUpdate) -> User:
     if obj_in.password:
         update_data = obj_in.dict(exclude_unset=True)
         hashed_password = get_password_hash(obj_in.password)
         del update_data["password"]
         update_data["hashed_password"] = hashed_password
         use_obj_in = UserInDB.parse_obj(update_data)
     return super().update(db_session, db_obj=db_obj, obj_in=use_obj_in)
Esempio n. 3
0
def update(db_session: Session, *, user: User, user_in: UserUpdate) -> User:
    user_data = jsonable_encoder(user)
    update_data = user_in.dict(skip_defaults=True)
    for field in user_data:
        if field in update_data:
            setattr(user, field, update_data[field])
    if user_in.password:
        passwordhash = get_password_hash(user_in.password)
        user.hashed_password = passwordhash
    db_session.add(user)
    db_session.commit()
    db_session.refresh(user)
    return user
Esempio n. 4
0
def update(
      user_to_update: schemas.UserUpdate,
      db_session: Session = Depends(get_db),
      current_user: models.User = Depends(get_current_user)
):
    db_user = crud.get_by_email(db_session, user_to_update.email)

    if not db_user:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f'User with email "{user_to_update.email}" not found.'
        )

    return crud.update(
        db_session,
        db_user=db_user,
        user_to_update=user_to_update.dict(exclude={'email'}),
        updated_by_admin=current_user.is_admin
    )