From ee3ae12735cc8bde2293dc68bcc6deb37217985f Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Tue, 4 Jan 2022 13:43:30 -0500 Subject: [PATCH] Work on order reports and related --- app/lib_general.py | 16 ++++++++++++++-- app/routers/order.py | 2 +- app/routers/order_line.py | 13 ++++++++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/app/lib_general.py b/app/lib_general.py index abc774f..9e95d00 100644 --- a/app/lib_general.py +++ b/app/lib_general.py @@ -129,9 +129,11 @@ def create_export_file( data_dict_list: list, subdir_path: str, filename: str, + column_name_li: list = [], + rm_id: bool = True, export_type: str = 'CSV', # CSV, Excel ) -> bool|str: - log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) hosted_tmp_path = settings.PATH_HOSTED_TMP_ROOT @@ -144,9 +146,19 @@ def create_export_file( 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() + if column_name_li: pass + else: + column_name_li = list(data_dict_list[0].keys()) log.debug(column_name_li) + if rm_id: + for column_name in list(column_name_li): + # log.info(f'Checking column name: {column_name}') + if column_name.endswith('_id'): + column_name_li.remove(column_name) + log.info(f'Removing column name: {column_name}') + log.debug(column_name_li) + data_dataframe = pandas.DataFrame(data_dict_list) log.debug(data_dataframe) diff --git a/app/routers/order.py b/app/routers/order.py index c3145a5..eb96497 100644 --- a/app/routers/order.py +++ b/app/routers/order.py @@ -273,7 +273,7 @@ async def get_obj_id_order_list( log.warning('Likely bad request') return mk_resp(data=False, status_code=400, response=response) # Bad Request - return mk_resp(data=response_data) + return mk_resp(data=response_data, response=response) # ### END ### API Order ### get_obj_id_order_list() ### diff --git a/app/routers/order_line.py b/app/routers/order_line.py index 60482b3..9009fb2 100644 --- a/app/routers/order_line.py +++ b/app/routers/order_line.py @@ -85,7 +85,7 @@ async def get_obj_id_order_line_list( else: order_line_result_list.append(None) response_data = order_line_result_list - elif order_line_rec_list_result is None: + elif isinstance(order_line_rec_list_result, list) or order_line_rec_list_result is None: # Empty list or None log.info('No results') return mk_resp(data=None, status_code=404, response=response) # Not Found else: @@ -93,17 +93,24 @@ async def get_obj_id_order_line_list( return mk_resp(data=False, status_code=400, response=response) # Bad Request if create_export: + # column_name_li = ['order_id_random', 'order_line_id_random', '', 'product_name', 'quantity', 'amount', 'dollar_amount', 'person_email'] + + column_name_li = ['order_line_id_random', 'order_id_random', 'product_id_random', 'product_type', 'product_name', 'product_unit_price', 'product_recurring', 'curr_product_id_random', 'curr_product_for_type', 'curr_product_type', 'curr_product_type_name', 'curr_product_name', 'name', 'quantity', 'amount', 'dollar_amount', 'recurring', 'message', 'person_id_random', 'person_given_name', 'person_family_name', 'person_display_name', 'person_full_name', 'person_contact_email', 'person_contact_cc_email', 'person_contact_phone_mobile', 'person_contact_phone_home', 'person_contact_phone_office', 'person_contact_phone_land', 'person_contact_phone_fax', 'person_contact_phone_other', 'person_contact_address_name', 'person_contact_address_organization_name', 'person_contact_address_line_1', 'person_contact_address_line_2', 'person_contact_address_line_3', 'person_contact_address_city', 'person_contact_address_country_subdivision_code', 'person_contact_address_state_province', 'person_contact_address_postal_code', 'person_contact_address_country_alpha_2_code', 'person_contact_address_country_name', 'person_contact_address_country', 'order_status', 'order_created_on', 'order_updated_on', 'created_on', 'updated_on'] + + + # column_name_li = [] datetime_format='%Y-%m-%d_%H%M' # current_datetime = datetime.datetime.now() # Servers timezone (Eastern) current_datetime_utc = datetime.datetime.utcnow() current_datetime_utc = current_datetime_utc.strftime(datetime_format) filename = f'order_line_list_{current_datetime_utc}' - if result := 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, column_name_li=column_name_li, 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 + else: tmp_file_path = None - return mk_resp(data=response_data, tmp_file_path=tmp_file_path) + return mk_resp(data=response_data, tmp_file_path=tmp_file_path, response=response) # ### END ### API Order Line ### get_obj_id_order_line_list() ###