Ukladani metadat k planum

This commit is contained in:
Michal Kváček
2024-04-30 22:46:40 +02:00
parent c8138860ca
commit a706303155
7 changed files with 160 additions and 9 deletions
@@ -1,9 +1,17 @@
from sqlalchemy import delete
import asyncio
from sqlalchemy import delete, select
from sqlalchemy.dialects.mysql import insert
from database import models
from database.models import flight_plan_has_copilot
from database.transaction import get_session
from graphql_schema.entities.helpers.combobox import handle_combobox_save
from graphql_schema.entities.resolvers.base import BaseMutationResolver
from graphql_schema.entities.resolvers.flight import handle_aircraft_save
from graphql_schema.entities.types.mutation_input import CreateFlightPlanInput, EditFlightPlanInput
from graphql_schema.entities.types.types import FlightPlan
from utils.str_utils import random_str
class FlightPlanMutationResolver(BaseMutationResolver):
@@ -41,13 +49,55 @@ class FlightPlanMutationResolver(BaseMutationResolver):
await self.save_markers(db, flight_plan, data.markers)
return flight_plan
async def save_copilots(self, db, flight_plan_id: int, copilots: list, user_id: int):
await db.execute(delete(flight_plan_has_copilot).filter_by(flight_plan_id=flight_plan_id))
copilots = await asyncio.gather(*[
handle_combobox_save(db, models.Copilot, copilot, user_id) for copilot in copilots
])
for copilot_id in copilots:
await db.execute(insert(flight_plan_has_copilot).values(
flight_plan_id=flight_plan_id,
copilot_id=copilot_id,
token=random_str(64)
))
async def update(self, id: int, data: EditFlightPlanInput, user_id: int) -> FlightPlan:
input_data = data.to_dict()
async with get_session() as db:
flight_plan = await self._do_update(db, id, input_data)
flight_plan_model = await self._get_one(db, id=id, created_by_id=user_id)
await self.reset_plan_markers(db, flight_plan)
await self.save_markers(db, flight_plan, data.markers)
if data.aircraft is not None:
input_data['aircraft_id'] = await handle_aircraft_save(db, user_id, data.aircraft)
if data.markers is not None:
await self.reset_plan_markers(db, flight_plan_model)
await self.save_markers(db, flight_plan_model, data.markers)
if flight_plan_model.is_default_name:
if data.markers:
markers = data.markers
else:
markers = (await db.scalars(select(models.FlightPlanMarker).filter(models.FlightPlanMarker.flight_plan_id == id))).all()
used_markers = evenly_spaced_elements(markers, 5)
input_data['name'] = " - ".join(m.name for m in used_markers)
if data.copilots is not None:
await self.save_copilots(
db,
flight_plan_id=id,
copilots=data.copilots,
user_id=user_id
)
flight_plan = await self._do_update(db, obj=flight_plan_model, data=input_data)
return flight_plan
def evenly_spaced_elements(lst: list, count: int) -> list:
if count > len(lst):
return lst
interval = (len(lst) - 1) / (count - 1) if count > 1 else 0
return [lst[int(round(i * interval))] for i in range(count)]