From f4ecacf0c80f0d4f79473517280bc15c9beb5b5f Mon Sep 17 00:00:00 2001 From: itskovacs Date: Sun, 9 Nov 2025 19:18:13 +0100 Subject: [PATCH] Handle image URL for place creation --- backend/trip/routers/places.py | 31 ++++++++++++++++++++----------- backend/trip/utils/gmaps.py | 12 +++++++++++- backend/trip/utils/utils.py | 2 +- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/backend/trip/routers/places.py b/backend/trip/routers/places.py index 7ac804a..614250b 100644 --- a/backend/trip/routers/places.py +++ b/backend/trip/routers/places.py @@ -32,7 +32,7 @@ def read_places( @router.post("", response_model=PlaceRead) -def create_place( +async def create_place( place: PlaceCreate, session: SessionDep, current_user: Annotated[str, Depends(get_current_username)] ) -> PlaceRead: new_place = Place( @@ -51,16 +51,25 @@ def create_place( ) if place.image: - image_bytes = b64img_decode(place.image) - filename = save_image_to_file(image_bytes, settings.PLACE_IMAGE_SIZE) - if not filename: - raise HTTPException(status_code=400, detail="Bad request") - - image = Image(filename=filename, user=current_user) - session.add(image) - session.commit() - session.refresh(image) - new_place.image_id = image.id + if place.image[:4] == "http": + fp = await download_file(place.image) + if fp: + patch_image(fp) + image = Image(filename=fp.split("/")[-1], user=current_user) + session.add(image) + session.flush() + session.refresh(image) + new_place.image_id = image.id + else: + image_bytes = b64img_decode(place.image) + filename = save_image_to_file(image_bytes, settings.PLACE_IMAGE_SIZE) + if not filename: + raise HTTPException(status_code=400, detail="Bad request") + image = Image(filename=filename, user=current_user) + session.add(image) + session.commit() + session.refresh(image) + new_place.image_id = image.id session.add(new_place) session.commit() diff --git a/backend/trip/utils/gmaps.py b/backend/trip/utils/gmaps.py index 85a50a9..3f53b67 100644 --- a/backend/trip/utils/gmaps.py +++ b/backend/trip/utils/gmaps.py @@ -3,6 +3,16 @@ from typing import Any import httpx from fastapi import HTTPException +gmaps_types_mapper: dict[str, list] = { + "Nature & Outdoor": ["natural_feature", "landmark"], + "Entertainment & Leisure": ["amusement", "aquarium"], + "Culture": ["museum", "historical", "art_", "church"], + "Food & Drink": ["food", "bar", "bakery", "coffee_shop", "restaurant"], + "Adventure & Sports": ["adventure_sports_center"], + "Wellness": ["wellness"], + "Accommodation": ["hotel", "camping"], +} + def compute_avg_price(price_range: dict | None) -> float | None: if not price_range: @@ -42,7 +52,7 @@ async def gmaps_textsearch(search: str, api_key: str) -> list[dict[str, Any]]: headers = { "Content-Type": "application/json", "X-Goog-Api-Key": api_key, - "X-Goog-FieldMask": "places.id,places.types,places.location,places.priceRange,places.formattedAddress,places.websiteUri,places.internationalPhoneNumber,places.displayName,places.allowsDogs", + "X-Goog-FieldMask": "places.id,places.types,places.location,places.priceRange,places.formattedAddress,places.websiteUri,places.internationalPhoneNumber,places.displayName,places.allowsDogs,places.photos", } try: diff --git a/backend/trip/utils/utils.py b/backend/trip/utils/utils.py index 97f208b..0ae8550 100644 --- a/backend/trip/utils/utils.py +++ b/backend/trip/utils/utils.py @@ -117,7 +117,7 @@ async def download_file(link: str, raise_on_error: bool = False) -> str: path = assets_folder_path() / generate_filename(infer_extension) with open(path, "wb") as f: f.write(response.content) - return f.name + return str(path) except Exception as e: if raise_on_error: raise HTTPException(status_code=400, detail=f"Failed to download file: {e}")