Delete flights associated with user on user delete

This commit is contained in:
april 2023-12-29 08:59:47 -06:00
parent e788536956
commit c2a649852d
3 changed files with 17 additions and 16 deletions

View File

@ -4,7 +4,7 @@ from bson import ObjectId
from fastapi import HTTPException from fastapi import HTTPException
from database.utils import user_helper, create_user_helper, system_user_helper from database.utils import user_helper, create_user_helper, system_user_helper
from .db import user_collection from .db import user_collection, flight_collection
from routes.utils import get_hashed_password from routes.utils import get_hashed_password
from schemas.user import UserDisplaySchema, UserCreateSchema, UserSystemSchema, AuthLevel from schemas.user import UserDisplaySchema, UserCreateSchema, UserSystemSchema, AuthLevel
@ -84,7 +84,7 @@ async def get_user_system_info(username: str) -> UserSystemSchema:
async def delete_user(id: str) -> UserDisplaySchema: async def delete_user(id: str) -> UserDisplaySchema:
""" """
Delete given user from the database Delete given user and all associated flights from the database
:param id: ID of user to delete :param id: ID of user to delete
:return: Information of deleted user :return: Information of deleted user
@ -95,6 +95,10 @@ async def delete_user(id: str) -> UserDisplaySchema:
raise HTTPException(404, "User not found") raise HTTPException(404, "User not found")
await user_collection.delete_one({"_id": ObjectId(id)}) await user_collection.delete_one({"_id": ObjectId(id)})
# Delete all flights associated with user
await flight_collection.delete_many({"user": ObjectId(id)})
return UserDisplaySchema(**user_helper(user)) return UserDisplaySchema(**user_helper(user))

View File

@ -13,7 +13,8 @@ router = APIRouter()
logger = logging.getLogger("flights") logger = logging.getLogger("flights")
@router.get('/', summary="Get flights logged by the currently logged-in user", status_code=200) @router.get('/', summary="Get flights logged by the currently logged-in user", status_code=200,
response_model=list[FlightConciseSchema])
async def get_flights(user: UserDisplaySchema = Depends(get_current_user)) -> list[FlightConciseSchema]: async def get_flights(user: UserDisplaySchema = Depends(get_current_user)) -> list[FlightConciseSchema]:
""" """
Get a list of the flights logged by the currently logged-in user Get a list of the flights logged by the currently logged-in user
@ -26,7 +27,7 @@ async def get_flights(user: UserDisplaySchema = Depends(get_current_user)) -> li
@router.get('/all', summary="Get all flights logged by all users", status_code=200, @router.get('/all', summary="Get all flights logged by all users", status_code=200,
dependencies=[Depends(admin_required)]) dependencies=[Depends(admin_required)], response_model=list[FlightConciseSchema])
async def get_all_flights() -> list[FlightConciseSchema]: async def get_all_flights() -> list[FlightConciseSchema]:
""" """
Get a list of all flights logged by any user Get a list of all flights logged by any user
@ -39,7 +40,7 @@ async def get_all_flights() -> list[FlightConciseSchema]:
@router.get('/{flight_id}', summary="Get details of a given flight", response_model=FlightDisplaySchema, @router.get('/{flight_id}', summary="Get details of a given flight", response_model=FlightDisplaySchema,
status_code=200) status_code=200)
async def get_flight(flight_id: str, user: UserDisplaySchema = Depends(get_current_user)): async def get_flight(flight_id: str, user: UserDisplaySchema = Depends(get_current_user)) -> FlightDisplaySchema:
""" """
Get all details of a given flight Get all details of a given flight
@ -56,7 +57,7 @@ async def get_flight(flight_id: str, user: UserDisplaySchema = Depends(get_curre
@router.post('/', summary="Add a flight logbook entry", status_code=200) @router.post('/', summary="Add a flight logbook entry", status_code=200)
async def add_flight(flight_body: FlightCreateSchema, user: UserDisplaySchema = Depends(get_current_user)): async def add_flight(flight_body: FlightCreateSchema, user: UserDisplaySchema = Depends(get_current_user)) -> dict:
""" """
Add a flight logbook entry Add a flight logbook entry
@ -89,13 +90,13 @@ async def update_flight(flight_id: str, flight_body: FlightCreateSchema,
logger.info("Attempted access to unauthorized flight by %s", user.username) logger.info("Attempted access to unauthorized flight by %s", user.username)
raise HTTPException(403, "Unauthorized access") raise HTTPException(403, "Unauthorized access")
updated_flight = await db.update_flight(flight_body, flight_id) updated_flight_id = await db.update_flight(flight_body, flight_id)
return str(updated_flight) return str(updated_flight_id)
@router.delete('/{flight_id}', summary="Delete the given flight", status_code=200) @router.delete('/{flight_id}', summary="Delete the given flight", status_code=200, response_model=FlightDisplaySchema)
async def delete_flight(flight_id: str, user: UserDisplaySchema = Depends(get_current_user)): async def delete_flight(flight_id: str, user: UserDisplaySchema = Depends(get_current_user)) -> FlightDisplaySchema:
""" """
Delete the given flight Delete the given flight

View File

@ -41,7 +41,7 @@ async def add_user(body: UserCreateSchema) -> dict:
@router.delete('/{user_id}', summary="Delete given user and all associated flights", status_code=200, @router.delete('/{user_id}', summary="Delete given user and all associated flights", status_code=200,
dependencies=[Depends(admin_required)]) dependencies=[Depends(admin_required)])
async def remove_user(user_id: str) -> None: async def remove_user(user_id: str) -> UserDisplaySchema:
""" """
Delete given user from database along with all flights associated with said user Delete given user from database along with all flights associated with said user
@ -54,12 +54,8 @@ async def remove_user(user_id: str) -> None:
if not deleted: if not deleted:
logger.info("Attempt to delete nonexistent user %s", user_id) logger.info("Attempt to delete nonexistent user %s", user_id)
raise HTTPException(401, "User does not exist") raise HTTPException(401, "User does not exist")
# except ValidationError:
# logger.debug("Invalid user delete request")
# raise HTTPException(400, "Invalid user")
# Delete all flights associated with the user TODO return deleted
# Flight.objects(user=user_id).delete()
@router.get('/', summary="Get a list of all users", status_code=200, response_model=list[UserDisplaySchema], @router.get('/', summary="Get a list of all users", status_code=200, response_model=list[UserDisplaySchema],