Skip to content

Commit 7128ce4

Browse files
authored
Cdr export results rework (#820)
### Description Close #577
1 parent d1b964d commit 7128ce4

File tree

6 files changed

+513
-177
lines changed

6 files changed

+513
-177
lines changed

app/modules/cdr/endpoints_cdr.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
import re
33
from collections.abc import Sequence
44
from datetime import UTC, date, datetime
5-
from pathlib import Path
5+
from io import BytesIO
66
from uuid import UUID, uuid4
77

88
import calypsso
99
from fastapi import (
1010
Depends,
1111
HTTPException,
12+
Response,
1213
WebSocket,
1314
)
1415
from fastapi.responses import FileResponse
@@ -418,7 +419,7 @@ async def generate_and_send_results(
418419
# emails: schemas_cdr.ResultRequest,
419420
db: AsyncSession,
420421
# settings: Settings,
421-
) -> Path:
422+
) -> bytes:
422423
cdr_year = await get_core_data(coredata_cdr.CdrYear, db)
423424
seller = await cruds_cdr.get_seller_by_id(db, seller_id)
424425
if not seller:
@@ -478,33 +479,28 @@ async def generate_and_send_results(
478479
each_user.id,
479480
),
480481
)
482+
481483
hyperion_error_logger.info(
482-
f"Data for seller {seller.name} fetched. Starting to construct the dataframe.",
484+
f"Data for seller {seller.name} fetched. Generating the Excel file.",
483485
)
484-
df = construct_dataframe_from_users_purchases(
486+
487+
excel_io = BytesIO()
488+
489+
construct_dataframe_from_users_purchases(
485490
users_purchases=purchases_by_users,
486491
users=list(users),
487492
products=list(products),
488493
variants=variants,
489494
data_fields=product_fields,
490495
users_answers=users_answers,
491-
)
492-
hyperion_error_logger.info(
493-
f"Dataframe for seller {seller.name} constructed. Generating the Excel file.",
496+
export_io=excel_io,
494497
)
495498

496-
file_directory = "/app/data/cdr"
497-
file_uuid = uuid4()
498-
# file_name = f"CdR {datetime.now(tz=UTC).year} ventes {seller.name}.xlsx"
499+
res = excel_io.getvalue()
499500

500-
Path.mkdir(Path(file_directory), parents=True, exist_ok=True)
501-
df.to_excel(
502-
Path(file_directory, str(file_uuid)),
503-
index=False,
504-
freeze_panes=(2, 3),
505-
engine="xlsxwriter",
506-
)
507-
return Path(file_directory, str(file_uuid))
501+
excel_io.close()
502+
503+
return res
508504

509505
# Not working, we have to keep the file in the server
510506
# hyperion_error_logger.debug(
@@ -545,8 +541,16 @@ async def send_seller_results(
545541

546542
await is_user_in_a_seller_group(seller_id, user=user, db=db)
547543

548-
path = await generate_and_send_results(seller_id=seller_id, db=db)
549-
return FileResponse(path)
544+
res = await generate_and_send_results(seller_id=seller_id, db=db)
545+
546+
headers = {
547+
"Content-Disposition": f'attachment; filename="results_{seller_id}.xlsx"',
548+
}
549+
return Response(
550+
res,
551+
headers=headers,
552+
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
553+
)
550554

551555

552556
@module.router.get(

0 commit comments

Comments
 (0)