Starting implementation of user made custom CSS templates
This commit is contained in:
66
main.py
66
main.py
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
from fastapi import FastAPI, Request, Depends, HTTPException
|
||||
from starlette.middleware.sessions import SessionMiddleware
|
||||
from starlette.staticfiles import StaticFiles
|
||||
from starlette.staticfiles import StaticFiles
|
||||
from starlette.responses import FileResponse, RedirectResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from contextlib import asynccontextmanager
|
||||
@@ -63,7 +63,14 @@ async def read_dashboard(request: Request, db: Session = Depends(auth.get_db)):
|
||||
user.settings = models.Setting()
|
||||
db.commit()
|
||||
|
||||
return templates.TemplateResponse("dashboard.html", {"request": request, "user": user, "overlay_url": overlay_url, "current_theme": user.settings.overlay_theme, "settings": settings})
|
||||
return templates.TemplateResponse("dashboard.html", {
|
||||
"request": request,
|
||||
"user": user,
|
||||
"overlay_url": overlay_url,
|
||||
"current_theme": user.settings.overlay_theme,
|
||||
"settings": settings,
|
||||
"custom_themes": user.custom_themes
|
||||
})
|
||||
|
||||
@app.get("/logout")
|
||||
async def logout(request: Request):
|
||||
@@ -78,13 +85,20 @@ async def read_overlay(request: Request, user_id: int, theme_override: str = Non
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
# The theme can be forced by a query parameter for previewing
|
||||
theme_name = "dark-purple" # Default theme
|
||||
if theme_override:
|
||||
theme = theme_override
|
||||
else:
|
||||
theme = "dark-purple" # Default theme
|
||||
if user.settings and user.settings.overlay_theme:
|
||||
theme = user.settings.overlay_theme
|
||||
theme_name = theme_override
|
||||
elif user.settings and user.settings.overlay_theme:
|
||||
theme_name = user.settings.overlay_theme
|
||||
|
||||
# Check if it's a custom theme
|
||||
if theme_name.startswith("custom-"):
|
||||
theme_id = int(theme_name.split("-")[1])
|
||||
theme = db.query(models.CustomTheme).filter(models.CustomTheme.id == theme_id, models.CustomTheme.owner_id == user.id).first()
|
||||
if not theme:
|
||||
raise HTTPException(status_code=404, detail="Custom theme not found")
|
||||
# Use a generic overlay template that will link to the dynamic CSS
|
||||
return templates.TemplateResponse("overlay-custom.html", {"request": request, "theme_id": theme.id})
|
||||
|
||||
return templates.TemplateResponse(f"overlay-{theme}.html", {"request": request})
|
||||
|
||||
@@ -101,4 +115,38 @@ async def update_settings(settings_data: schemas.SettingsUpdate, request: Reques
|
||||
user.settings.overlay_theme = settings_data.overlay_theme
|
||||
db.commit()
|
||||
|
||||
return {"message": "Settings updated successfully"}
|
||||
return {"message": "Settings updated successfully"}
|
||||
|
||||
@app.post("/api/themes", response_model=schemas.CustomTheme)
|
||||
async def create_theme(theme_data: schemas.CustomThemeCreate, request: Request, db: Session = Depends(auth.get_db)):
|
||||
user_id = request.session.get('user_id')
|
||||
if not user_id:
|
||||
raise HTTPException(status_code=401, detail="Not authenticated")
|
||||
|
||||
new_theme = models.CustomTheme(**theme_data.dict(), owner_id=user_id)
|
||||
db.add(new_theme)
|
||||
db.commit()
|
||||
db.refresh(new_theme)
|
||||
return new_theme
|
||||
|
||||
@app.delete("/api/themes/{theme_id}")
|
||||
async def delete_theme(theme_id: int, request: Request, db: Session = Depends(auth.get_db)):
|
||||
user_id = request.session.get('user_id')
|
||||
if not user_id:
|
||||
raise HTTPException(status_code=401, detail="Not authenticated")
|
||||
|
||||
theme = db.query(models.CustomTheme).filter(models.CustomTheme.id == theme_id, models.CustomTheme.owner_id == user_id).first()
|
||||
if not theme:
|
||||
raise HTTPException(status_code=404, detail="Theme not found")
|
||||
|
||||
db.delete(theme)
|
||||
db.commit()
|
||||
return {"message": "Theme deleted successfully"}
|
||||
|
||||
@app.get("/css/custom/{theme_id}")
|
||||
async def get_custom_css(theme_id: int, db: Session = Depends(auth.get_db)):
|
||||
theme = db.query(models.CustomTheme).filter(models.CustomTheme.id == theme_id).first()
|
||||
if not theme:
|
||||
raise HTTPException(status_code=404, detail="Custom theme not found")
|
||||
|
||||
return Response(content=theme.css_content, media_type="text/css")
|
||||
Reference in New Issue
Block a user