33from pymongo .errors import PyMongoError
44
55
6- import jsonpickle
7-
86from lib import logger , parse_error
7+ from lib .controllers .rocket import RocketController
98from lib .models .environment import Env
109from lib .models .rocket import Rocket
1110from lib .models .flight import Flight
11+ from lib .views .motor import MotorView
12+ from lib .views .rocket import RocketView
1213from lib .views .flight import (
1314 FlightSummary ,
1415 FlightCreated ,
1516 FlightUpdated ,
1617 FlightDeleted ,
17- FlightPickle ,
18+ FlightView ,
1819)
1920from lib .repositories .flight import FlightRepository
2021from lib .services .flight import FlightService
@@ -42,6 +43,7 @@ def __init__(
4243 self ,
4344 flight : Flight ,
4445 ):
46+ self .guard (flight )
4547 self ._flight = flight
4648
4749 @property
@@ -52,6 +54,10 @@ def flight(self) -> Flight:
5254 def flight (self , flight : Flight ):
5355 self ._flight = flight
5456
57+ @staticmethod
58+ def guard (flight : Flight ):
59+ RocketController .guard (flight .rocket )
60+
5561 async def create_flight (self ) -> Union [FlightCreated , HTTPException ]:
5662 """
5763 Create a flight in the database.
@@ -85,7 +91,9 @@ async def create_flight(self) -> Union[FlightCreated, HTTPException]:
8591 )
8692
8793 @staticmethod
88- async def get_flight_by_id (flight_id : str ) -> Union [Flight , HTTPException ]:
94+ async def get_flight_by_id (
95+ flight_id : str ,
96+ ) -> Union [FlightView , HTTPException ]:
8997 """
9098 Get a flight from the database.
9199
@@ -101,7 +109,7 @@ async def get_flight_by_id(flight_id: str) -> Union[Flight, HTTPException]:
101109 try :
102110 async with FlightRepository () as flight_repo :
103111 await flight_repo .get_flight_by_id (flight_id )
104- read_flight = flight_repo .flight
112+ flight = flight_repo .flight
105113 except PyMongoError as e :
106114 logger .error (
107115 f"controllers.flight.get_flight_by_id: PyMongoError { e } "
@@ -120,8 +128,20 @@ async def get_flight_by_id(flight_id: str) -> Union[Flight, HTTPException]:
120128 detail = f"Failed to read flight: { exc_str } " ,
121129 ) from e
122130 else :
123- if read_flight :
124- return read_flight
131+ if flight :
132+ motor_view = MotorView (
133+ ** flight .rocket .motor .dict (),
134+ selected_motor_kind = flight .rocket .motor .motor_kind ,
135+ )
136+ updated_rocket = flight .rocket .dict ()
137+ updated_rocket .update (motor = motor_view )
138+ rocket_view = RocketView (
139+ ** updated_rocket ,
140+ )
141+ updated_flight = flight .dict ()
142+ updated_flight .update (rocket = rocket_view )
143+ flight_view = FlightView (** updated_flight )
144+ return flight_view
125145 raise HTTPException (
126146 status_code = status .HTTP_404_NOT_FOUND ,
127147 detail = "Flight not found." ,
@@ -132,43 +152,41 @@ async def get_flight_by_id(flight_id: str) -> Union[Flight, HTTPException]:
132152 )
133153
134154 @classmethod
135- async def get_rocketpy_flight_as_jsonpickle (
155+ async def get_rocketpy_flight_binary (
136156 cls ,
137157 flight_id : str ,
138- ) -> Union [FlightPickle , HTTPException ]:
158+ ) -> Union [bytes , HTTPException ]:
139159 """
140- Get rocketpy.flight as jsonpickle string .
160+ Get rocketpy.flight as dill binary .
141161
142162 Args:
143163 flight_id: str
144164
145165 Returns:
146- views.FlightPickle
166+ bytes
147167
148168 Raises:
149169 HTTP 404 Not Found: If the flight is not found in the database.
150170 """
151171 try :
152- read_flight = await cls .get_flight_by_id (flight_id )
153- rocketpy_flight = FlightService .from_flight_model (read_flight )
172+ flight = await cls .get_flight_by_id (flight_id )
173+ flight_service = FlightService .from_flight_model (flight )
154174 except HTTPException as e :
155175 raise e from e
156176 except Exception as e :
157177 exc_str = parse_error (e )
158178 logger .error (
159- f"controllers.flight.get_rocketpy_flight_as_jsonpickle : { exc_str } "
179+ f"controllers.flight.get_rocketpy_flight_binary : { exc_str } "
160180 )
161181 raise HTTPException (
162182 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
163183 detail = f"Failed to read flight: { exc_str } " ,
164184 ) from e
165185 else :
166- return FlightPickle (
167- jsonpickle_rocketpy_flight = jsonpickle .encode (rocketpy_flight )
168- )
186+ return flight_service .get_flight_binary ()
169187 finally :
170188 logger .info (
171- f"Call to controllers.flight.get_rocketpy_flight_as_jsonpickle completed for Flight { flight_id } "
189+ f"Call to controllers.flight.get_rocketpy_flight_binary completed for Flight { flight_id } "
172190 )
173191
174192 async def update_flight_by_id (
@@ -229,11 +247,9 @@ async def update_env_by_flight_id(
229247 HTTP 404 Not Found: If the flight is not found in the database.
230248 """
231249 try :
232- read_flight = await cls .get_flight_by_id (flight_id )
233- new_flight = read_flight .dict ()
234- new_flight ["environment" ] = env
235- new_flight = Flight (** new_flight )
236- async with FlightRepository (new_flight ) as flight_repo :
250+ flight = await cls .get_flight_by_id (flight_id )
251+ flight .environment = env
252+ async with FlightRepository (flight ) as flight_repo :
237253 await flight_repo .update_env_by_flight_id (flight_id )
238254 except PyMongoError as e :
239255 logger .error (
@@ -277,16 +293,9 @@ async def update_rocket_by_flight_id(
277293 HTTP 404 Not Found: If the flight is not found in the database.
278294 """
279295 try :
280- read_flight = await cls .get_flight_by_id (flight_id )
281- updated_rocket = rocket .dict ()
282- updated_rocket ["rocket_option" ] = rocket .rocket_option .value
283- updated_rocket ["motor" ][
284- "motor_kind"
285- ] = rocket .motor .motor_kind .value
286- new_flight = read_flight .dict ()
287- new_flight ["rocket" ] = updated_rocket
288- new_flight = Flight (** new_flight )
289- async with FlightRepository (new_flight ) as flight_repo :
296+ flight = await cls .get_flight_by_id (flight_id )
297+ flight .rocket = rocket
298+ async with FlightRepository (flight ) as flight_repo :
290299 await flight_repo .update_rocket_by_flight_id (flight_id )
291300 except PyMongoError as e :
292301 logger .error (
@@ -371,9 +380,9 @@ async def simulate_flight(
371380 HTTP 404 Not Found: If the flight does not exist in the database.
372381 """
373382 try :
374- read_flight = await cls .get_flight_by_id (flight_id = flight_id )
375- rocketpy_flight = FlightService .from_flight_model (read_flight )
376- flight_summary = rocketpy_flight .get_flight_summary ()
383+ flight = await cls .get_flight_by_id (flight_id = flight_id )
384+ flight_service = FlightService .from_flight_model (flight )
385+ flight_summary = flight_service .get_flight_summary ()
377386 except HTTPException as e :
378387 raise e from e
379388 except Exception as e :
0 commit comments