Update flights to reference aircraft collection
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
from enum import Enum
|
||||
from typing import Annotated
|
||||
|
||||
from pydantic import BaseModel, field_validator, Field
|
||||
from bson import ObjectId
|
||||
from pydantic import BaseModel, field_validator
|
||||
from pydantic_core.core_schema import ValidationInfo
|
||||
|
||||
from schemas.flight import PyObjectId
|
||||
from schemas.utils import PyObjectId, PositiveFloat
|
||||
|
||||
|
||||
class AircraftCategory(Enum):
|
||||
@@ -47,9 +47,6 @@ class AircraftClass(Enum):
|
||||
wss = "Weight-Shift Control Sea"
|
||||
|
||||
|
||||
PositiveFloat = Annotated[float, Field(default=0., ge=0)]
|
||||
|
||||
|
||||
class AircraftCreateSchema(BaseModel):
|
||||
tail_no: str
|
||||
make: str
|
||||
@@ -99,3 +96,40 @@ class AircraftCreateSchema(BaseModel):
|
||||
class AircraftDisplaySchema(AircraftCreateSchema):
|
||||
user: PyObjectId
|
||||
id: PyObjectId
|
||||
|
||||
|
||||
# HELPERS #
|
||||
|
||||
|
||||
def aircraft_add_helper(aircraft: dict, user: str) -> dict:
|
||||
"""
|
||||
Convert given aircraft dict to a format that can be inserted into the db
|
||||
|
||||
:param aircraft: Aircraft request body
|
||||
:param user: User that created aircraft
|
||||
:return: Combined dict that can be inserted into db
|
||||
"""
|
||||
aircraft["user"] = ObjectId(user)
|
||||
aircraft["aircraft_category"] = aircraft["aircraft_category"].name
|
||||
aircraft["aircraft_class"] = aircraft["aircraft_class"].name
|
||||
|
||||
return aircraft
|
||||
|
||||
|
||||
def aircraft_display_helper(aircraft: dict) -> dict:
|
||||
"""
|
||||
Convert given db response into a format usable by AircraftDisplaySchema
|
||||
|
||||
:param aircraft:
|
||||
:return: USable dict
|
||||
"""
|
||||
aircraft["id"] = str(aircraft["_id"])
|
||||
aircraft["user"] = str(aircraft["user"])
|
||||
|
||||
if aircraft["aircraft_category"] is not AircraftCategory:
|
||||
aircraft["aircraft_category"] = AircraftCategory.__members__.get(aircraft["aircraft_category"])
|
||||
|
||||
if aircraft["aircraft_class"] is not AircraftClass:
|
||||
aircraft["aircraft_class"] = AircraftClass.__members__.get(aircraft["aircraft_class"])
|
||||
|
||||
return aircraft
|
||||
|
Reference in New Issue
Block a user