Work on temporary hosted files
This commit is contained in:
@@ -122,8 +122,8 @@ def create_export_file(
|
|||||||
subdir_path: str,
|
subdir_path: str,
|
||||||
filename: str,
|
filename: str,
|
||||||
export_type: str = 'CSV', # CSV, Excel
|
export_type: str = 'CSV', # CSV, Excel
|
||||||
) -> bool:
|
) -> bool|str:
|
||||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
hosted_tmp_path = settings.PATH_HOSTED_TMP_ROOT
|
hosted_tmp_path = settings.PATH_HOSTED_TMP_ROOT
|
||||||
@@ -146,14 +146,20 @@ def create_export_file(
|
|||||||
if export_type == 'CSV':
|
if export_type == 'CSV':
|
||||||
log.info('Saving dataframe to CSV file')
|
log.info('Saving dataframe to CSV file')
|
||||||
full_dest_path = file_dest_w_subdir+'.csv'
|
full_dest_path = file_dest_w_subdir+'.csv'
|
||||||
|
filename_w_ext = filename+'.csv'
|
||||||
|
tmp_file_path = os.path.join(subdir_path,filename_w_ext)
|
||||||
data_dataframe.to_csv(full_dest_path, columns=column_name_li, index=False)
|
data_dataframe.to_csv(full_dest_path, columns=column_name_li, index=False)
|
||||||
elif export_type == 'Excel':
|
elif export_type == 'Excel':
|
||||||
log.info('Saving dataframe to Excel file')
|
log.info('Saving dataframe to Excel file')
|
||||||
full_dest_path = file_dest_w_subdir+'.xlsx'
|
full_dest_path = file_dest_w_subdir+'.xlsx'
|
||||||
|
filename_w_ext = filename+'.xlsx'
|
||||||
|
tmp_file_path = os.path.join(subdir_path,filename_w_ext)
|
||||||
data_dataframe.to_excel(full_dest_path, columns=column_name_li, index=False) # sheet_name='Sheet_name_1'
|
data_dataframe.to_excel(full_dest_path, columns=column_name_li, index=False) # sheet_name='Sheet_name_1'
|
||||||
except:
|
except:
|
||||||
log.exception('Something went wrong while trying to save the export file.')
|
log.exception('Something went wrong while trying to save the export file.')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
log.info(f'Temp File Path: {tmp_file_path}')
|
||||||
|
|
||||||
|
return tmp_file_path # True
|
||||||
# ### END ### API Lib General ### create_export() ###
|
# ### END ### API Lib General ### create_export() ###
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ class Resp_Body_Base(BaseModel):
|
|||||||
# Update 2021-08-23
|
# Update 2021-08-23
|
||||||
def mk_resp(
|
def mk_resp(
|
||||||
data: None|bool|dict|list,
|
data: None|bool|dict|list,
|
||||||
|
tmp_file_path: None|str = None,
|
||||||
dict_to_json: bool = False,
|
dict_to_json: bool = False,
|
||||||
status_code: int = 200,
|
status_code: int = 200,
|
||||||
status_message: str = '',
|
status_message: str = '',
|
||||||
@@ -71,6 +72,7 @@ def mk_resp(
|
|||||||
resp_body['meta']['status_message'] = settings.HTTP_STATUS_LI[status_code]['message']
|
resp_body['meta']['status_message'] = settings.HTTP_STATUS_LI[status_code]['message']
|
||||||
resp_body['meta']['status_name'] = settings.HTTP_STATUS_LI[status_code]['name']
|
resp_body['meta']['status_name'] = settings.HTTP_STATUS_LI[status_code]['name']
|
||||||
resp_body['meta']['success'] = success
|
resp_body['meta']['success'] = success
|
||||||
|
resp_body['meta']['tmp_file_path'] = tmp_file_path
|
||||||
|
|
||||||
if isinstance(data, bool):
|
if isinstance(data, bool):
|
||||||
resp_body['meta']['data_type'] = 'bool'
|
resp_body['meta']['data_type'] = 'bool'
|
||||||
|
|||||||
@@ -433,7 +433,7 @@ async def get_hosted_file_obj(
|
|||||||
|
|
||||||
# ### BEGIN ### API Hosted File ### download_tmp() ###
|
# ### BEGIN ### API Hosted File ### download_tmp() ###
|
||||||
# Updated 2021-11-23
|
# Updated 2021-11-23
|
||||||
@router.get('/download/{filename}', response_model=Resp_Body_Base)
|
@router.get('/download/tmp/{filename}', response_model=Resp_Body_Base)
|
||||||
async def download_tmp(
|
async def download_tmp(
|
||||||
filename: str = Query(..., min_length=4, max_length=100),
|
filename: str = Query(..., min_length=4, max_length=100),
|
||||||
# x_account_id: str = Header(...),
|
# x_account_id: str = Header(...),
|
||||||
|
|||||||
@@ -99,19 +99,11 @@ async def get_obj_id_order_line_list(
|
|||||||
current_datetime_utc = datetime.datetime.utcnow()
|
current_datetime_utc = datetime.datetime.utcnow()
|
||||||
current_datetime_utc = current_datetime_utc.strftime(datetime_format)
|
current_datetime_utc = current_datetime_utc.strftime(datetime_format)
|
||||||
filename = f'order_line_list_{current_datetime_utc}'
|
filename = f'order_line_list_{current_datetime_utc}'
|
||||||
create_export_file(data_dict_list=data_dict_list_for_export, subdir_path='order_line', filename=filename, export_type='Excel')
|
if result := create_export_file(data_dict_list=data_dict_list_for_export, subdir_path='order_line', filename=filename, export_type='Excel'):
|
||||||
|
tmp_file_path = result
|
||||||
|
else:
|
||||||
|
log.error('Something went wrong while creating or saving the export file')
|
||||||
|
tmp_file_path = result
|
||||||
|
|
||||||
# print(response_data)
|
return mk_resp(data=response_data, tmp_file_path=tmp_file_path)
|
||||||
|
|
||||||
# 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() ###
|
# ### END ### API Order Line ### get_obj_id_order_line_list() ###
|
||||||
|
|||||||
Reference in New Issue
Block a user