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

@@ -1,5 +1,5 @@
from __future__ import annotations
import datetime, jwt, pytz, redis, time
import datetime, jwt, os, pandas, pathlib, pytz, redis, time
from passlib.hash import argon2
from fastapi import APIRouter, Depends, Header, HTTPException, Response, status
@@ -111,3 +111,48 @@ def decode_jwt(
except:
return None
# ### END ### API Lib General ### decode_jwt() ###
# ### BEGIN ### API Lib General ### create_export() ###
# Updated 2021-07-14
@logger_reset
def create_export_file(
data_dict_list: list,
subdir_path: str,
filename: str,
export_type: str = 'CSV', # CSV, Excel
) -> bool:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
hosted_temp_path = 'admin/temp'
log.info(f'Hosted Temp Path: {hosted_temp_path}')
subdirectory_dest = os.path.join(hosted_temp_path, subdir_path)
log.debug(subdirectory_dest)
pathlib.Path(subdirectory_dest).mkdir(parents=True, exist_ok=True)
file_dest_w_subdir = os.path.join(subdirectory_dest, filename)
log.info(f'File Dest With Subdir: {file_dest_w_subdir}')
column_name_li = data_dict_list[0].keys()
log.debug(column_name_li)
data_dataframe = pandas.DataFrame(data_dict_list)
log.debug(data_dataframe)
try:
if export_type == 'CSV':
log.info('Saving dataframe to CSV file')
full_dest_path = file_dest_w_subdir+'.csv'
data_dataframe.to_csv(full_dest_path, columns=column_name_li, index=False)
elif export_type == 'Excel':
log.info('Saving dataframe to Excel file')
full_dest_path = file_dest_w_subdir+'.xlsx'
data_dataframe.to_excel(full_dest_path, columns=column_name_li, index=False) # sheet_name='Sheet_name_1'
except:
log.exception('Something went wrong while trying to save the export file.')
return False
return True
# ### END ### API Lib General ### create_export() ###