diff --git a/app/models/users.py b/app/models/users.py index 9d71f9d..9064d53 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -14,8 +14,9 @@ class User(BaseModel): birth: str | None = None created_at: datetime = datetime.today() connected_at: datetime | None = None - updated_at: datetime = datetime.today() + updated_at: datetime | None = None deleted_at: datetime | None = None + disabled_at: datetime | None = None class UserOut(BaseModel): id: ObjectIdField = None diff --git a/app/routers/users.py b/app/routers/users.py index 309ce13..4940019 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -94,14 +94,21 @@ async def read_users_id(item_id : str, authorize: Annotated[bool, Depends(permis return user -@router.delete("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"]) +@router.delete("/users/me",tags=["users"]) async def delete_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], remove: bool = False): user_repository = users.UserRepository(database=database.database) - current_user.status = 0 + if remove is True: + current_user.deleted_at = datetime.today() current_user.status = -1 + content = {"message": "users are deleted"} + else: + current_user.status = 0 + current_user.deleted_at = datetime.today() + content = {"message": "users are disabled"} user_repository.save(current_user) - return current_user + response = JSONResponse(content=content) + return response @router.delete("/users/groups",tags=["users"]) async def delete_users_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove: bool = False, userids: users.UserIDS | None = None): @@ -112,30 +119,38 @@ async def delete_users_groups(authorize: Annotated[bool, Depends(permissions_che ) user_repository = users.UserRepository(database=database.database) - content = {"message": "users are disabled"} for i in userids.ids: user = user_repository.find_one_by_id(ObjectId(i)) - user.status = 0 if remove is True: user.status = -1 + user.deleted_at = datetime.today() content = {"message": "users are deleted "} + else: + user.status = 0 + user.disabled_at = datetime.today() + content = {"message": "users are disabled"} user_repository.save(user) - response = JSONResponse(content=content) return response -@router.delete("/users/{item_id}", tags=["users"], response_model=users.User) +@router.delete("/users/{item_id}", tags=["users"]) async def delete_users_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove : bool = False): user_repository = users.UserRepository(database=database.database) user = user_repository.find_one_by_id(ObjectId(item_id)) - user.status = 0 if remove is True: user.status = -1 + user.deleted_at = datetime.today() + content = {"message": "users are deleted"} + else: + user.status = 0 + user.disabled_at = datetime.today() + content = {"message": "users are disabled"} user_repository.save(user) - return user + response = JSONResponse(content=content) + return response -@router.put("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"]) +@router.put("/users/me",tags=["users"]) async def update_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], userSingle: users.UserIn | None = None): user_repository = users.UserRepository(database=database.database) current_user.username = userSingle.username @@ -143,9 +158,11 @@ async def update_users_me(current_user: Annotated[users.User, Depends(users_toke current_user.roles = userSingle.roles current_user.email = userSingle.email user_repository.save(current_user) + content = {"message": "user is updated"} + response = JSONResponse(content=content) return current_user -@router.put("/users", tags=["users"], response_model=users.User, status_code=status.HTTP_201_CREATED) +@router.put("/users", tags=["users"], status_code=status.HTTP_201_CREATED) async def update_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None, response: Response = Response): if userSingle is None: raise HTTPException( @@ -174,12 +191,14 @@ async def update_users(authorize: Annotated[bool, Depends(permissions_checker.Pe user.firstName = userSingle.firstName user.name = userSingle.name user.birth = userSingle.birth - user.updated_at = datetime.today() + user.created_at = datetime.today() user_repository.save(user) - return user + content = {"message": "user is created"} + response = JSONResponse(content=content) + return response -@router.put("/users/{item_id}", tags=["users"], response_model=users.User, status_code=status.HTTP_200_OK) +@router.put("/users/{item_id}", tags=["users"], status_code=status.HTTP_200_OK) async def update_users_id(item_id: str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None, response: Response = Response): if userSingle is None: raise HTTPException( @@ -204,7 +223,9 @@ async def update_users_id(item_id: str, authorize: Annotated[bool, Depends(permi user.birth = userSingle.birth user.updated_at = datetime.today() user_repository.save(user) - return user + content = {"message": "user is updated"} + response = JSONResponse(content=content) + return response @router.patch("/users/groups",tags=["users"]) @@ -220,6 +241,8 @@ async def patch_users_groups(authorize: Annotated[bool, Depends(permissions_chec for i in userids.ids: user = user_repository.find_one_by_id(ObjectId(i)) user.status = 1 + user.disabled_at = None + user.deleted_at = None user_repository.save(user) @@ -231,5 +254,9 @@ async def patch_users_id(item_id : str, authorize: Annotated[bool, Depends(permi user_repository = users.UserRepository(database=database.database) user = user_repository.find_one_by_id(ObjectId(item_id)) user.status = 1 + user.disabled_at = None + user.deleted_at = None user_repository.save(user) - return user \ No newline at end of file + content = {"message": "user is enabled"} + response = JSONResponse(content=content) + return response \ No newline at end of file