Migrate to motor for DB interaction
This commit is contained in:
@@ -3,48 +3,43 @@ import logging
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
|
||||
from app.deps import get_current_user, admin_required
|
||||
from schemas import FlightModel, GetSystemUserSchema
|
||||
from database import flights as db
|
||||
from schemas.flight import FlightConciseSchema, FlightDisplaySchema, FlightCreateSchema
|
||||
|
||||
from mongoengine import ValidationError
|
||||
|
||||
from database.models import Flight, AuthLevel
|
||||
from database.utils import get_flight_list
|
||||
from schemas.user import UserDisplaySchema, AuthLevel
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
logger = logging.getLogger("flights")
|
||||
|
||||
|
||||
@router.get('/flights', summary="Get flights logged by the currently logged-in user", status_code=200)
|
||||
async def get_flights(user: GetSystemUserSchema = Depends(get_current_user)) -> list[FlightModel]:
|
||||
@router.get('/', summary="Get flights logged by the currently logged-in user", status_code=200)
|
||||
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
|
||||
|
||||
:return: List of flights
|
||||
"""
|
||||
# l = get_flight_list(filters=[[{"field": "user", "operator": "eq", "value": user.id}]])
|
||||
l = get_flight_list(user=str(user.id))
|
||||
flights = []
|
||||
for f in l:
|
||||
flights.append(FlightModel(**f.to_mongo()))
|
||||
return [f.to_mongo() for f in flights]
|
||||
flights = await db.retrieve_flights(user.id)
|
||||
return flights
|
||||
|
||||
|
||||
@router.get('/flights/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)])
|
||||
def get_all_flights() -> list[FlightModel]:
|
||||
async def get_all_flights() -> list[FlightConciseSchema]:
|
||||
"""
|
||||
Get a list of all flights logged by any user
|
||||
|
||||
:return: List of flights
|
||||
"""
|
||||
flights = [FlightModel(**f.to_mongo()) for f in get_flight_list()]
|
||||
flights = await db.retrieve_flights()
|
||||
return flights
|
||||
|
||||
|
||||
@router.get('/flights/{flight_id}', summary="Get details of a given flight", response_model=FlightModel,
|
||||
@router.get('/{flight_id}', summary="Get details of a given flight", response_model=FlightDisplaySchema,
|
||||
status_code=200)
|
||||
def get_flight(flight_id: str, user: GetSystemUserSchema = Depends(get_current_user)):
|
||||
async def get_flight(flight_id: str, user: UserDisplaySchema = Depends(get_current_user)):
|
||||
"""
|
||||
Get all details of a given flight
|
||||
|
||||
@@ -52,7 +47,7 @@ def get_flight(flight_id: str, user: GetSystemUserSchema = Depends(get_current_u
|
||||
:param user: Currently logged-in user
|
||||
:return: Flight details
|
||||
"""
|
||||
flight = Flight.objects(id=flight_id).to_json()
|
||||
flight = await db.retrieve_flight(flight_id)
|
||||
if flight.user != user.id and AuthLevel(user.level) != AuthLevel.ADMIN:
|
||||
logger.info("Attempted access to unauthorized flight by %s", user.username)
|
||||
raise HTTPException(403, "Unauthorized access")
|
||||
@@ -60,26 +55,24 @@ def get_flight(flight_id: str, user: GetSystemUserSchema = Depends(get_current_u
|
||||
return flight
|
||||
|
||||
|
||||
@router.post('/flights', summary="Add a flight logbook entry", status_code=200)
|
||||
def add_flight(flight_body: FlightModel, user: GetSystemUserSchema = Depends(get_current_user)):
|
||||
@router.post('/', summary="Add a flight logbook entry", status_code=200)
|
||||
async def add_flight(flight_body: FlightCreateSchema, user: UserDisplaySchema = Depends(get_current_user)):
|
||||
"""
|
||||
Add a flight logbook entry
|
||||
|
||||
:param flight_body: Information associated with new flight
|
||||
:param user: Currently logged-in user
|
||||
:return: Error message if request invalid, else ID of newly created log
|
||||
"""
|
||||
try:
|
||||
flight = Flight(user=user.id, **flight_body.model_dump()).save()
|
||||
except ValidationError as e:
|
||||
logger.info("Invalid flight body: %s", e)
|
||||
raise HTTPException(400, "Invalid request")
|
||||
|
||||
return {"id": flight.id}
|
||||
flight = await db.insert_flight(flight_body, user.id)
|
||||
|
||||
return {"id": str(flight)}
|
||||
|
||||
|
||||
@router.put('/flights/{flight_id}', summary="Update the given flight with new information", status_code=201,
|
||||
response_model=FlightModel)
|
||||
def update_flight(flight_id: str, flight_body: FlightModel, user: GetSystemUserSchema = Depends(get_current_user)):
|
||||
@router.put('/{flight_id}', summary="Update the given flight with new information", status_code=201)
|
||||
async def update_flight(flight_id: str, flight_body: FlightCreateSchema,
|
||||
user: UserDisplaySchema = Depends(get_current_user)) -> str:
|
||||
"""
|
||||
Update the given flight with new information
|
||||
|
||||
@@ -88,19 +81,21 @@ def update_flight(flight_id: str, flight_body: FlightModel, user: GetSystemUserS
|
||||
:param user: Currently logged-in user
|
||||
:return: Updated flight
|
||||
"""
|
||||
flight = Flight.objects(id=flight_id)
|
||||
flight = await get_flight(flight_id)
|
||||
if flight is None:
|
||||
raise HTTPException(404, "Flight not found")
|
||||
|
||||
if flight.user != user and AuthLevel(user.level) != AuthLevel.ADMIN:
|
||||
logger.info("Attempted access to unauthorized flight by %s", user.username)
|
||||
raise HTTPException(403, "Unauthorized access")
|
||||
|
||||
flight.update(**flight_body.model_dump())
|
||||
updated_flight = await db.update_flight(flight_body, flight_id)
|
||||
|
||||
return flight_body
|
||||
return str(updated_flight)
|
||||
|
||||
|
||||
@router.delete('/flights/{flight_id}', summary="Delete the given flight", status_code=200)
|
||||
def delete_flight(flight_id: str, user: GetSystemUserSchema = Depends(get_current_user)):
|
||||
@router.delete('/{flight_id}', summary="Delete the given flight", status_code=200)
|
||||
async def delete_flight(flight_id: str, user: UserDisplaySchema = Depends(get_current_user)):
|
||||
"""
|
||||
Delete the given flight
|
||||
|
||||
@@ -108,12 +103,12 @@ def delete_flight(flight_id: str, user: GetSystemUserSchema = Depends(get_curren
|
||||
:param user: Currently logged-in user
|
||||
:return: 200
|
||||
"""
|
||||
flight = Flight.objects(id=flight_id)
|
||||
flight = await get_flight(flight_id)
|
||||
|
||||
if flight.user != user and AuthLevel(user.level) != AuthLevel.ADMIN:
|
||||
logger.info("Attempted access to unauthorized flight by %s", user.username)
|
||||
raise HTTPException(403, "Unauthorized access")
|
||||
|
||||
flight.delete()
|
||||
deleted = await db.delete_flight(flight_id)
|
||||
|
||||
return '', 200
|
||||
return deleted
|
||||
|
Reference in New Issue
Block a user