mirror of
https://github.com/sstent/foodplanner.git
synced 2026-01-25 11:11:36 +00:00
working
This commit is contained in:
89
main.py
89
main.py
@@ -16,48 +16,59 @@ import os
|
||||
import csv
|
||||
import requests
|
||||
from fastapi import File, UploadFile
|
||||
import logging
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
|
||||
# Database setup - Use SQLite for easier setup
|
||||
DATABASE_URL = f"sqlite:///{os.getenv('DATABASE_PATH', './data')}/meal_planner.db"
|
||||
DATABASE_PATH = os.getenv('DATABASE_PATH', './data')
|
||||
DATABASE_URL = f"sqlite:///{DATABASE_PATH}/meal_planner.db"
|
||||
|
||||
logging.info(f"Database URL: {DATABASE_URL}")
|
||||
|
||||
# Ensure the database directory exists
|
||||
os.makedirs(DATABASE_PATH, exist_ok=True)
|
||||
# For production, use PostgreSQL: DATABASE_URL = "postgresql://username:password@localhost/meal_planner"
|
||||
|
||||
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False} if "sqlite" in DATABASE_URL else {})
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
Base = declarative_base()
|
||||
try:
|
||||
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False} if "sqlite" in DATABASE_URL else {})
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
Base = declarative_base()
|
||||
|
||||
# Initialize FastAPI app
|
||||
app = FastAPI(title="Meal Planner")
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
# Initialize FastAPI app
|
||||
app = FastAPI(title="Meal Planner")
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
|
||||
# Database Models
|
||||
class Food(Base):
|
||||
__tablename__ = "foods"
|
||||
# Database Models
|
||||
class Food(Base):
|
||||
__tablename__ = "foods"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, unique=True, index=True)
|
||||
serving_size = Column(String)
|
||||
serving_unit = Column(String)
|
||||
calories = Column(Float)
|
||||
protein = Column(Float)
|
||||
carbs = Column(Float)
|
||||
fat = Column(Float)
|
||||
fiber = Column(Float, default=0)
|
||||
sugar = Column(Float, default=0)
|
||||
sodium = Column(Float, default=0)
|
||||
calcium = Column(Float, default=0)
|
||||
source = Column(String, default="manual") # manual, csv, openfoodfacts
|
||||
brand = Column(String, default="") # Brand name for the food
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, unique=True, index=True)
|
||||
serving_size = Column(String)
|
||||
serving_unit = Column(String)
|
||||
calories = Column(Float)
|
||||
protein = Column(Float)
|
||||
carbs = Column(Float)
|
||||
fat = Column(Float)
|
||||
fiber = Column(Float, default=0)
|
||||
sugar = Column(Float, default=0)
|
||||
sodium = Column(Float, default=0)
|
||||
calcium = Column(Float, default=0)
|
||||
source = Column(String, default="manual") # manual, csv, openfoodfacts
|
||||
brand = Column(String, default="") # Brand name for the food
|
||||
|
||||
class Meal(Base):
|
||||
__tablename__ = "meals"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, index=True)
|
||||
meal_type = Column(String) # breakfast, lunch, dinner, snack, custom
|
||||
meal_time = Column(String, default="Breakfast") # Breakfast, Lunch, Dinner, Snack 1, Snack 2, Beverage 1, Beverage 2
|
||||
|
||||
# Relationship to meal foods
|
||||
meal_foods = relationship("MealFood", back_populates="meal")
|
||||
class Meal(Base):
|
||||
__tablename__ = "meals"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, index=True)
|
||||
meal_type = Column(String) # breakfast, lunch, dinner, snack, custom
|
||||
meal_time = Column(String, default="Breakfast") # Breakfast, Lunch, Dinner, Snack 1, Snack 2, Beverage 1, Beverage 2
|
||||
|
||||
# Relationship to meal foods
|
||||
meal_foods = relationship("MealFood", back_populates="meal")
|
||||
|
||||
class MealFood(Base):
|
||||
__tablename__ = "meal_foods"
|
||||
@@ -204,8 +215,14 @@ def get_db():
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
# Create tables
|
||||
Base.metadata.create_all(bind=engine)
|
||||
# Create tables
|
||||
Base.metadata.create_all(bind=engine)
|
||||
logging.info("Database tables checked/created successfully.")
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to connect to or create database at {DATABASE_URL}: {e}")
|
||||
# Depending on desired behavior, you might want to exit the application or handle it gracefully
|
||||
# For now, we'll re-raise to ensure the application doesn't start with a broken DB connection
|
||||
raise
|
||||
|
||||
# Utility functions
|
||||
def calculate_meal_nutrition(meal, db: Session):
|
||||
|
||||
Reference in New Issue
Block a user