|
7 | 7 | import os, shutil |
8 | 8 | from dotenv import load_dotenv |
9 | 9 |
|
10 | | -# ⬇️ your database bits |
| 10 | +# ============================== |
| 11 | +# 1️⃣ Load environment variables |
| 12 | +# ============================== |
11 | 13 | load_dotenv() |
| 14 | + |
12 | 15 | DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./app.db") |
13 | 16 | engine = create_engine(DATABASE_URL, pool_pre_ping=True) |
14 | 17 | SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) |
15 | 18 | Base = declarative_base() |
16 | 19 |
|
17 | | -# ⬇️ import models so SQLAlchemy knows about User |
18 | | -from app import models |
19 | | -# … define Task/AuditLog classes … |
| 20 | + |
| 21 | +# ============================== |
| 22 | +# 2️⃣ Database models (Tasks, Audit Logs) |
| 23 | +# ============================== |
| 24 | +class Task(Base): |
| 25 | + __tablename__ = "tasks" |
| 26 | + id = Column(Integer, primary_key=True, index=True) |
| 27 | + title = Column(String(255), nullable=False) |
| 28 | + department = Column(String(100), nullable=True) |
| 29 | + assignee = Column(String(100), nullable=True) |
| 30 | + status = Column(String(50), nullable=False, default="Pending") |
| 31 | + due_date = Column(Date, nullable=True) |
| 32 | + priority = Column(String(20), nullable=True) |
| 33 | + remarks = Column(Text, nullable=True) |
| 34 | + attachment = Column(String(255), nullable=True) |
| 35 | + |
| 36 | + |
| 37 | +class AuditLog(Base): |
| 38 | + __tablename__ = "audit_logs" |
| 39 | + id = Column(Integer, primary_key=True) |
| 40 | + action = Column(String(50)) |
| 41 | + detail = Column(Text) |
| 42 | + |
| 43 | + |
| 44 | +# ✅ Create all tables initially (will also be re-created by /create-db) |
20 | 45 | Base.metadata.create_all(bind=engine) |
21 | 46 |
|
| 47 | + |
| 48 | +# ============================== |
| 49 | +# 3️⃣ FastAPI app setup |
| 50 | +# ============================== |
22 | 51 | app = FastAPI(title="FAT-EIBL (Edme) – API") |
23 | 52 |
|
24 | | -# CORS |
| 53 | +# ✅ Enable CORS for frontend |
25 | 54 | allow = os.getenv("ALLOW_ORIGINS", "*").split(",") |
26 | 55 | app.add_middleware( |
27 | 56 | CORSMiddleware, |
|
31 | 60 | allow_headers=["*"], |
32 | 61 | ) |
33 | 62 |
|
34 | | -# at the bottom: include users router |
35 | | -from app.routers import users |
| 63 | + |
| 64 | +# ============================== |
| 65 | +# 4️⃣ Database dependency |
| 66 | +# ============================== |
| 67 | +def get_db(): |
| 68 | + db = SessionLocal() |
| 69 | + try: |
| 70 | + yield db |
| 71 | + finally: |
| 72 | + db.close() |
| 73 | + |
| 74 | + |
| 75 | +# ============================== |
| 76 | +# 5️⃣ Import Routers (User + Forgot Password) |
| 77 | +# ============================== |
| 78 | +from app.routers import users, forgot_password |
| 79 | + |
36 | 80 | app.include_router(users.router, prefix="/users", tags=["Users"]) |
37 | | -# --- TEMPORARY: Create Database Tables on Render --- |
| 81 | +app.include_router(forgot_password.router, prefix="/users", tags=["Forgot Password"]) |
| 82 | + |
| 83 | + |
| 84 | +# ============================== |
| 85 | +# 6️⃣ Health check |
| 86 | +# ============================== |
| 87 | +@app.get("/health") |
| 88 | +def health(): |
| 89 | + return {"status": "ok", "message": "Backend is running"} |
| 90 | + |
| 91 | + |
| 92 | +# ============================== |
| 93 | +# 7️⃣ File Uploads (optional) |
| 94 | +# ============================== |
| 95 | +UPLOAD_DIR = os.path.join(os.getcwd(), "uploads") |
| 96 | +os.makedirs(UPLOAD_DIR, exist_ok=True) |
| 97 | + |
| 98 | + |
| 99 | +@app.post("/upload/{task_id}") |
| 100 | +async def upload_file(task_id: int, file: UploadFile = File(...), db: Session = Depends(get_db)): |
| 101 | + obj = db.get(Task, task_id) |
| 102 | + if not obj: |
| 103 | + raise HTTPException(status_code=404, detail="Task not found") |
| 104 | + |
| 105 | + safe_name = f"task_{task_id}_" + os.path.basename(file.filename) |
| 106 | + dest = os.path.join(UPLOAD_DIR, safe_name) |
| 107 | + |
| 108 | + with open(dest, "wb") as buffer: |
| 109 | + shutil.copyfileobj(file.file, buffer) |
| 110 | + |
| 111 | + obj.attachment = safe_name |
| 112 | + db.add(AuditLog(action="upload", detail=f"Task ID {task_id} file {safe_name}")) |
| 113 | + db.commit() |
| 114 | + |
| 115 | + return {"task_id": task_id, "filename": safe_name} |
| 116 | + |
| 117 | + |
| 118 | +# ============================== |
| 119 | +# 8️⃣ Emergency endpoint: Create database tables |
| 120 | +# ============================== |
38 | 121 | from app.models.user import User |
39 | | -from app.database import Base, engine |
| 122 | +from app.database import Base as DBBase, engine as DBEngine |
| 123 | + |
40 | 124 |
|
41 | 125 | @app.get("/create-db") |
42 | 126 | def create_database(): |
| 127 | + """ |
| 128 | + 🔧 Use this endpoint on Render once if tables fail to auto-create. |
| 129 | + """ |
43 | 130 | try: |
44 | | - Base.metadata.create_all(bind=engine) |
| 131 | + DBBase.metadata.create_all(bind=DBEngine) |
45 | 132 | return {"ok": True, "message": "Database tables created successfully"} |
46 | 133 | except Exception as e: |
47 | 134 | return {"ok": False, "error": str(e)} |
48 | | - |
|
0 commit comments