Remove tach field from aircraft

This commit is contained in:
april 2024-01-10 13:55:43 -06:00
parent dafcacf28a
commit c1050d8234
4 changed files with 48 additions and 18 deletions

View File

@ -23,11 +23,26 @@ async def retrieve_aircraft(user: str = "") -> list[AircraftDisplaySchema]:
return aircraft return aircraft
async def retrieve_aircraft_by_tail(tail_no: str) -> AircraftDisplaySchema:
"""
Retrieve details about the requested aircraft
:param tail_no: Tail number of desired aircraft
:return: Aircraft details
"""
aircraft = await aircraft_collection.find_one({"tail_no": tail_no})
if aircraft is None:
raise HTTPException(404, "Aircraft not found")
return AircraftDisplaySchema(**aircraft_display_helper(aircraft))
async def retrieve_aircraft_by_id(id: str) -> AircraftDisplaySchema: async def retrieve_aircraft_by_id(id: str) -> AircraftDisplaySchema:
""" """
Retrieve details about the requested aircraft Retrieve details about the requested aircraft
:param id: ID of desired aircraft :param tail_no: Tail number of desired aircraft
:return: Aircraft details :return: Aircraft details
""" """
aircraft = await aircraft_collection.find_one({"_id": ObjectId(id)}) aircraft = await aircraft_collection.find_one({"_id": ObjectId(id)})

View File

@ -59,7 +59,7 @@ async def get_categories(category: str = "Airplane") -> dict:
return {"classes": category_class[category]} return {"classes": category_class[category]}
@router.get('/{aircraft_id}', summary="Get details of a given aircraft", response_model=AircraftDisplaySchema, @router.get('/id/{aircraft_id}', summary="Get details of a given aircraft", response_model=AircraftDisplaySchema,
status_code=200) status_code=200)
async def get_aircraft_by_id(aircraft_id: str, async def get_aircraft_by_id(aircraft_id: str,
user: UserDisplaySchema = Depends(get_current_user)) -> AircraftDisplaySchema: user: UserDisplaySchema = Depends(get_current_user)) -> AircraftDisplaySchema:
@ -71,6 +71,27 @@ async def get_aircraft_by_id(aircraft_id: str,
:return: Aircraft details :return: Aircraft details
""" """
aircraft = await db.retrieve_aircraft_by_id(aircraft_id) aircraft = await db.retrieve_aircraft_by_id(aircraft_id)
if str(aircraft.user) != user.id and AuthLevel(user.level) != AuthLevel.ADMIN:
logger.info("Attempted access to unauthorized aircraft by %s", user.username)
raise HTTPException(403, "Unauthorized access")
return aircraft
@router.get('/tail/{tail_no}', summary="Get details of a given aircraft", response_model=AircraftDisplaySchema,
status_code=200)
async def get_aircraft_by_tail(tail_no: str,
user: UserDisplaySchema = Depends(get_current_user)) -> AircraftDisplaySchema:
"""
Get all details of a given aircraft
:param tail_no: Tail number of requested aircraft
:param user: Currently logged-in user
:return: Aircraft details
"""
aircraft = await db.retrieve_aircraft_by_tail(tail_no)
if str(aircraft.user) != user.id and AuthLevel(user.level) != AuthLevel.ADMIN: if str(aircraft.user) != user.id and AuthLevel(user.level) != AuthLevel.ADMIN:
logger.info("Attempted access to unauthorized aircraft by %s", user.username) logger.info("Attempted access to unauthorized aircraft by %s", user.username)
raise HTTPException(403, "Unauthorized access") raise HTTPException(403, "Unauthorized access")
@ -89,10 +110,15 @@ async def add_aircraft(aircraft_body: AircraftCreateSchema,
:return: Error message if request invalid, else ID of newly created aircraft :return: Error message if request invalid, else ID of newly created aircraft
""" """
try:
await db.retrieve_aircraft_by_tail(aircraft_body.tail_no)
except HTTPException:
aircraft = await db.insert_aircraft(aircraft_body, user.id) aircraft = await db.insert_aircraft(aircraft_body, user.id)
return {"id": str(aircraft)} return {"id": str(aircraft)}
raise HTTPException(400, "Aircraft with tail number " + aircraft_body.tail_no + " already exists", )
@router.put('/{aircraft_id}', summary="Update the given aircraft with new information", status_code=200) @router.put('/{aircraft_id}', summary="Update the given aircraft with new information", status_code=200)
async def update_aircraft(aircraft_id: str, aircraft_body: AircraftCreateSchema, async def update_aircraft(aircraft_id: str, aircraft_body: AircraftCreateSchema,

View File

@ -86,7 +86,6 @@ class AircraftCreateSchema(BaseModel):
aircraft_class: AircraftClass aircraft_class: AircraftClass
hobbs: PositiveFloat hobbs: PositiveFloat
tach: PositiveFloat
@field_validator('aircraft_class') @field_validator('aircraft_class')
def validate_class(cls, v: str, info: ValidationInfo, **kwargs): def validate_class(cls, v: str, info: ValidationInfo, **kwargs):

View File

@ -4,20 +4,18 @@ from typing import Optional, Dict, Union, List
from bson import ObjectId from bson import ObjectId
from pydantic import BaseModel from pydantic import BaseModel
from database.aircraft import retrieve_aircraft_by_id
from schemas.utils import PositiveFloatNullable, PositiveFloat, PositiveInt, PyObjectId from schemas.utils import PositiveFloatNullable, PositiveFloat, PositiveInt, PyObjectId
class FlightSchema(BaseModel): class FlightCreateSchema(BaseModel):
date: datetime.datetime date: datetime.datetime
aircraft: str
waypoint_from: Optional[str] = None waypoint_from: Optional[str] = None
waypoint_to: Optional[str] = None waypoint_to: Optional[str] = None
route: Optional[str] = None route: Optional[str] = None
hobbs_start: Optional[PositiveFloatNullable] = None hobbs_start: Optional[PositiveFloatNullable] = None
hobbs_end: Optional[PositiveFloatNullable] = None hobbs_end: Optional[PositiveFloatNullable] = None
tach_start: Optional[PositiveFloatNullable] = None
tach_end: Optional[PositiveFloatNullable] = None
time_start: Optional[datetime.datetime] = None time_start: Optional[datetime.datetime] = None
time_off: Optional[datetime.datetime] = None time_off: Optional[datetime.datetime] = None
@ -53,14 +51,9 @@ class FlightSchema(BaseModel):
comments: Optional[str] = None comments: Optional[str] = None
class FlightCreateSchema(FlightSchema): class FlightDisplaySchema(FlightCreateSchema):
aircraft: str
class FlightDisplaySchema(FlightSchema):
user: PyObjectId user: PyObjectId
id: PyObjectId id: PyObjectId
aircraft: PyObjectId
class FlightConciseSchema(BaseModel): class FlightConciseSchema(BaseModel):
@ -93,7 +86,6 @@ def flight_display_helper(flight: dict) -> dict:
""" """
flight["id"] = str(flight["_id"]) flight["id"] = str(flight["_id"])
flight["user"] = str(flight["user"]) flight["user"] = str(flight["user"])
flight["aircraft"] = str(flight["aircraft"])
return flight return flight
@ -107,7 +99,6 @@ async def flight_concise_helper(flight: dict) -> dict:
""" """
flight["id"] = str(flight["_id"]) flight["id"] = str(flight["_id"])
flight["user"] = str(flight["user"]) flight["user"] = str(flight["user"])
flight["aircraft"] = (await retrieve_aircraft_by_id(str(flight["aircraft"]))).tail_no
return flight return flight
@ -121,6 +112,5 @@ def flight_add_helper(flight: dict, user: str) -> dict:
:return: Combined dict that can be inserted into db :return: Combined dict that can be inserted into db
""" """
flight["user"] = ObjectId(user) flight["user"] = ObjectId(user)
flight["aircraft"] = ObjectId(flight["aircraft"])
return flight return flight