Handle image URL for place creation
This commit is contained in:
parent
789a3937a6
commit
f4ecacf0c8
@ -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,11 +51,20 @@ def create_place(
|
||||
)
|
||||
|
||||
if place.image:
|
||||
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()
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -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}")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user