11from fastapi import FastAPI , UploadFile , File , Depends , HTTPException , Form
22from fastapi .middleware .cors import CORSMiddleware
3- from sqlalchemy import create_engine , Column , Integer , String , Date , Text
4- from sqlalchemy .orm import sessionmaker , declarative_base , Session
3+ from sqlalchemy .orm import Session
54from pydantic import BaseModel
65from datetime import date
76import os , shutil
87from dotenv import load_dotenv
98import requests
109
11- # ✅ Import users and models
12- from app .models import user
10+ # ✅ Import from database (moved get_db here to avoid circular import)
11+ from app .database import Base , engine , get_db
12+
13+ # ✅ Import users router (no circular import now)
1314from app .routers import users
1415
1516# Optional OpenAI
2122# Load environment variables
2223load_dotenv ()
2324
24- # === Database Setup ===
25- DATABASE_URL = os .getenv ("DATABASE_URL" , "sqlite:///./app.db" )
26- engine = create_engine (DATABASE_URL , pool_pre_ping = True )
27- SessionLocal = sessionmaker (autocommit = False , autoflush = False , bind = engine )
28- Base = declarative_base ()
25+ # === FastAPI App ===
26+ app = FastAPI (title = "FAT-EIBL (Edme) – API" )
27+
28+ # === CORS ===
29+ allow = os .getenv ("ALLOW_ORIGINS" , "*" ).split ("," )
30+ app .add_middleware (
31+ CORSMiddleware ,
32+ allow_origins = allow ,
33+ allow_credentials = True ,
34+ allow_methods = ["*" ],
35+ allow_headers = ["*" ],
36+ )
37+
38+ # === Models (local models only for tasks/audit) ===
39+ from sqlalchemy import Column , Integer , String , Date , Text
2940
30- # === Models ===
3141class Task (Base ):
3242 __tablename__ = "tasks"
3343 id = Column (Integer , primary_key = True , index = True )
@@ -46,30 +56,9 @@ class AuditLog(Base):
4656 action = Column (String (50 ))
4757 detail = Column (Text )
4858
49- # ✅ Create all tables (includes users table too )
59+ # ✅ Create all tables (includes Task, AuditLog, and user models )
5060Base .metadata .create_all (bind = engine )
5161
52- # === DB Session ===
53- def get_db ():
54- db = SessionLocal ()
55- try :
56- yield db
57- finally :
58- db .close ()
59-
60- # === FastAPI App ===
61- app = FastAPI (title = "FAT-EIBL (Edme) – API" )
62-
63- # === CORS ===
64- allow = os .getenv ("ALLOW_ORIGINS" , "*" ).split ("," )
65- app .add_middleware (
66- CORSMiddleware ,
67- allow_origins = allow ,
68- allow_credentials = True ,
69- allow_methods = ["*" ],
70- allow_headers = ["*" ],
71- )
72-
7362# === Pydantic Schemas ===
7463class TaskCreate (BaseModel ):
7564 title : str
@@ -189,4 +178,3 @@ def ai_chat(prompt: str = Form(...)):
189178
190179# --- USER ROUTES ---
191180app .include_router (users .router , prefix = "/users" , tags = ["Users" ])
192-
0 commit comments