Work on temporary hosted file downloads and export creation.

This commit is contained in:
Scott Idem
2021-11-23 16:51:41 -05:00
parent 4fa32f63fd
commit 24c7411109
5 changed files with 114 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ from __future__ import annotations
# import datetime, hashlib, os, pathlib, shutil, time
#from datetime import datetime, time, timedelta
from fastapi import APIRouter, Body, Depends, File, Form, Header, HTTPException, Query, Response, status, UploadFile
from fastapi.responses import FileResponse
from pydantic import BaseModel, EmailStr, Field
from typing import Dict, List, Optional, Set, Union
@@ -428,3 +429,23 @@ async def get_hosted_file_obj(
return mk_resp(data=hosted_file_dict, response=response)
#return mk_resp(data=hosted_file_obj)
# ### END ### API Hosted File ### get_hosted_file_obj() ###
# ### BEGIN ### API Hosted File ### download_tmp() ###
# Updated 2021-11-23
@router.get('/download/{filename}', response_model=Resp_Body_Base)
async def download_tmp(
filename: str = Query(..., min_length=4, max_length=100),
# x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
full_dest_path = 'admin/temp/order_line/order_line_list_2021-11-23_1310.xlsx'
filename = 'text.xlsx'
return FileResponse(full_dest_path, filename=filename)
# ### END ### API Hosted File ### download_tmp() ###

View File

@@ -32,6 +32,7 @@ async def get_obj_id_order_line_list(
limit: int = 50,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
create_export: Optional[bool] = False,
x_account_id: str = Header(..., min_length=11, max_length=22),
response: Response = Response,
):
@@ -57,6 +58,7 @@ async def get_obj_id_order_line_list(
limit = limit,
):
order_line_result_list = []
data_dict_list_for_export = []
for order_line_rec in order_line_rec_list_result:
if not full_detail:
if load_order_line_result := load_order_line_obj(
@@ -74,8 +76,11 @@ async def get_obj_id_order_line_list(
order_line_rec = order_line_rec,
by_alias = by_alias,
exclude_unset = exclude_unset,
# model_as_dict = model_as_dict,
model_as_dict = False,
):
if create_export:
data_dict = load_order_line_result.dict(by_alias=by_alias, exclude_unset=exclude_unset)
data_dict_list_for_export.append(data_dict)
order_line_result_list.append(load_order_line_result)
else:
order_line_result_list.append(None)
@@ -87,5 +92,26 @@ async def get_obj_id_order_line_list(
log.warning('Likely bad request')
return mk_resp(data=False, status_code=400, response=response) # Bad Request
if create_export:
datetime_format='%Y-%m-%d_%H%M'
current_datetime = datetime.datetime.now()
# current_datetime_string = current_datetime.isoformat()
current_datetime_string = current_datetime.strftime(datetime_format)
filename = f'order_line_list_{current_datetime_string}'
create_export_file(data_dict_list=data_dict_list_for_export, subdir_path='order_line', filename=filename, export_type='Excel')
# print(response_data)
# full_file_path = 'admin/temp/export_test_1.csv'
# full_file_path_xlsx = 'admin/temp/export_test_1.xlsx'
# keys = response_data[0].keys()
# print(keys)
# order_line_obj_dataframe = pandas.DataFrame(response_data)
# print(order_line_obj_dataframe)
# order_line_obj_dataframe.to_csv(full_file_path, columns=keys, index=False)
# order_line_obj_dataframe.to_excel(full_file_path_xlsx, columns=keys, index=False) # sheet_name='Sheet_name_1'
return mk_resp(data=response_data)
# ### END ### API Order Line ### get_obj_id_order_line_list() ###