From 322e61bce327fae656c6cdd365e325a14c9e4008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Sun, 15 Oct 2023 14:24:57 +0200 Subject: [PATCH] Fix otaceni fotek, drobny refaktoring --- src/database/query_builder.py | 9 +++++++-- src/graphql_schema/entities/copilot.py | 14 ++------------ src/graphql_schema/entities/event.py | 8 +------- src/graphql_schema/entities/photo.py | 5 +++-- src/graphql_schema/entities/resolvers/base.py | 6 ++++++ src/graphql_schema/entities/resolvers/flight.py | 2 +- src/graphql_schema/entities/resolvers/photo.py | 6 +++--- src/graphql_schema/mutation.py | 5 ++--- 8 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/database/query_builder.py b/src/database/query_builder.py index cf39aec..9a7b894 100644 --- a/src/database/query_builder.py +++ b/src/database/query_builder.py @@ -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) diff --git a/src/graphql_schema/entities/copilot.py b/src/graphql_schema/entities/copilot.py index d12e523..b4a41f1 100644 --- a/src/graphql_schema/entities/copilot.py +++ b/src/graphql_schema/entities/copilot.py @@ -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) diff --git a/src/graphql_schema/entities/event.py b/src/graphql_schema/entities/event.py index 9caf4cb..8929275 100644 --- a/src/graphql_schema/entities/event.py +++ b/src/graphql_schema/entities/event.py @@ -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) diff --git a/src/graphql_schema/entities/photo.py b/src/graphql_schema/entities/photo.py index 15070cb..b3ca3d5 100644 --- a/src/graphql_schema/entities/photo.py +++ b/src/graphql_schema/entities/photo.py @@ -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, ), diff --git a/src/graphql_schema/entities/resolvers/base.py b/src/graphql_schema/entities/resolvers/base.py index 6e8fa71..e1053c9 100644 --- a/src/graphql_schema/entities/resolvers/base.py +++ b/src/graphql_schema/entities/resolvers/base.py @@ -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) diff --git a/src/graphql_schema/entities/resolvers/flight.py b/src/graphql_schema/entities/resolvers/flight.py index 99fde48..75b8ccd 100644 --- a/src/graphql_schema/entities/resolvers/flight.py +++ b/src/graphql_schema/entities/resolvers/flight.py @@ -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 diff --git a/src/graphql_schema/entities/resolvers/photo.py b/src/graphql_schema/entities/resolvers/photo.py index 4089ab8..1ee5895 100644 --- a/src/graphql_schema/entities/resolvers/photo.py +++ b/src/graphql_schema/entities/resolvers/photo.py @@ -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) diff --git a/src/graphql_schema/mutation.py b/src/graphql_schema/mutation.py index 9ea061f..cabc84f 100644 --- a/src/graphql_schema/mutation.py +++ b/src/graphql_schema/mutation.py @@ -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,