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 typing import Optional, Type
from sqlalchemy import select from sqlalchemy import select, or_
from database import models from database import models
@@ -23,7 +23,12 @@ class QueryBuilder:
query = query.filter(self.model.deleted.is_(False)) query = query.filter(self.model.deleted.is_(False))
if hasattr(self.model, "created_by_id") and created_by_id: 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: if order_by:
query = query.order_by(*order_by) query = query.order_by(*order_by)
+2 -12
View File
@@ -2,7 +2,6 @@ from typing import List
import strawberry import strawberry
from database import models from database import models
from decorators.endpoints import authenticated_user_only 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.resolvers.base import BaseQueryResolver, BaseMutationResolver
from graphql_schema.entities.types.mutation_input import CreateCopilotInput, EditCopilotInput from graphql_schema.entities.types.mutation_input import CreateCopilotInput, EditCopilotInput
from graphql_schema.entities.types.types import Copilot from graphql_schema.entities.types.types import Copilot
@@ -22,7 +21,7 @@ class CopilotQueries:
@strawberry.type @strawberry.type
class CreateCopilotMutation: class CopilotMutation:
@strawberry.mutation @strawberry.mutation
@authenticated_user_only() @authenticated_user_only()
async def create_copilot(root, info, input: CreateCopilotInput) -> Copilot: async def create_copilot(root, info, input: CreateCopilotInput) -> Copilot:
@@ -31,16 +30,7 @@ class CreateCopilotMutation:
user_id=info.context.user_id user_id=info.context.user_id
) )
@strawberry.type
class EditCopilotMutation:
@strawberry.mutation @strawberry.mutation
@authenticated_user_only() @authenticated_user_only()
async def edit_copilot(root, info, id: int, input: EditCopilotInput) -> Copilot: async def edit_copilot(root, info, id: int, input: EditCopilotInput) -> Copilot:
async with get_session() as db: return await BaseMutationResolver(Copilot, models.Copilot).update(id, input, info.context.user_id)
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())
+1 -7
View File
@@ -47,10 +47,4 @@ class EventMutation:
@strawberry.mutation @strawberry.mutation
@authenticated_user_only() @authenticated_user_only()
async def edit_event(root, info, id: int, input: EditEventInput) -> Event: async def edit_event(root, info, id: int, input: EditEventInput) -> Event:
async with get_session() as db: return await BaseMutationResolver(Event, models.Event).update(id, input, info.context.user_id)
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())
+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) BaseQueryResolver(Photo, models.Photo).get_query(user_id=info.context.user_id, object_id=id)
)).one() )).one()
photo_filename = photo.filename photo_filename = photo.filename
flight_id = photo.flight_id
photo_as_dict = photo.as_dict() photo_as_dict = photo.as_dict()
await asyncio.gather( await asyncio.gather(
rotate_image( rotate_image(
path=get_photo_basepath(photo.flight_id), path=get_photo_basepath(flight_id),
filename=photo_filename, filename=photo_filename,
angle=angle, angle=angle,
), ),
rotate_image( rotate_image(
path=get_photo_basepath(photo.flight_id) + "/thumbs", path=get_photo_basepath(flight_id) + "/thumbs",
filename=photo_filename, filename=photo_filename,
angle=angle, angle=angle,
), ),
@@ -3,6 +3,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from database import models from database import models
from database.query_builder import QueryBuilder from database.query_builder import QueryBuilder
from database.transaction import get_session from database.transaction import get_session
from graphql_schema.entities.types.base import BaseGraphqlInputType
GQL_TYPE = TypeVar('GQL_TYPE') GQL_TYPE = TypeVar('GQL_TYPE')
@@ -76,6 +77,11 @@ class BaseMutationResolver(BaseResolver):
model = await self.model.update(db, data=data, **update_where) model = await self.model.update(db, data=data, **update_where)
return self.graphql_type(**model.as_dict()) 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 def delete(self, user_id: int, id: int) -> GQL_TYPE:
async with get_session() as db: async with get_session() as db:
model = await self._get_one(db, id, user_id) 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): async def update(self, context, id: int, input: EditFlightInput):
user_id = context.user_id user_id = context.user_id
async with get_session() as db: 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_data = flight.as_dict()
flight_id = flight.id flight_id = flight.id
@@ -12,11 +12,11 @@ class PhotoMutationResolver(BaseMutationResolver):
def __init__(self): def __init__(self):
super().__init__(Photo, models.Photo) 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( (await db.execute(
update(models.Photo) update(models.Photo)
.filter(models.Photo.flight_id == flight_id) .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: async def update(self, id: int, input: EditPhotoInput, user_id: int) -> Photo:
@@ -35,6 +35,6 @@ class PhotoMutationResolver(BaseMutationResolver):
if input.is_flight_cover: if input.is_flight_cover:
# reset other covers # 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) 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 strawberry.tools import merge_types
from graphql_schema.entities.aircraft import AircraftMutation 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.event import EventMutation
from graphql_schema.entities.flight import FlightMutation from graphql_schema.entities.flight import FlightMutation
from graphql_schema.entities.organization import OrganizationUserMutation, OrganizationMutation from graphql_schema.entities.organization import OrganizationUserMutation, OrganizationMutation
@@ -13,8 +13,7 @@ Mutation = merge_types("Mutation", (
FlightMutation, FlightMutation,
PhotoMutation, PhotoMutation,
PointOfInterestMutation, PointOfInterestMutation,
CreateCopilotMutation, CopilotMutation,
EditCopilotMutation,
EditUserMutation, EditUserMutation,
EventMutation, EventMutation,
OrganizationMutation, OrganizationMutation,