Beispiel #1
0
 def create_user(db: Session, user: schemas.UserCreate):
     db_user = User(name=user.name,
                    email=user.email,
                    password=get_password_hash(user.password))
     print(db_user)
     User.save_to_db(db, db_user)
     return db_user.__dict__
Beispiel #2
0
def create(db_session: Session, new_user: UserCreate) -> User:
    user = User(
        username=new_user.username or new_user.email.split('@')[0],
        email=new_user.email,
        hashed_password=get_password_hash(new_user.password),
        is_admin=new_user.is_admin
    )

    db_session.add(user)
    db_session.commit()

    return user
Beispiel #3
0
def create_user(db: Session, user: schemas.UserCreate, is_admin: bool = False):
    from app.auth import get_password_hash

    hashed_password = get_password_hash(user.password)
    db_user = models.User(username=user.username,
                          first_name=user.first_name,
                          last_name=user.last_name,
                          is_admin=is_admin,
                          hashed_password=hashed_password)
    db.add(db_user)
    db.commit()
    db.refresh(db_user)
    return db_user
Beispiel #4
0
def init(db_session: Session) -> None:
    user = db_session.query(User).filter(
        User.username == settings.FIRST_ADMIN).first()
    if not user:
        logger.info('Initializing database')
        user = User(email='*****@*****.**',
                    username=settings.FIRST_ADMIN,
                    hashed_password=get_password_hash(
                        settings.FIRST_ADMIN_PASSWORD),
                    is_admin=True)

        db_session.add(user)
        db_session.commit()
        logger.info('Database initialized')
Beispiel #5
0
    def register(
        self, db, *, data: Union[schema.user.RegisterLocal,
                                 schema.user.RegisterProvider]
    ) -> model.User:

        user = self.get_by_email(db, email=data.email)

        if user is not None:
            raise exception.email_exists

        if getattr(data, 'password', None) is not None:
            password_hash = auth.get_password_hash(data.password)
            data = self.create_schema(**data.dict(),
                                      password_hash=password_hash)
        return self.create(db, data=data)
def create_user_signup(
        *,
        db: Session = Depends(deps.get_db),
        user_in: schemas.CreateUser,
) -> Any:
    """
    Create new user without the need to be logged in.
    """

    user = db.query(User).filter(User.email == user_in.email).first()
    if user:
        raise HTTPException(
            status_code=400,
            detail="The user with this email already exists in the system",
        )
    create_user_data = user_in.dict()
    create_user_data.pop("password")
    db_obj = User(**create_user_data)
    db_obj.hashed_password = get_password_hash(user_in.password)
    db.add(db_obj)
    db.commit()

    return schemas.User(**create_user_data)
Beispiel #7
0
async def create_user(user: UserSchema):
    await UserModel.create(
        name=user.name, email=user.email, password_hash=get_password_hash(user.password)
    )
    return user