async def create_user(user: UserInDB): if db.users.find({'username': user.username}).count() > 0: raise HTTPException(status_code=409, detail="User exists") pwd = get_password_hash(user.hashed_password) user.hashed_password = pwd ret = db.users.insert_one(user.dict(by_alias=True)) user.id = ret.inserted_id return {'user': user}
async def register_user(request: RegisterRequest, client: AsyncIOMotorClient = Depends(get_nosql_db)): try: collection = client[getenv("MONGODB_NAME")]["user"] user = create_user(request) dbuser = UserInDB(**user.dict()) response = await collection.insert_one(dbuser.dict()) return {"user_id": str(response.inserted_id)} except DuplicateKeyError as e: print(e) return JSONResponse(status_code=400, content={"user_id": "user_alreay exists"})
async def create_user(request, collection): salt = uuid.uuid4().hex hashed_password = hashlib.sha512( request.password.encode("utf-8") + salt.encode("utf-8")).hexdigest() user = {} user["username"] = request.username user["salt"] = salt user["hashed_password"] = hashed_password # user = User(**user) dbuser = UserInDB(**user) response = await collection.insert_one(dbuser.dict()) return {"id_inserted": str(response.inserted_id)}
async def get_user(username: str): client = await get_nosql_db() users_db = client[MONGODB_NAME]["user"] row = await users_db.find_one({"username": username}) if row is not None: return UserInDB(**row) else: return None
async def get_user(name) -> UserInDB: client = await get_nosql_db() db = client[MONGODB_DB_NAME] collection = db.users row = await collection.find_one({"username": name}) if row is not None: return UserInDB(**row) else: return None
def add_user(user: LoginUser): session = get_session() new_user = UserInDB(email=user.email, name=user.name, hashed_password=get_password_hash(user.password)) session.add(new_user) session.commit() return make_user_from_db(new_user)
async def user_register(user: UserReg, engine: AsyncEngine = Depends(async_return_engine)): try: async with AsyncSession(engine) as session: async with session.begin(): storage = hash_password(user.password) session.add(UserInDB(user.username, storage[0])) session.add(Salt(user.username, storage[1])) await session.commit() return {'result': 'user has been registered'} except IntegrityError: raise HTTPException(status_code=409, detail="User with this username already exist.")
def get_user(username: str): """ Function to return user for username from database. :param username: :return: """ user = db.users.find_one({'username': username}) if user is not None: return UserInDB(**user) else: return HTTPException(404, 'User is not exist')
async def create_user(user: UserIn): users = db.users check_user = users.find_one({'username': user.username}) if check_user is None: dict_in = user.dict() dict_in.update({'hashed_password': get_password_hash(user.password)}) user_id = users.insert_one(UserInDB(**dict_in).dict()).inserted_id if user_id: return {'user_id': str(user_id)} else: return HTTPException(423, 'Can\'t create user') else: return HTTPException(404, 'User already exist')
def get_user(db, username: str): if username in db: user_dict = db[username] return UserInDB(**user_dict)
def get_user(db, username: str): user = db.users.find_one({'username': username}) return UserInDB(**user)
def get_user(client: Collection, username: str): user_dict = client.find_one({"username": username}) if user_dict: return UserInDB(**user_dict)