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.password = passwordhash db_session.add(user) db_session.commit() db_session.refresh(user) return user
def update_in_db(bucket: Bucket, *, username: str, user_in: UserUpdate, persist_to=0): user_doc_id = get_doc_id(username) stored_user = get(bucket, username=username) stored_user = stored_user.copy(update=user_in.dict(skip_defaults=True)) if user_in.password: passwordhash = get_password_hash(user_in.password) stored_user.hashed_password = passwordhash data = jsonable_encoder(stored_user) with bucket.durability(persist_to=persist_to, timeout=config.COUCHBASE_DURABILITY_TIMEOUT_SECS): bucket.upsert(user_doc_id, data) return stored_user
def update(self, id: str, user_update: UserUpdate) -> User: data = user_update.dict(exclude_none=True) doc_ref = self.collection_ref.document(id) doc_ref.update(data) return self.get(id)