diff --git a/auth.py b/auth.py index 75be3f0..1d6d7a5 100644 --- a/auth.py +++ b/auth.py @@ -204,7 +204,9 @@ def _clear_auth_cookies(response: RedirectResponse) -> RedirectResponse: @router.get("/login", response_class=HTMLResponse) async def login_page(request: Request): - return templates.TemplateResponse(request, "auth/login.html", {}) + return templates.TemplateResponse(request, "auth/login.html", { + "price_pref": request.state.price_pref, + }) @router.post("/login") @@ -216,6 +218,7 @@ async def login_submit(request: Request, db: db_dependency): if not user: return templates.TemplateResponse(request, "auth/login.html", { "error": "Invalid username or password", + "price_pref": request.state.price_pref, }) access = create_access_token(user.username, user.id) refresh = create_refresh_token(user.username, user.id, db) @@ -226,7 +229,9 @@ async def login_submit(request: Request, db: db_dependency): @router.get("/register", response_class=HTMLResponse) async def register_page(request: Request): - return templates.TemplateResponse(request, "auth/register.html", {}) + return templates.TemplateResponse(request, "auth/register.html", { + "price_pref": request.state.price_pref, + }) @router.post("/register") @@ -258,6 +263,7 @@ async def register_submit(request: Request, db: db_dependency): "username": username, "email": email, "full_name": full_name, + "price_pref": request.state.price_pref, }) db.add(User( diff --git a/config.py b/config.py index befc5a2..a44220c 100644 --- a/config.py +++ b/config.py @@ -14,6 +14,16 @@ _loader = jinja2.FileSystemLoader("templates") _env = jinja2.Environment(loader=_loader, autoescape=jinja2.select_autoescape(), cache_size=0) templates = Jinja2Templates(env=_env) +# Custom Jinja2 filter: {{ prop.price|price_fmt(price_pref, prop.area_sqft) }} +from price_pref import format_price as _format_price + + +def _price_fmt(value, price_pref="total", area_sqft=0): + return _format_price(value, price_pref, area_sqft) + + +_env.filters["price_fmt"] = _price_fmt + bcrypt_context = CryptContext(schemes=["bcrypt"], deprecated="auto") oauth2_bearer = OAuth2PasswordBearer(tokenUrl="auth/token", auto_error=False) diff --git a/main.py b/main.py index 79a2976..39a1d2d 100644 --- a/main.py +++ b/main.py @@ -35,6 +35,15 @@ class RefreshTokenMiddleware(BaseHTTPMiddleware): app.add_middleware(RefreshTokenMiddleware) +class PricePrefMiddleware(BaseHTTPMiddleware): + async def dispatch(self, request: Request, call_next): + from price_pref import get_price_pref + request.state.price_pref = get_price_pref(request) + return await call_next(request) + +app.add_middleware(PricePrefMiddleware) + + db_dependency = Annotated[Session, Depends(get_db)] user_dependency = Annotated[Optional[dict], Depends(get_current_user)] @@ -64,4 +73,5 @@ async def homepage(request: Request, db: db_dependency, user: user_dependency): return templates.TemplateResponse(request, "index.html", { "user": user, "properties": featured, + "price_pref": request.state.price_pref, }) diff --git a/price_pref.py b/price_pref.py new file mode 100644 index 0000000..0ad38fe --- /dev/null +++ b/price_pref.py @@ -0,0 +1,19 @@ +VALID_PRICE_PREFS = ("total", "sqft", "both") + + +def get_price_pref(request) -> str: + pref = request.cookies.get("price_pref", "total") + if pref not in VALID_PRICE_PREFS: + pref = "total" + return pref + + +def format_price(value: int, price_pref: str = "total", area_sqft: int = 0) -> str: + + if area_sqft > 0: + unit_price = round(value / area_sqft) + if price_pref == "sqft": + return f"${unit_price:,}/sqft" + if price_pref == "both": + return f"${value:,} (${unit_price:,}/sqft)" + return f"${value:,}" diff --git a/properties.py b/properties.py index c3a5e12..34044d7 100644 --- a/properties.py +++ b/properties.py @@ -52,9 +52,17 @@ async def property_list(request: Request, db: Session = Depends(get_db)): if prop_type: query = query.filter(Property.property_type == prop_type) if min_price: - query = query.filter(Property.price >= int(min_price)) + min_val = int(min_price) + if request.state.price_pref == "sqft": + query = query.filter(Property.price >= min_val * Property.area_sqft) + else: + query = query.filter(Property.price >= min_val) if max_price: - query = query.filter(Property.price <= int(max_price)) + max_val = int(max_price) + if request.state.price_pref == "sqft": + query = query.filter(Property.price <= max_val * Property.area_sqft) + else: + query = query.filter(Property.price <= max_val) if bedrooms: query = query.filter(Property.bedrooms >= int(bedrooms)) if city: @@ -91,6 +99,7 @@ async def property_list(request: Request, db: Session = Depends(get_db)): "bedrooms": bedrooms, "city_filter": city, "state_filter": state, + "price_pref": request.state.price_pref, }) @@ -102,6 +111,7 @@ async def property_create_form(request: Request, db: Session = Depends(get_db)): return templates.TemplateResponse(request, "properties/create.html", { "user": user, "values": {}, + "price_pref": request.state.price_pref, }) @@ -154,6 +164,7 @@ async def property_create_submit( "user": user, "values": {k: v for k, v in request._form.items()} if hasattr(request, '_form') else {}, "error": "Please provide at least one contact method (email or phone).", + "price_pref": request.state.price_pref, }) prop = Property( @@ -211,6 +222,7 @@ async def property_detail(prop_id: int, request: Request, db: Session = Depends( "primary_image": primary_image, "images": images, "is_favorited": is_favorited, + "price_pref": request.state.price_pref, }) @@ -231,6 +243,7 @@ async def property_edit_form(prop_id: int, request: Request, db: Session = Depen "user": user, "prop": prop, "primary_image": primary_image, + "price_pref": request.state.price_pref, }) @@ -365,4 +378,5 @@ async def dashboard(request: Request, db: Session = Depends(get_db)): "user": user, "my_properties": my_properties, "favorites": favorites, + "price_pref": request.state.price_pref, }) diff --git a/templates/components/navbar.html b/templates/components/navbar.html index eecffa0..6098b3e 100644 --- a/templates/components/navbar.html +++ b/templates/components/navbar.html @@ -16,6 +16,38 @@ Rent Sell + +