Refaktoring, pridani letiste pro mezipristani

This commit is contained in:
Michal Kváček
2023-10-07 21:36:56 +02:00
parent 2422f26f10
commit b355d0b439
25 changed files with 581 additions and 566 deletions
+8 -37
View File
@@ -1,52 +1,23 @@
import asyncio
from typing import List, Optional, Annotated, TYPE_CHECKING
from typing import List, Optional
import strawberry
from sqlalchemy import select, update
from sqlalchemy import update
from strawberry.file_uploads import Upload
from background_jobs.elevation import add_terrain_elevation_to_photo
from database import models
from decorators.endpoints import authenticated_user_only
from dependencies.db import get_session
from graphql_schema.sqlalchemy_to_strawberry_type import strawberry_sqlalchemy_type
from graphql_schema.types import ComboboxInput
from graphql_schema.entities.types.types import Photo
from paths import get_photo_basepath
from upload_utils import (
get_public_url, handle_file_upload, delete_file, parse_exif_info, generate_thumbnail, file_exists, resize_image,
handle_file_upload, delete_file, parse_exif_info, generate_thumbnail, resize_image,
rotate_image
)
from graphql_schema.entities.helpers.combobox import handle_combobox_save
from .resolvers.base import get_base_resolver, get_list
from ..dataloaders.single_model import poi_dataloader
if TYPE_CHECKING:
from .poi import PointOfInterest
from graphql_schema.entities.types.mutation_input import ComboboxInput
@strawberry_sqlalchemy_type(models.Photo)
class Photo:
def resolve_thumb_url(root):
thumbnail = get_photo_basepath(root.flight_id) + "/thumbs/" + root.filename
if not file_exists(thumbnail):
return get_public_url(f"photos/{root.flight_id}/{root.filename}")
return get_public_url(f"photos/{root.flight_id}/thumbs/{root.filename}")
url: str = strawberry.field(resolver=lambda root: get_public_url(f"photos/{root.flight_id}/{root.filename}"))
thumbnail_url: str = strawberry.field(resolver=resolve_thumb_url)
point_of_interest: Optional[Annotated["PointOfInterest", strawberry.lazy('.poi')]] = strawberry.field(
resolver=lambda root: poi_dataloader.load(root.point_of_interest_id)
)
def get_base_query(user_id: int):
return (
select(models.Photo)
.filter(models.Photo.created_by_id == user_id)
.order_by(models.Photo.id.desc())
)
def get_photo_basepath(flight_id: int) -> str:
return f"/app/uploads/photos/{flight_id}"
@strawberry.type
@@ -173,9 +144,9 @@ class DeletePhotoMutation:
@strawberry.mutation()
@authenticated_user_only()
async def delete_photo(self, info, id: int) -> Photo:
query = get_base_query(info.context.user_id)
query = get_base_resolver(models.Photo, user_id=info.context.user_id, object_id=id)
async with get_session() as db:
photo_model = (await db.scalars(query.filter(models.Photo.id == id))).one()
photo_model = (await db.scalars(query)).one()
await db.delete(photo_model)
photo = Photo(**photo_model.as_dict())