From 41bdca8a50fa2f2ba6d6c1e8447c352583a63e28 Mon Sep 17 00:00:00 2001 From: felixaldam-gates <44896994+Felixim0@users.noreply.github.com> Date: Mon, 11 Nov 2024 17:59:50 +0000 Subject: [PATCH] Refactor download utilties into seperate functions has allowed for functions deffined within themselves to be removed and replaced with the contents of the functions, making the code much clearer. Also removed YAPF formatting for some lines --- .../page_multiple_address_original.py | 2 +- ...submit_multiple_match_from_singlesearch.py | 61 +++++++------------ .../page_error_annotation_multiple.py | 13 ++-- 3 files changed, 30 insertions(+), 46 deletions(-) diff --git a/src/aims_ui/page_controllers/b_multiple_matches/page_multiple_address_original.py b/src/aims_ui/page_controllers/b_multiple_matches/page_multiple_address_original.py index ff98d25b..94855147 100644 --- a/src/aims_ui/page_controllers/b_multiple_matches/page_multiple_address_original.py +++ b/src/aims_ui/page_controllers/b_multiple_matches/page_multiple_address_original.py @@ -1,3 +1,4 @@ + from flask import render_template, request, send_file, session from flask_login import login_required @@ -105,7 +106,6 @@ def multiple_address_original(): e.error_description) if not file_valid: - print('FILE IS NOT VALID') return page_error_annotation_multiple(page_name, all_user_input, error_description) diff --git a/src/aims_ui/page_controllers/b_multiple_matches/utils/submit_multiple_match_from_singlesearch.py b/src/aims_ui/page_controllers/b_multiple_matches/utils/submit_multiple_match_from_singlesearch.py index 933ace7e..1858b2a2 100644 --- a/src/aims_ui/page_controllers/b_multiple_matches/utils/submit_multiple_match_from_singlesearch.py +++ b/src/aims_ui/page_controllers/b_multiple_matches/utils/submit_multiple_match_from_singlesearch.py @@ -38,32 +38,6 @@ def write(id, addr, m_addr, address_type, uprn, m_type, confid_score, ] }) # yapf: disable - def finalize(line_count, no_addresses_searched, single_match_total, - multiple_match_total, no_match_total): - headers = [ - 'Number of addresses searched:', 'Single matches:', 'Multiple Matches', - 'No Match:' - ] - answers = [ - no_addresses_searched, single_match_total, multiple_match_total, - no_match_total - ] - results_summary_table_trs = [{ - 'tds': [{ - 'value': headers[x] - }, { - 'value': answers[x] - }] - } for x in range(0, len(headers))] - - return { - 'ths': ths, - 'trs': trs - }, { - 'ths': [], - 'trs': results_summary_table_trs - }, - def get_match_type(n_addr): return '
M
' if n_addr > 1 else 'S
' @@ -119,8 +93,21 @@ def get_match_type(n_addr): adrs.airRating.value, ) - return finalize(line_count, no_addresses_searched, single_match_total, - multiple_match_total, no_match_total) + headers = [ + 'Number of addresses searched:', 'Single matches:', 'Multiple Matches', + 'No Match:' + ] + answers = [ + no_addresses_searched, single_match_total, multiple_match_total, + no_match_total + ] + + + results_summary_table_trs = [{ + 'tds': [{'value': headers[x]}, {'value': answers[x] }] + } for x in range(0, len(headers))] # yapf: disable + + return {'ths': ths,'trs': trs }, { 'ths': [], 'trs': results_summary_table_trs} # yapf: disable def multiple_address_match_from_singlesearch_download(file, all_user_input): @@ -150,15 +137,6 @@ def write(id, addr, m_addr, address_type, uprn, m_type, confid_score, ai_rating, ]) - def finalize(line_count, no_addresses_searched, single_match_total, - multiple_match_total, no_match_total): - # Creating the byteIO object from the StringIO Object - mem = BytesIO() - mem.write(proxy.getvalue().encode()) - mem.seek(0) - proxy.close() - return mem, line_count - def get_match_type(n_addr): return 'M' if n_addr > 1 else 'S' @@ -214,5 +192,10 @@ def get_match_type(n_addr): adrs.airRating.value, ) - return finalize(line_count, no_addresses_searched, single_match_total, - multiple_match_total, no_match_total) + # Creating the byteIO object from the StringIO Object + mem = BytesIO() + mem.write(proxy.getvalue().encode()) + mem.seek(0) + proxy.close() + + return mem, line_count diff --git a/src/aims_ui/page_controllers/f_error_pages/page_error_annotation_multiple.py b/src/aims_ui/page_controllers/f_error_pages/page_error_annotation_multiple.py index 42bed60b..22df35c7 100644 --- a/src/aims_ui/page_controllers/f_error_pages/page_error_annotation_multiple.py +++ b/src/aims_ui/page_controllers/f_error_pages/page_error_annotation_multiple.py @@ -6,7 +6,6 @@ from aims_ui.models.get_fields import get_fields from aims_ui.page_helpers.google_utils import get_current_group from aims_ui.page_helpers.pages_location_utils import get_page_location - """ Manage errors specific to multiple match pages """ @@ -20,7 +19,8 @@ def page_error_annotation_multiple( logging.error('Error message: {}'.format(primary_error_message)) # Convert the primary error message to a string if it's an exception - primary_error_message = convert_exception_to_error_message(primary_error_message) + primary_error_message = convert_exception_to_error_message( + primary_error_message) endpoints = get_endpoints(called_from=page_name_with_error) page_location = get_page_location(endpoints, page_name_with_error) @@ -54,27 +54,28 @@ def page_error_annotation_multiple( bulk_limits=bulk_limits, ) + def convert_exception_to_error_message(primary_error_message): """ Convert an exception to a string """ # If the primary error message is an instance of an Exception if isinstance(primary_error_message, Exception): primary_error_message = str(primary_error_message) - + if 'Expecting value: line 2 column 1' in primary_error_message: # Error message when there's a connection error to the API return 'Connection error to the API' # TODO add here other error message from submission of multiple_address_match_from_singlesearch return primary_error_message - + def match_api_error_message_to_name_of_field(primary_error_message): """ Given an error message, return the name of the field that caused the error """ default_element_for_error_message = 'file_upload' - + # If the primary error message is a string, decide which element to return based on the error message if 'Record Limit Exceeded' in primary_error_message: return 'file_upload' - return default_element_for_error_message + return default_element_for_error_message