From 5a6488ccc69c2ce413f5e02c8c491458141d3aa6 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Thu, 27 Mar 2025 18:47:13 +0000 Subject: [PATCH] Add helper functions for FoodImage --- helpers/foodimage_helpers.py | 104 +++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 helpers/foodimage_helpers.py diff --git a/helpers/foodimage_helpers.py b/helpers/foodimage_helpers.py new file mode 100644 index 0000000..96f7327 --- /dev/null +++ b/helpers/foodimage_helpers.py @@ -0,0 +1,104 @@ +from typing import Optional, Dict, List, Union +from enum import Enum +from datetime import datetime +from sqlalchemy.orm import Session +import re +import requests +from PIL import Image +from io import BytesIO + +class ImageStatus(Enum): + PENDING = "pending" + GENERATING = "generating" + COMPLETED = "completed" + FAILED = "failed" + +def validate_image_url(image_url: str) -> bool: + """ + Validates if the provided URL is a valid image URL. + + Args: + image_url: URL string to validate + + Returns: + bool: True if valid image URL, False otherwise + """ + try: + response = requests.head(image_url) + return response.headers.get('content-type', '').startswith('image/') + except: + return False + +def validate_prompt(prompt: str) -> bool: + """ + Validates if the provided prompt meets requirements. + + Args: + prompt: The prompt string to validate + + Returns: + bool: True if valid prompt, False otherwise + """ + # Prompt should be between 3 and 500 chars and not just whitespace + if not prompt or len(prompt.strip()) < 3 or len(prompt) > 500: + return False + return True + +def get_food_image_by_status( + db: Session, + status: ImageStatus +) -> List['FoodImage']: + """ + Retrieves food images by status. + + Args: + db: Database session + status: Status to filter by + + Returns: + List of FoodImage objects matching the status + """ + return db.query(FoodImage).filter(FoodImage.status == status.value).all() + +def update_image_status( + db: Session, + food_image_id: int, + new_status: ImageStatus +) -> Optional['FoodImage']: + """ + Updates the status of a food image. + + Args: + db: Database session + food_image_id: ID of image to update + new_status: New status to set + + Returns: + Updated FoodImage object if found, None otherwise + """ + food_image = db.query(FoodImage).filter(FoodImage.id == food_image_id).first() + if food_image: + food_image.status = new_status.value + db.commit() + db.refresh(food_image) + return food_image + +def verify_image_dimensions(image_url: str, min_width: int = 512, min_height: int = 512) -> bool: + """ + Verifies if image meets minimum dimension requirements. + + Args: + image_url: URL of image to verify + min_width: Minimum required width + min_height: Minimum required height + + Returns: + bool: True if image meets requirements, False otherwise + """ + try: + response = requests.get(image_url) + img = Image.open(BytesIO(response.content)) + width, height = img.size + return width >= min_width and height >= min_height + except: + return False \ No newline at end of file