reproduce ganador en dispositivo seleccionado si lo permite
- play_winner acepta device_id del formulario y verifica que el dispositivo exista y no sea restringido antes de reproducir - corrige construcción del URI según spotify_type del ganador (antes siempre usaba spotify:playlist: independiente del tipo) - errores se muestran en la página admin en lugar de HTTPException cruda - template sincroniza device_id activo al campo oculto del formulario - dispositivos restringidos se marcan en el selector del admin Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+46
-4
@@ -340,18 +340,60 @@ def reset_votes(request: Request, db: Session = Depends(get_db)):
|
||||
|
||||
|
||||
@router.post("/voting/play-winner")
|
||||
def play_winner(request: Request, db: Session = Depends(get_db)):
|
||||
def play_winner(
|
||||
request: Request,
|
||||
device_id: str = Form(default=""),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
_require_admin(request)
|
||||
results = _vote_results(db)
|
||||
config = _get_or_create_config(db)
|
||||
now_str = datetime.now().strftime("%H:%M:%S")
|
||||
|
||||
def _error(msg: str):
|
||||
return templates.TemplateResponse(
|
||||
"admin/voting.html",
|
||||
{
|
||||
"request": request,
|
||||
"config": config,
|
||||
"results": results,
|
||||
"total_votes": sum(r["votes"] for r in results),
|
||||
"server_time": now_str,
|
||||
"error": msg,
|
||||
"success": None,
|
||||
},
|
||||
status_code=400,
|
||||
)
|
||||
|
||||
if not results or results[0]["votes"] == 0:
|
||||
raise HTTPException(status_code=400, detail="No hay votos registrados")
|
||||
return _error("No hay votos registrados")
|
||||
|
||||
winner = results[0]["playlist"]
|
||||
|
||||
try:
|
||||
sp = spotify.get_client()
|
||||
sp.start_playback(context_uri=f"spotify:playlist:{winner.spotify_id}")
|
||||
|
||||
if device_id:
|
||||
devices = sp.devices().get("devices", [])
|
||||
device = next((d for d in devices if d["id"] == device_id), None)
|
||||
if not device:
|
||||
return _error("El dispositivo seleccionado ya no está disponible")
|
||||
if device.get("is_restricted"):
|
||||
return _error(
|
||||
f"El dispositivo «{device['name']}» no permite reproducción remota"
|
||||
)
|
||||
|
||||
kwargs: dict = {}
|
||||
if device_id:
|
||||
kwargs["device_id"] = device_id
|
||||
if winner.spotify_type == "track":
|
||||
kwargs["uris"] = [f"spotify:track:{winner.spotify_id}"]
|
||||
else:
|
||||
kwargs["context_uri"] = f"spotify:{winner.spotify_type}:{winner.spotify_id}"
|
||||
|
||||
sp.start_playback(**kwargs)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
return _error(f"Error al reproducir: {e}")
|
||||
|
||||
db.query(Vote).delete()
|
||||
db.commit()
|
||||
|
||||
Reference in New Issue
Block a user