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)
|
@router.post("", response_model=PlaceRead)
|
||||||
def create_place(
|
async def create_place(
|
||||||
place: PlaceCreate, session: SessionDep, current_user: Annotated[str, Depends(get_current_username)]
|
place: PlaceCreate, session: SessionDep, current_user: Annotated[str, Depends(get_current_username)]
|
||||||
) -> PlaceRead:
|
) -> PlaceRead:
|
||||||
new_place = Place(
|
new_place = Place(
|
||||||
@ -51,16 +51,25 @@ def create_place(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if place.image:
|
if place.image:
|
||||||
image_bytes = b64img_decode(place.image)
|
if place.image[:4] == "http":
|
||||||
filename = save_image_to_file(image_bytes, settings.PLACE_IMAGE_SIZE)
|
fp = await download_file(place.image)
|
||||||
if not filename:
|
if fp:
|
||||||
raise HTTPException(status_code=400, detail="Bad request")
|
patch_image(fp)
|
||||||
|
image = Image(filename=fp.split("/")[-1], user=current_user)
|
||||||
image = Image(filename=filename, user=current_user)
|
session.add(image)
|
||||||
session.add(image)
|
session.flush()
|
||||||
session.commit()
|
session.refresh(image)
|
||||||
session.refresh(image)
|
new_place.image_id = image.id
|
||||||
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.add(new_place)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|||||||
@ -3,6 +3,16 @@ from typing import Any
|
|||||||
import httpx
|
import httpx
|
||||||
from fastapi import HTTPException
|
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:
|
def compute_avg_price(price_range: dict | None) -> float | None:
|
||||||
if not price_range:
|
if not price_range:
|
||||||
@ -42,7 +52,7 @@ async def gmaps_textsearch(search: str, api_key: str) -> list[dict[str, Any]]:
|
|||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"X-Goog-Api-Key": api_key,
|
"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:
|
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)
|
path = assets_folder_path() / generate_filename(infer_extension)
|
||||||
with open(path, "wb") as f:
|
with open(path, "wb") as f:
|
||||||
f.write(response.content)
|
f.write(response.content)
|
||||||
return f.name
|
return str(path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if raise_on_error:
|
if raise_on_error:
|
||||||
raise HTTPException(status_code=400, detail=f"Failed to download file: {e}")
|
raise HTTPException(status_code=400, detail=f"Failed to download file: {e}")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user