52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
from typing import Annotated, Optional
|
|
|
|
from fastapi import FastAPI, Depends, Request
|
|
from sqlalchemy.orm import Session
|
|
from starlette import status
|
|
from starlette.staticfiles import StaticFiles
|
|
|
|
import models
|
|
from auth import get_db, get_current_user, router as auth_router
|
|
from config import templates
|
|
from models import Property
|
|
from properties import router as properties_router
|
|
|
|
app = FastAPI()
|
|
|
|
app.include_router(auth_router)
|
|
app.include_router(properties_router)
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
models.Base.metadata.create_all(bind=__import__("database", fromlist=["engine"]).engine)
|
|
|
|
db_dependency = Annotated[Session, Depends(get_db)]
|
|
user_dependency = Annotated[Optional[dict], Depends(get_current_user)]
|
|
|
|
|
|
@app.get("/", status_code=status.HTTP_200_OK)
|
|
async def homepage(request: Request, db: db_dependency, user: user_dependency):
|
|
# Show featured properties; if none are featured, show the latest listings
|
|
featured = (
|
|
db.query(Property)
|
|
.filter(Property.is_featured == True, Property.status == "active")
|
|
.limit(6)
|
|
.all()
|
|
)
|
|
if not featured:
|
|
featured = (
|
|
db.query(Property)
|
|
.filter(Property.status == "active")
|
|
.order_by(Property.created_at.desc())
|
|
.limit(6)
|
|
.all()
|
|
)
|
|
for prop in featured:
|
|
prop.primary_image = next(
|
|
(img for img in prop.images if img.is_primary),
|
|
prop.images[0] if prop.images else None,
|
|
)
|
|
return templates.TemplateResponse(request, "index.html", {
|
|
"user": user,
|
|
"properties": featured,
|
|
})
|