From 0e13cda9e5b9e8e37d17abb68578ab0249b899b1 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 25 Nov 2021 10:04:21 +0000 Subject: [PATCH 1/3] Split out unit and monetary columns This is so we can treat them slightly differently later on. --- app/main/views/organisations.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/main/views/organisations.py b/app/main/views/organisations.py index c54b9444a..aaa6e1bea 100644 --- a/app/main/views/organisations.py +++ b/app/main/views/organisations.py @@ -175,15 +175,22 @@ def download_organisation_usage_report(org_id): financial_year=selected_year )['services'] - column_names = OrderedDict([ + unit_column_names = OrderedDict([ ('service_id', 'Service ID'), ('service_name', 'Service Name'), ('emails_sent', 'Emails sent'), ('sms_remainder', 'Free text message allowance remaining'), + ]) + + monetary_column_names = OrderedDict([ ('sms_cost', 'Spent on text messages (£)'), ('letter_cost', 'Spent on letters (£)') ]) + column_names = OrderedDict( + list(unit_column_names.items()) + list(monetary_column_names.items()) + ) + org_usage_data = [[x for x in column_names.values()]] for service in services_usage: From 0eb967bb7c023bdca5675c54904434e7ac78067e Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 25 Nov 2021 10:07:53 +0000 Subject: [PATCH 2/3] Refactor into list expression This is maybe a bit of personal preference but generally I find list expressions a bit more Pythonic that `append`-ing. --- app/main/views/organisations.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/main/views/organisations.py b/app/main/views/organisations.py index aaa6e1bea..f29f47e04 100644 --- a/app/main/views/organisations.py +++ b/app/main/views/organisations.py @@ -191,10 +191,14 @@ def download_organisation_usage_report(org_id): list(unit_column_names.items()) + list(monetary_column_names.items()) ) - org_usage_data = [[x for x in column_names.values()]] - - for service in services_usage: - org_usage_data.append([service[attribute] for attribute in column_names.keys()]) + org_usage_data = [ + list(column_names.values()) + ] + [ + [ + service[attribute] for attribute in column_names.keys() + ] + for service in services_usage + ] return Spreadsheet.from_rows(org_usage_data).as_csv_data, 200, { 'Content-Type': 'text/csv; charset=utf-8', From 5c33fbd48af567fa5c38e5b0976fab94960d96f3 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 25 Nov 2021 10:15:50 +0000 Subject: [PATCH 3/3] Format monetary values to two decimal places MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This means that the data in the report will match what’s on the page, where the values are rounded to the nearest penny. This uses the same string formatting to round the numbers which the `big_number` component does, so it should round the numbers in the same way. --- app/main/views/organisations.py | 10 ++++------ .../main/views/organisations/test_organisations.py | 12 ++++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/app/main/views/organisations.py b/app/main/views/organisations.py index f29f47e04..21bba27af 100644 --- a/app/main/views/organisations.py +++ b/app/main/views/organisations.py @@ -187,15 +187,13 @@ def download_organisation_usage_report(org_id): ('letter_cost', 'Spent on letters (£)') ]) - column_names = OrderedDict( - list(unit_column_names.items()) + list(monetary_column_names.items()) - ) - org_usage_data = [ - list(column_names.values()) + list(unit_column_names.values()) + list(monetary_column_names.values()) ] + [ [ - service[attribute] for attribute in column_names.keys() + service[attribute] for attribute in unit_column_names.keys() + ] + [ + '{:,.2f}'.format(service[attribute]) for attribute in monetary_column_names.keys() ] for service in services_usage ] diff --git a/tests/app/main/views/organisations/test_organisations.py b/tests/app/main/views/organisations/test_organisations.py index 4cb5e1c9e..bbef28c9b 100644 --- a/tests/app/main/views/organisations/test_organisations.py +++ b/tests/app/main/views/organisations/test_organisations.py @@ -692,9 +692,9 @@ def test_download_organisation_usage_report( 'chargeable_billable_sms': 22, 'emails_sent': 13000, 'free_sms_limit': 100, - 'letter_cost': 30.50, + 'letter_cost': 30.5, 'sms_billable_units': 122, - 'sms_cost': 1.93, + 'sms_cost': 1.934, 'sms_remainder': None }, { @@ -703,9 +703,9 @@ def test_download_organisation_usage_report( 'chargeable_billable_sms': 222, 'emails_sent': 23000, 'free_sms_limit': 250000, - 'letter_cost': 60.50, + 'letter_cost': 60.5, 'sms_billable_units': 322, - 'sms_cost': 3.93, + 'sms_cost': 3.935, 'sms_remainder': None }, ]} @@ -721,8 +721,8 @@ def test_download_organisation_usage_report( assert csv_report.string == ( "Service ID,Service Name,Emails sent,Free text message allowance remaining," "Spent on text messages (£),Spent on letters (£)" - "\r\n596364a0-858e-42c8-9062-a8fe822260eb,Service 1,13000,,1.93,30.5" - "\r\n147ad62a-2951-4fa1-9ca0-093cd1a52c52,Service 1,23000,,3.93,60.5\r\n" + "\r\n596364a0-858e-42c8-9062-a8fe822260eb,Service 1,13000,,1.93,30.50" + "\r\n147ad62a-2951-4fa1-9ca0-093cd1a52c52,Service 1,23000,,3.94,60.50\r\n" )