feature: added route to export xlsx
This commit is contained in:
parent
1716b5d0a3
commit
8c9caec943
52
backend/controllers/export.py
Normal file
52
backend/controllers/export.py
Normal file
@ -0,0 +1,52 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from models.users import User
|
||||
from configuration import DBSessionDep
|
||||
import pandas as pd
|
||||
from sqlalchemy import text
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
router = APIRouter(prefix="/api")
|
||||
|
||||
@router.get("/study-track-access-log")
|
||||
def get_study_track_access_log(session: DBSessionDep):
|
||||
try:
|
||||
|
||||
result = session.execute()
|
||||
rows = result.fetchall()
|
||||
|
||||
data = []
|
||||
for row in rows:
|
||||
data.append(dict(row._mapping))
|
||||
|
||||
if not data:
|
||||
return {"message": "Nenhum dado encontrado"}
|
||||
|
||||
df = pd.DataFrame(data)
|
||||
|
||||
excel_file = "modelo.xlsx"
|
||||
|
||||
if os.path.exists(excel_file):
|
||||
with pd.ExcelWriter(excel_file, engine='openpyxl', mode='a', if_sheet_exists='replace') as writer:
|
||||
df.to_excel(writer, sheet_name='Study Track Access Log', index=False)
|
||||
else:
|
||||
df.to_excel(excel_file, sheet_name='Study Track Access Log', index=False)
|
||||
|
||||
update_info = pd.DataFrame({
|
||||
'Última Atualização': [datetime.now().strftime("%Y-%m-%d %H:%M:%S")],
|
||||
'Total de Registros': [len(data)]
|
||||
})
|
||||
|
||||
with pd.ExcelWriter(excel_file, engine='openpyxl', mode='a', if_sheet_exists='replace') as writer:
|
||||
update_info.to_excel(writer, sheet_name='Info Atualização', index=False)
|
||||
|
||||
return {
|
||||
"message": f"Dados inseridos com sucesso na planilha {excel_file}",
|
||||
"total_records": len(data),
|
||||
"excel_file": excel_file
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Erro ao processar dados: {str(e)}")
|
||||
finally:
|
||||
session.close()
|
||||
@ -1 +1,47 @@
|
||||
sqlalchemy>=2.0.0
|
||||
alembic==1.13.3
|
||||
annotated-types==0.7.0
|
||||
anyio==4.6.2.post1
|
||||
certifi==2024.8.30
|
||||
click==8.1.7
|
||||
dnspython==2.7.0
|
||||
email_validator==2.2.0
|
||||
et_xmlfile==2.0.0
|
||||
fastapi==0.115.4
|
||||
fastapi-cli==0.0.5
|
||||
greenlet==3.1.1
|
||||
h11==0.14.0
|
||||
httpcore==1.0.6
|
||||
httptools==0.6.4
|
||||
httpx==0.27.2
|
||||
idna==3.10
|
||||
Jinja2==3.1.4
|
||||
Mako==1.3.6
|
||||
markdown-it-py==3.0.0
|
||||
MarkupSafe==3.0.2
|
||||
mdurl==0.1.2
|
||||
mysql==0.0.3
|
||||
mysqlclient==2.2.5
|
||||
numpy==2.3.3
|
||||
openpyxl==3.1.5
|
||||
pandas==2.3.2
|
||||
pydantic==2.9.2
|
||||
pydantic_core==2.23.4
|
||||
Pygments==2.18.0
|
||||
python-dateutil==2.9.0.post0
|
||||
python-dotenv==1.0.1
|
||||
python-multipart==0.0.16
|
||||
pytz==2025.2
|
||||
PyYAML==6.0.2
|
||||
rich==13.9.3
|
||||
shellingham==1.5.4
|
||||
six==1.17.0
|
||||
sniffio==1.3.1
|
||||
SQLAlchemy==2.0.36
|
||||
starlette==0.41.2
|
||||
typer==0.12.5
|
||||
typing_extensions==4.12.2
|
||||
tzdata==2025.2
|
||||
uvicorn==0.32.0
|
||||
uvloop==0.21.0
|
||||
watchfiles==0.24.0
|
||||
websockets==13.1
|
||||
Loading…
Reference in New Issue
Block a user