async def update_parent( parent: ParentUpdatePydantic, current_parent: Parent = Depends(auth.get_current_parent), ): if parent.password != parent.second_password: raise HTTPException( status.HTTP_400_BAD_REQUEST, "first and second passwords do not match", ) if parent.password: parent.password = get_password_hash(parent.password) await crud.update_parent( current_parent.parent_id, parent.dict(exclude_defaults=True, exclude={"second_password"}), ) db_parent = await crud.get_parent_by_username(current_parent.username) return await ParentPydantic.from_tortoise_orm(db_parent)
async def create_parent(parent: ParentCreatePydantic, request: Request): db_parent = await crud.get_parent_by_username(parent.username) if db_parent: raise HTTPException(status.HTTP_400_BAD_REQUEST, "Username is already exist") if parent.password != parent.second_password: raise HTTPException( status.HTTP_400_BAD_REQUEST, "first and second passwords do not match", ) parent.password = get_password_hash(parent.password) placeholder_link = f"{request.base_url}photos/placeholder.jpg" parent_dict = parent.dict(exclude={"second_password"}) parent_dict["photo_link"] = placeholder_link db_parent = await crud.create_parent(parent_dict) access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) access_token = auth.create_access_token( data={"sub": db_parent.username}, expires_delta=access_token_expires ) return {"access_token": access_token, "token_type": "bearer"}
def change_password(self, password: str) -> None: self.salt = security.generate_salt() self.hashed_password = security.get_password_hash(self.salt + password)
def change_secret(self, secret: str) -> None: self.salt = security.generate_salt() self.hashed_secret = security.get_password_hash(self.salt + secret)
db_uri = "mongodb://" + db_username + ":" + db_password + "@mongo:27017/" client = MongoClient(db_uri) # Imports 'users' and 'vehicles' data from file system CSVs to the MongoDB database dbnames = client.list_database_names() if str(config.DB_NAME) not in dbnames: THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(THIS_FOLDER, "users.csv")) as csv_file: fieldnames = ("username", "password") reader = csv.DictReader(csv_file, fieldnames) next(reader) out = json.dumps([row for row in reader]) users = json.loads(out) for user in users: user["password"] = get_password_hash(user["password"]) print(user) client[str(config.DB_NAME)].users.insert_one(user) with open(os.path.join(THIS_FOLDER, "vehicles.csv")) as csv_file: fieldnames = ("id", "distance", "owner") reader = csv.DictReader(csv_file, fieldnames) next(reader) out = json.dumps([row for row in reader]) vehicles = json.loads(out) for vehicle in vehicles: print(vehicle) client[str(config.DB_NAME)].vehicles.insert_one(vehicle) # Returns a MongoDB connection def get_db_connection():
def test_get_password_hash_and_verify_password(): hashed_p = get_password_hash("test") assert verify_password("test", hashed_p)