70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Header, Body, status, Request
|
|
from sqlalchemy.orm import Session
|
|
from fastapi.security import APIKeyHeader
|
|
from typing import Union, Optional, List
|
|
from typing_extensions import Annotated
|
|
from db import models, schemas, crud
|
|
import json
|
|
import logging
|
|
import datetime
|
|
from kafka import KafkaProducer
|
|
from fastapi.responses import FileResponse, StreamingResponse
|
|
import io
|
|
import openpyxl
|
|
|
|
from db.schemas import RawData, RtuGenerator
|
|
from db.base import get_db
|
|
from db.models import RawDatas, Raw_data_herit
|
|
import pandas as pd
|
|
|
|
KST = datetime.timezone(datetime.timedelta(hours=9))
|
|
|
|
router = APIRouter(
|
|
prefix="/api/card",
|
|
tags=["api", "card"],
|
|
responses={404: {"description": "Not found"}},
|
|
)
|
|
|
|
# -----------------------------------------------------------------------------------------------
|
|
|
|
async def get_body(request: Request):
|
|
return await request.body()
|
|
|
|
|
|
@router.post("/regist", response_model=schemas.CardExpense)
|
|
def create_card_expense(card_expense: schemas.CardExpenseCreate, db: Session = Depends(get_db)):
|
|
return crud.create_card_expense(db=db, card_expense=card_expense)
|
|
|
|
|
|
@router.post("/excel", response_model=schemas.CardExpense)
|
|
async def create_excel(payload: schemas.DatePayload, db: Session = Depends(get_db)):
|
|
results = crud.get_card_expenses_by_date(db, payload.year, payload.month)
|
|
|
|
if not results:
|
|
raise HTTPException(status_code=404, detail="No data found for the specified month")
|
|
|
|
# Convert results to pandas DataFrame
|
|
df = pd.DataFrame([dict(result) for result in results])
|
|
|
|
# Create an Excel file in memory
|
|
output = io.BytesIO()
|
|
writer = pd.ExcelWriter(output, engine='openpyxl')
|
|
df.to_excel(writer, index=False, sheet_name='Summary')
|
|
writer.save()
|
|
output.seek(0)
|
|
|
|
# Return the Excel file
|
|
response = StreamingResponse(output, media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
|
response.headers["Content-Disposition"] = "attachment; filename=summary.xlsx"
|
|
return response
|
|
|
|
|
|
# 테스트 URL
|
|
@router.post("/test")
|
|
async def col_raw_data_from_herit(
|
|
body: bytes = Depends(get_body)
|
|
):
|
|
return {
|
|
"reason": "OK2",
|
|
"body": body
|
|
} |