def create_user(db: Session, user: schemas.users.UserCreate): db_user = models.User( id=random.randint(10000000,99999999), document=user.document, names=user.names, last_names=user.last_names, phone_number=user.phone_number, email=user.email, adress= user.adress, birthdate=user.birthdate, bank=user.bank, bank_account=user.bank_account, password=security.get_password_hash(user.password), upline_code=user.upline_code, suscription_code=( ''.join(random.choice(string.ascii_uppercase) for i in range(10))), is_active= False, is_admin= False, created_at = datetime.now(), updated_at = datetime.now(), token_session = 'PRUEBA TOKEN' ) db.add(db_user) db.commit() db.refresh(db_user) return db_user
async def create(con: AsyncIOConnection, *, obj_in: UserCreate) -> User: try: result = await con.fetchone_json( """SELECT ( INSERT User { full_name := <str>$full_name, email := <str>$email, hashed_password := <str>$hashed_password, is_superuser := <bool>$is_superuser, is_active := <bool>$is_active } ) { id, full_name, email, is_superuser, is_active, num_items := count(.<owner[IS Item]), items:= .<owner[IS Item] { id, title } }""", full_name=obj_in.full_name, email=obj_in.email, hashed_password=get_password_hash(obj_in.password), is_superuser=obj_in.is_superuser, is_active=obj_in.is_active, ) except Exception as e: print(f"EXCEPTION: {e}") user = User.parse_raw(result) return user
def create_user(db: Session, user: schemas.UserCreate) -> User: hashed_password = get_password_hash(user.password) db_user = User(email=user.email, hashed_password=hashed_password) db.add(db_user) db.commit() db.refresh(db_user) return db_user
def create(self, db: Session, obj_in: UserCreateSchema) -> User: hashed_password = get_password_hash(obj_in.password.get_secret_value()) user = User(login=obj_in.login, hashed_password=hashed_password) db.add(user) db.commit() db.refresh(user) return user
async def update(con: AsyncIOConnection, *, 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 shape = ", ".join( [k + db.type_cast(update_data[k]) + k for k in update_data]) try: result = await con.fetchone_json( f"""SELECT ( UPDATE User FILTER .id = <uuid>$id SET {{ {shape} }} ) {{ id, full_name, email, is_superuser, is_active, num_items := count(.<owner[IS Item]), items:= .<owner[IS Item] {{ id, title }} }}""", id=db_obj.id, **update_data, ) except Exception as e: print(f"EXCEPTION: {e}") user = User.parse_raw(result) return user
async def create(con: AsyncIOConnection, *, obj_in: UserCreate) -> User: data_in = obj_in.dict(exclude_unset=True) if data_in.get("password"): data_in["hashed_password"] = get_password_hash(obj_in.password) del data_in["password"] shape_expr = utils.get_shape(data_in) try: result = await con.query_one_json( f"""SELECT ( INSERT User {{ {shape_expr} }} ) {{ id, email, full_name, is_superuser, is_active, num_items, items: {{ id, title }} }}""", **data_in, ) except Exception as e: raise HTTPException(status_code=400, detail=f"{e}") user = User.parse_raw(result) return user
def create_user(db: Session, user: users_schemas.UserCreate): hashed_password = security.get_password_hash(user.password) db_user = users_models.User(email=user.email, hashed_password=hashed_password) db.add(db_user) db.commit() db.refresh(db_user) return db_user
def create(self, db_session: Session, *, obj_in: UserCreate) -> User: db_obj = User(name=obj_in.name, password=get_password_hash(obj_in.password), date=datetime.now()) db_session.add(db_obj) db_session.commit() db_session.refresh(db_obj) return db_obj
def create(self, db: Session, object_in: UserCreate) -> User: obj = self.model( username=object_in.username, password_hashed=get_password_hash(object_in.password), is_superuser=object_in.is_superuser, ) db.add(obj) db.commit() db.refresh(obj) return obj
def update(self, db: Session, db_obj: User, obj_in: UserUpdateSchema) -> User: if obj_in.password: db_obj.hashed_password = get_password_hash( obj_in.password.get_secret_value()) if obj_in.login: db_obj.login = obj_in.login db.add(db_obj) db.commit() return db_obj
def create(self, db: Session, *, obj_in: UserCreate) -> User: obj_in_data = jsonable_encoder(obj_in) hashed_password = get_password_hash(obj_in_data["password"]) del obj_in_data["password"] obj_in_data["hashed_password"] = hashed_password db_obj = User(**obj_in_data) db.add(db_obj) db.commit() db.refresh(db_obj) return db_obj
def update(self, db: Session, *, db_obj: User, obj_in: Union[UserUpdate, Dict[str, Any]]) -> User: if isinstance(obj_in, dict): update_data = obj_in else: 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)
def create_user(db_session: Session, user: user.UserCreate): password_hash = get_password_hash(user.password) db_user = models.User(email=user.email, password_hash=password_hash, name=user.name, surname=user.surname, username=user.username) db_session.add(db_user) db_session.commit() db_session.refresh(db_user) return db_user
def test_create_user_new_email( client: TestClient, db: Session, user_data: Dict[str, str] ) -> None: r = client.post("/api/v1/users", json=user_data) assert r.status_code == 200 created_user = r.json() user = crud.user.get_by_username(db, username=user_data["username"]) assert user assert user.email == created_user["email"] assert user.username == created_user["username"] assert user.hashed_password[:6] == get_password_hash(user_data["password"])[:6]
async def update(con: AsyncIOConnection, *, id: UUID, obj_in: UserUpdate) -> Optional[User]: data_in = obj_in.dict(exclude_unset=True) if not data_in: user = await get(con, id=id) return user if data_in.get("password"): data_in["hashed_password"] = get_password_hash( obj_in.password) # type: ignore del data_in["password"] shape_expr = utils.get_shape(data_in) try: result = await con.query_one_json( f"""SELECT ( UPDATE User FILTER .id = <uuid>$id SET {{ {shape_expr} }} ) {{ id, email, full_name, is_superuser, is_active, num_items, items: {{ id, title }} }}""", id=id, **data_in, ) except Exception as e: raise HTTPException(status_code=400, detail=f"{e}") user = User.parse_raw(result) return user
def reset_password( token: str = Body(...), new_password: str = Body(...), db: Session = Depends(deps.get_db), ): """ Reset password """ email = verify_password_reset_token(token) if not email: raise HTTPException(status_code=400, detail="Invalid token") user = crud.user.get_by_email(db, email=email) if not user: raise HTTPException( status_code=404, detail="The user with this username does not exist in the system.", ) elif not crud.user.is_active(user): raise HTTPException(status_code=400, detail="Inactive user") hashed_password = get_password_hash(new_password) user.hashed_password = hashed_password db.add(user) db.commit() return {"msg": "Password updated successfully"}
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
class UserFactory(BaseModelFactory): class Meta: model = User login = factory.Sequence(lambda n: f'user{n}') hashed_password = factory.Sequence(lambda n: get_password_hash(f'password{n}'))