export results to csv
All checks were successful
release-tag / release-image (push) Successful in 5m49s
All checks were successful
release-tag / release-image (push) Successful in 5m49s
This commit is contained in:
@@ -3,12 +3,14 @@ import numpy as np
|
|||||||
import re
|
import re
|
||||||
import requests
|
import requests
|
||||||
import os
|
import os
|
||||||
from fastapi import APIRouter, Security, Depends
|
from fastapi import APIRouter, Security, Depends, HTTPException, Response
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from app.database import get_db, engine, Base
|
from app.database import get_db, engine, Base
|
||||||
from app.security import get_current_user
|
from app.security import get_current_user
|
||||||
from app.crud import create_song, get_setting, set_setting, get_all_songs_and_votes
|
from app.crud import create_song, get_setting, set_setting, get_all_songs_and_votes
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
prefix="/admin",
|
prefix="/admin",
|
||||||
@@ -130,5 +132,13 @@ async def toggle_veto_mode(db: Session = Depends(get_db)) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
@router.get("/results")
|
@router.get("/results")
|
||||||
async def get_evaluation(db = Depends(get_db)) -> list:
|
async def get_evaluation(format: str = "json", db = Depends(get_db)) -> Any:
|
||||||
return get_all_songs_and_votes(db)
|
res = get_all_songs_and_votes(db)
|
||||||
|
if format == "json":
|
||||||
|
return JSONResponse(content=res)
|
||||||
|
elif format == "csv":
|
||||||
|
df = pd.json_normalize(res)
|
||||||
|
df.columns = [c.split(".")[-1] for c in df.columns]
|
||||||
|
return Response(content=df.to_csv(), media_type="application/csv", headers={'Content-Disposition': 'attachment; filename="results.csv"'})
|
||||||
|
else:
|
||||||
|
raise HTTPException(status_code=404, detail="format must be json or csv")
|
||||||
|
|||||||
Reference in New Issue
Block a user