From 6cc43005f0044fb16679f8454cee273769551ecb Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 7 Nov 2023 21:08:58 +0100 Subject: [PATCH] add name --- app/routers/events.py | 26 +++++--------------------- app/routers/users.py | 33 +++++++++++---------------------- 2 files changed, 16 insertions(+), 43 deletions(-) diff --git a/app/routers/events.py b/app/routers/events.py index 1705703..51fd6ab 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -11,7 +11,7 @@ router = APIRouter() @router.get("/events", tags=["events"], response_model=list[events.EventOut]) -async def read_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], skip: int = 0, limit: int = 20, id_event: str | None = None, status: int | None = None): +async def read_events(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20, id_event: str | None = None, name: str | None = None, status: int | None = None): if limit < 1 or skip < 0 or limit < skip: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -28,6 +28,10 @@ async def read_users(authorize: Annotated[bool, Depends(permissions_checker.Perm object_search = {"id": {"$regex": userid}} if status is not None: object_search = {"$and":[{"id":{"$regex": eventid}}, {"status":{"$eq":status}}]} + if name is not None: + object_search = {"name": {"$regex": name}} + if status is not None: + object_search = {"$and":[{"name":{"$regex": name}}, {"status":{"$eq":status}}]} for event_index in event_repository.find_by(object_search, limit=limit, skip=skip): @@ -35,26 +39,6 @@ async def read_users(authorize: Annotated[bool, Depends(permissions_checker.Perm listEvents.append(event) return listEvents -@router.get("/users/search", tags=["users"], response_model=list[users.UserOut]) -async def read_users_search(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20): - if limit < 1 or skip < 0 or limit < skip: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="skip should be greater than 0 and limit should be greater than 1. Limit should be greater than skip" - ) - if key is None or value is None: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="Key or/and value parameter is empty" - ) - limit = limit + skip - listUsers = [] - user_repository = users.UserRepository(database=database.database) - for user_index in user_repository.find_by({key: {'$regex': value}}, limit=limit, skip=skip): - user = users.UserOut(id=user_index.id, username=user_index.username, status=user_index.status, roles=user_index.roles, email=user_index.email, firstName=user_index.firstName, name=user_index.name) - listUsers.append(user) - return listUsers - @router.get("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"]) async def read_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): diff --git a/app/routers/users.py b/app/routers/users.py index 5a7444b..fdbda41 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -11,7 +11,7 @@ router = APIRouter() @router.get("/users", tags=["users"], response_model=list[users.UserOut]) -async def read_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], skip: int = 0, limit: int = 20, id_user: str | None = None, roles: str | None = None, status: int | None = None, email: EmailStr | None = None): +async def read_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20, id_user: str | None = None, roles: str | None = None, status: int | None = None, email: EmailStr | None = None, name: str | None = None): if limit < 1 or skip < 0 or limit < skip: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -48,33 +48,22 @@ async def read_users(authorize: Annotated[bool, Depends(permissions_checker.Perm if roles is not None: object_search = {"$and":[{"email":{"$eq": email}}, {"roles":{"$eq":roles}}]} + if name is not None: + object_search = {"username": {"$regex": name}} + if status is not None and roles is not None: + object_search = {"$and":[{"username":{"$regex": name}}, {"roles":{"$eq": roles}}, {"status":{"$eq":status}}]} + else: + if status is not None: + object_search = {"$and":[{"username":{"$regex": name}}, {"status":{"$eq":status}}]} + if roles is not None: + object_search = {"$and":[{"username":{"$regex": name}}, {"roles":{"$eq":roles}}]} + for user_index in user_repository.find_by(object_search, limit=limit, skip=skip): user = users.UserOut(id=user_index.id, username=user_index.username, email=user_index.email, status=user_index.status, roles=user_index.roles, firstName=user_index.firstName, name=user_index.name) listUsers.append(user) return listUsers -@router.get("/users/search", tags=["users"], response_model=list[users.UserOut]) -async def read_users_search(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20): - if limit < 1 or skip < 0 or limit < skip: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="skip should be greater than 0 and limit should be greater than 1. Limit should be greater than skip" - ) - if key is None or value is None: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="Key or/and value parameter is empty" - ) - limit = limit + skip - listUsers = [] - user_repository = users.UserRepository(database=database.database) - for user_index in user_repository.find_by({key: {'$regex': value}}, limit=limit, skip=skip): - user = users.UserOut(id=user_index.id, username=user_index.username, status=user_index.status, roles=user_index.roles, email=user_index.email, firstName=user_index.firstName, name=user_index.name) - listUsers.append(user) - return listUsers - - @router.get("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"]) async def read_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): return current_user