From d31f1c0c61aedaa3cf364572edb9c2a221329dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Thu, 3 Oct 2024 22:41:24 +0200 Subject: [PATCH] Bugfixing --- src/graphql_schema/entities/photo.py | 4 +- .../entities/resolvers/photo.py | 39 ++++++++++++------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/graphql_schema/entities/photo.py b/src/graphql_schema/entities/photo.py index 86ddd2f..2ecb406 100644 --- a/src/graphql_schema/entities/photo.py +++ b/src/graphql_schema/entities/photo.py @@ -59,7 +59,7 @@ class PhotoMutation: @error_logging @authenticated_user_only() async def edit_photo(self, info, id: int, input: EditPhotoInput) -> Photo: - return await PhotoMutationResolver().update(id, input, info.context.user_id) + return await PhotoMutationResolver().update(info.context, id, input, info.context.user_id) @strawberry.mutation() @error_logging @@ -87,4 +87,4 @@ class PhotoMutation: if info.context.user_id is None and not copilot_upload_token: raise_unauthorized() - return await PhotoMutationResolver().delete(user_id=info.context.user_id, id=id) + return await PhotoMutationResolver().delete(info.context, id=id) diff --git a/src/graphql_schema/entities/resolvers/photo.py b/src/graphql_schema/entities/resolvers/photo.py index 50ddb6a..5a573f2 100644 --- a/src/graphql_schema/entities/resolvers/photo.py +++ b/src/graphql_schema/entities/resolvers/photo.py @@ -2,8 +2,13 @@ import os import shutil from time import time from typing import Optional + +import pymysql.err +import sqlalchemy from PIL import Image +from graphql import GraphQLError from pydantic import BaseModel +from pymysql.err import IntegrityError from sqlalchemy import delete, insert, select from background_jobs.elevation import add_terrain_elevation_to_photo from background_jobs.photo import generate_thumbnail, resize_photo @@ -61,7 +66,7 @@ class PhotoQueryResolver(BaseQueryResolver): class PhotoDetailInfo(BaseModel): filename: str - original_filename: str + original_filename: str | None path: str flight_id: int @@ -78,7 +83,7 @@ class PhotoMutationResolver(BaseMutationResolver): return original_filename - async def _get_photo_details(self, id: int, user_id: int, copilot_token: Optional[str]): + async def _get_photo_details(self, id: int, user_id: int, copilot_token: str | None, copy_original: bool = True): async with get_session() as db: if not copilot_token: photo = await self._get_one(db, id, created_by_id=user_id) @@ -89,11 +94,16 @@ class PhotoMutationResolver(BaseMutationResolver): filename = photo.filename + "." + photo.filename_extension path = get_photo_basepath(flight_id) + + original_filename = None + if copy_original: + original_filename = self._copy_original(path, filename) + return PhotoDetailInfo( flight_id=flight_id, path=path, filename=filename, - original_filename=self._copy_original(path, filename) # TODO: tohle nedelat v getteru! + original_filename=original_filename ) @staticmethod @@ -165,23 +175,23 @@ class PhotoMutationResolver(BaseMutationResolver): return photo - async def update(self, id: int, input: EditPhotoInput, user_id: int) -> Photo: - data = input.to_dict() + async def update(self, context, id: int, data: EditPhotoInput, user_id: int) -> Photo: + data = data.to_dict() async with get_session() as db: photo = await self._get_one(db, id, created_by_id=user_id) - if input.point_of_interest: + if data.point_of_interest: data['point_of_interest_id'] = await handle_combobox_save( db, models.PointOfInterest, - input.point_of_interest, + data.point_of_interest, user_id, extra_data={"description": ""} ) - if input.copilots is not None: + if data.copilots is not None: await db.execute(delete(models.copilot_has_photo).filter_by(photo_id=id)) - for copilot in input.copilots: + for copilot in data.copilots: await db.execute(insert(models.copilot_has_photo).values(photo_id=id, copilot_id=copilot.id)) return await self._do_update(db, obj=photo, data=data) @@ -261,12 +271,15 @@ class PhotoMutationResolver(BaseMutationResolver): "cache_key": int(time()) }) - async def delete(self, user_id: int, id: int, **kwargs) -> Photo: + async def delete(self, context, id: int, **kwargs) -> Photo: copilot_upload_token = kwargs.get("copilot_upload_token") - await self._get_photo_details(id, user_id, copilot_upload_token) - # ^^ pokud neexistuje, vyhodi vyjimku + await self._get_photo_details(id, context.user_id, copilot_upload_token, copy_original=False) + + try: + photo = await super().delete(context, id) + except (IntegrityError, sqlalchemy.exc.IntegrityError): + raise GraphQLError("Nelze smazat titulni fotku") - photo = await super().delete(user_id, id) base_path = get_photo_basepath(photo.flight_id) files_to_delete = [