Fix otaceni fotek, drobny refaktoring

This commit is contained in:
Michal Kváček
2023-10-15 14:24:57 +02:00
parent bc258efa3a
commit 322e61bce3
8 changed files with 25 additions and 30 deletions
+7 -2
View File
@@ -1,5 +1,5 @@
from typing import Optional, Type
from sqlalchemy import select
from sqlalchemy import select, or_
from database import models
@@ -23,7 +23,12 @@ class QueryBuilder:
query = query.filter(self.model.deleted.is_(False))
if hasattr(self.model, "created_by_id") and created_by_id:
query = query.filter(self.model.created_by_id == created_by_id)
ownership = [self.model.created_by_id == created_by_id]
if hasattr(self.model, "is_public"):
ownership.append(self.model.is_public.is_(True))
query = query.filter(or_(*ownership))
if order_by:
query = query.order_by(*order_by)
+2 -12
View File
@@ -2,7 +2,6 @@ from typing import List
import strawberry
from database import models
from decorators.endpoints import authenticated_user_only
from database.transaction import get_session
from graphql_schema.entities.resolvers.base import BaseQueryResolver, BaseMutationResolver
from graphql_schema.entities.types.mutation_input import CreateCopilotInput, EditCopilotInput
from graphql_schema.entities.types.types import Copilot
@@ -22,7 +21,7 @@ class CopilotQueries:
@strawberry.type
class CreateCopilotMutation:
class CopilotMutation:
@strawberry.mutation
@authenticated_user_only()
async def create_copilot(root, info, input: CreateCopilotInput) -> Copilot:
@@ -31,16 +30,7 @@ class CreateCopilotMutation:
user_id=info.context.user_id
)
@strawberry.type
class EditCopilotMutation:
@strawberry.mutation
@authenticated_user_only()
async def edit_copilot(root, info, id: int, input: EditCopilotInput) -> Copilot:
async with get_session() as db:
copilot = (await db.scalars(
BaseQueryResolver(Copilot, models.Copilot).get_query(object_id=id, user_id=info.context.user_id)
)).one()
updated_copilot = await models.Copilot.update(db, obj=copilot, data=input.to_dict())
return Copilot(**updated_copilot.as_dict())
return await BaseMutationResolver(Copilot, models.Copilot).update(id, input, info.context.user_id)
+1 -7
View File
@@ -47,10 +47,4 @@ class EventMutation:
@strawberry.mutation
@authenticated_user_only()
async def edit_event(root, info, id: int, input: EditEventInput) -> Event:
async with get_session() as db:
event = (await db.scalars(
EventQueryResolver().get_query(user_id=info.context.user_id, object_id=id)
)).one()
updated_event = await models.Event.update(db, obj=event, data=input.to_dict())
return Event(**updated_event.as_dict())
return await BaseMutationResolver(Event, models.Event).update(id, input, info.context.user_id)
+3 -2
View File
@@ -68,16 +68,17 @@ class PhotoMutation:
BaseQueryResolver(Photo, models.Photo).get_query(user_id=info.context.user_id, object_id=id)
)).one()
photo_filename = photo.filename
flight_id = photo.flight_id
photo_as_dict = photo.as_dict()
await asyncio.gather(
rotate_image(
path=get_photo_basepath(photo.flight_id),
path=get_photo_basepath(flight_id),
filename=photo_filename,
angle=angle,
),
rotate_image(
path=get_photo_basepath(photo.flight_id) + "/thumbs",
path=get_photo_basepath(flight_id) + "/thumbs",
filename=photo_filename,
angle=angle,
),
@@ -3,6 +3,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from database import models
from database.query_builder import QueryBuilder
from database.transaction import get_session
from graphql_schema.entities.types.base import BaseGraphqlInputType
GQL_TYPE = TypeVar('GQL_TYPE')
@@ -76,6 +77,11 @@ class BaseMutationResolver(BaseResolver):
model = await self.model.update(db, data=data, **update_where)
return self.graphql_type(**model.as_dict())
async def update(self, id: int, data: BaseGraphqlInputType, user_id: int) -> GQL_TYPE:
async with get_session() as db:
item = await self._get_one(db, id, user_id)
return await self._do_update(db, item, data.to_dict())
async def delete(self, user_id: int, id: int) -> GQL_TYPE:
async with get_session() as db:
model = await self._get_one(db, id, user_id)
@@ -45,7 +45,7 @@ class FlightMutationResolver(BaseMutationResolver):
async def update(self, context, id: int, input: EditFlightInput):
user_id = context.user_id
async with get_session() as db:
flight = await FlightQueryResolver().get_one(id, user_id)
flight = await self._get_one(db, id, user_id)
flight_data = flight.as_dict()
flight_id = flight.id
@@ -12,11 +12,11 @@ class PhotoMutationResolver(BaseMutationResolver):
def __init__(self):
super().__init__(Photo, models.Photo)
async def reset_flight_cover(self, db: AsyncSession, flight_id: int):
async def reset_flight_cover(self, db: AsyncSession, flight_id: int, ignored_photo_id: int):
(await db.execute(
update(models.Photo)
.filter(models.Photo.flight_id == flight_id)
.filter(models.Photo.id != id).values(is_flight_cover=False))
.filter(models.Photo.id != ignored_photo_id).values(is_flight_cover=False))
)
async def update(self, id: int, input: EditPhotoInput, user_id: int) -> Photo:
@@ -35,6 +35,6 @@ class PhotoMutationResolver(BaseMutationResolver):
if input.is_flight_cover:
# reset other covers
await self.reset_flight_cover(db, photo.flight_id)
await self.reset_flight_cover(db, photo.flight_id, id)
return await self._do_update(db, obj=photo, data=data)
+2 -3
View File
@@ -1,6 +1,6 @@
from strawberry.tools import merge_types
from graphql_schema.entities.aircraft import AircraftMutation
from graphql_schema.entities.copilot import CreateCopilotMutation, EditCopilotMutation
from graphql_schema.entities.copilot import CopilotMutation
from graphql_schema.entities.event import EventMutation
from graphql_schema.entities.flight import FlightMutation
from graphql_schema.entities.organization import OrganizationUserMutation, OrganizationMutation
@@ -13,8 +13,7 @@ Mutation = merge_types("Mutation", (
FlightMutation,
PhotoMutation,
PointOfInterestMutation,
CreateCopilotMutation,
EditCopilotMutation,
CopilotMutation,
EditUserMutation,
EventMutation,
OrganizationMutation,