change response return

This commit is contained in:
Valentin CZERYBA 2023-11-04 20:45:07 +01:00
parent 82750bbd0c
commit dd04cedf0e
2 changed files with 45 additions and 17 deletions

View File

@ -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

View File

@ -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
content = {"message": "user is enabled"}
response = JSONResponse(content=content)
return response