Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ disable = """
no-member,
protected-access,
import-outside-toplevel,
too-many-statements,
"""

[tool.ruff]
Expand Down
12 changes: 8 additions & 4 deletions src/models/environment.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import datetime
from datetime import datetime, timezone, timedelta
from typing import Optional, ClassVar, Self, Literal
from pydantic import Field
from src.models.interface import ApiBaseModel


def _default_future_datetime() -> datetime:
"""Factory function to create timezone-aware datetime one day in the future."""
return datetime.now(timezone.utc) + timedelta(days=1)


class EnvironmentModel(ApiBaseModel):
NAME: ClassVar = 'environment'
METHODS: ClassVar = ('POST', 'GET', 'PUT', 'DELETE')
Expand All @@ -20,9 +26,7 @@ class EnvironmentModel(ApiBaseModel):
'ensemble',
] = 'standard_atmosphere'
atmospheric_model_file: Optional[str] = None
date: Optional[datetime.datetime] = (
datetime.datetime.today() + datetime.timedelta(days=1)
)
date: Optional[datetime] = Field(default_factory=_default_future_datetime)

@staticmethod
def UPDATED():
Expand Down
1 change: 1 addition & 0 deletions src/models/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ApiBaseModel(BaseModel, ABC):
validate_default=True,
validate_all_in_root=True,
validate_assignment=True,
ser_json_exclude_none=True,
)

def set_id(self, value):
Expand Down
6 changes: 4 additions & 2 deletions src/repositories/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ def __init__(self):

@repository_exception_handler
async def create_flight(self, flight: FlightModel) -> str:
return await self.insert(flight.model_dump())
return await self.insert(flight.model_dump(exclude_none=True))

@repository_exception_handler
async def read_flight_by_id(self, flight_id: str) -> Optional[FlightModel]:
return await self.find_by_id(data_id=flight_id)

@repository_exception_handler
async def update_flight_by_id(self, flight_id: str, flight: FlightModel):
await self.update_by_id(flight.model_dump(), data_id=flight_id)
await self.update_by_id(
flight.model_dump(exclude_none=True), data_id=flight_id
)

@repository_exception_handler
async def delete_flight_by_id(self, flight_id: str):
Expand Down
6 changes: 4 additions & 2 deletions src/repositories/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ def __init__(self):

@repository_exception_handler
async def create_motor(self, motor: MotorModel) -> str:
return await self.insert(motor.model_dump())
return await self.insert(motor.model_dump(exclude_none=True))

@repository_exception_handler
async def read_motor_by_id(self, motor_id: str) -> Optional[MotorModel]:
return await self.find_by_id(data_id=motor_id)

@repository_exception_handler
async def update_motor_by_id(self, motor_id: str, motor: MotorModel):
await self.update_by_id(motor.model_dump(), data_id=motor_id)
await self.update_by_id(
motor.model_dump(exclude_none=True), data_id=motor_id
)

@repository_exception_handler
async def delete_motor_by_id(self, motor_id: str):
Expand Down
6 changes: 4 additions & 2 deletions src/repositories/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ def __init__(self):

@repository_exception_handler
async def create_rocket(self, rocket: RocketModel) -> str:
return await self.insert(rocket.model_dump())
return await self.insert(rocket.model_dump(exclude_none=True))

@repository_exception_handler
async def read_rocket_by_id(self, rocket_id: str) -> Optional[RocketModel]:
return await self.find_by_id(data_id=rocket_id)

@repository_exception_handler
async def update_rocket_by_id(self, rocket_id: str, rocket: RocketModel):
await self.update_by_id(rocket.model_dump(), data_id=rocket_id)
await self.update_by_id(
rocket.model_dump(exclude_none=True), data_id=rocket_id
)

@repository_exception_handler
async def delete_rocket_by_id(self, rocket_id: str):
Expand Down
6 changes: 4 additions & 2 deletions src/services/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import dill

from rocketpy.environment.environment import Environment as RocketPyEnvironment
from rocketpy.utilities import get_instance_attributes
from src.models.environment import EnvironmentModel
from src.views.environment import EnvironmentSimulation
from src.utils import rocketpy_encoder, DiscretizeConfig


class EnvironmentService:
Expand Down Expand Up @@ -50,7 +50,9 @@ def get_environment_simulation(self) -> EnvironmentSimulation:
EnvironmentSimulation
"""

attributes = get_instance_attributes(self.environment)
attributes = rocketpy_encoder(
self.environment, DiscretizeConfig.for_environment()
)
env_simulation = EnvironmentSimulation(**attributes)
return env_simulation

Expand Down
6 changes: 4 additions & 2 deletions src/services/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import dill

from rocketpy.simulation.flight import Flight as RocketPyFlight
from rocketpy.utilities import get_instance_attributes

from src.services.environment import EnvironmentService
from src.services.rocket import RocketService
from src.models.flight import FlightModel
from src.views.flight import FlightSimulation
from src.utils import rocketpy_encoder, DiscretizeConfig


class FlightService:
Expand Down Expand Up @@ -55,7 +55,9 @@ def get_flight_simulation(self) -> FlightSimulation:
Returns:
FlightSimulation
"""
attributes = get_instance_attributes(self.flight)
attributes = rocketpy_encoder(
self.flight, DiscretizeConfig.for_flight()
)
flight_simulation = FlightSimulation(**attributes)
return flight_simulation

Expand Down
4 changes: 2 additions & 2 deletions src/services/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from rocketpy.motors.solid_motor import SolidMotor
from rocketpy.motors.liquid_motor import LiquidMotor
from rocketpy.motors.hybrid_motor import HybridMotor
from rocketpy.utilities import get_instance_attributes
from rocketpy import (
LevelBasedTank,
MassBasedTank,
Expand All @@ -18,6 +17,7 @@
from src.models.sub.tanks import TankKinds
from src.models.motor import MotorKinds, MotorModel
from src.views.motor import MotorSimulation
from src.utils import rocketpy_encoder, DiscretizeConfig


class MotorService:
Expand Down Expand Up @@ -140,7 +140,7 @@ def get_motor_simulation(self) -> MotorSimulation:
Returns:
MotorSimulation
"""
attributes = get_instance_attributes(self.motor)
attributes = rocketpy_encoder(self.motor, DiscretizeConfig.for_motor())
motor_simulation = MotorSimulation(**attributes)
return motor_simulation

Expand Down
6 changes: 4 additions & 2 deletions src/services/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
Fins as RocketPyFins,
Tail as RocketPyTail,
)
from rocketpy.utilities import get_instance_attributes

from src import logger
from src.models.rocket import RocketModel, Parachute
from src.models.sub.aerosurfaces import NoseCone, Tail, Fins
from src.services.motor import MotorService
from src.views.rocket import RocketSimulation
from src.utils import rocketpy_encoder, DiscretizeConfig


class RocketService:
Expand Down Expand Up @@ -107,7 +107,9 @@ def get_rocket_simulation(self) -> RocketSimulation:
Returns:
RocketSimulation
"""
attributes = get_instance_attributes(self.rocket)
attributes = rocketpy_encoder(
self.rocket, DiscretizeConfig.for_rocket()
)
rocket_simulation = RocketSimulation(**attributes)
return rocket_simulation

Expand Down
Loading