Skip to content

Commit

Permalink
Refactor download utilties into seperate functions has allowed for fu…
Browse files Browse the repository at this point in the history
…nctions 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
  • Loading branch information
Felixim0 committed Nov 11, 2024
1 parent 44d0044 commit 41bdca8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

from flask import render_template, request, send_file, session
from flask_login import login_required

Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<p style="background-color:orange;">M</p>' if n_addr > 1 else '<p style="background-color:Aquamarine;">S</p>'

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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'

Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 """


Expand All @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 41bdca8

Please sign in to comment.