Make one method for comma-formatting numbers

We were doing this a few different ways in different places.
This commit is contained in:
Chris Hill-Scott
2019-07-08 14:46:34 +01:00
parent 6d5f542a88
commit 959dd6ac38
4 changed files with 18 additions and 14 deletions
+7 -2
View File
@@ -3,6 +3,7 @@ import os
import urllib import urllib
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from functools import partial from functools import partial
from numbers import Number
from time import monotonic from time import monotonic
import ago import ago
@@ -345,8 +346,12 @@ def format_delta(date):
) )
def format_thousands(number): def format_thousands(value):
return "{:,.0f}".format(number) if isinstance(value, Number):
return '{:,.0f}'.format(value)
if value is None:
return ''
return value
def valid_phone_number(phone_number): def valid_phone_number(phone_number):
+4 -3
View File
@@ -34,6 +34,7 @@ from wtforms.fields.html5 import EmailField, SearchField, TelField
from wtforms.validators import URL, DataRequired, Length, Optional, Regexp from wtforms.validators import URL, DataRequired, Length, Optional, Regexp
from wtforms.widgets import CheckboxInput, ListWidget from wtforms.widgets import CheckboxInput, ListWidget
from app import format_thousands
from app.main.validators import ( from app.main.validators import (
Blacklist, Blacklist,
CsvFileValidator, CsvFileValidator,
@@ -209,9 +210,9 @@ class ForgivingIntegerField(StringField):
error = None error = None
try: try:
if int(self.data) > self.POSTGRES_MAX_INT: if int(self.data) > self.POSTGRES_MAX_INT:
error = 'Number of {} must be {:,.0f} or less'.format( error = 'Number of {} must be {} or less'.format(
self.things, self.things,
self.POSTGRES_MAX_INT, format_thousands(self.POSTGRES_MAX_INT),
) )
except ValueError: except ValueError:
error = 'Enter the number of {} {}'.format( error = 'Enter the number of {} {}'.format(
@@ -234,7 +235,7 @@ class ForgivingIntegerField(StringField):
try: try:
value = int(self.data) value = int(self.data)
value = '{:,.0f}'.format(value) value = format_thousands(value)
except (ValueError, TypeError): except (ValueError, TypeError):
value = self.data if self.data is not None else '' value = self.data if self.data is not None else ''
+3 -2
View File
@@ -25,6 +25,7 @@ from notifications_utils.template import Template, WithSubjectTemplate
from app import ( from app import (
current_service, current_service,
format_datetime_short, format_datetime_short,
format_thousands,
job_api_client, job_api_client,
notification_api_client, notification_api_client,
service_api_client, service_api_client,
@@ -186,8 +187,8 @@ def cancel_letter_job(service_id, job_id):
except HTTPError as e: except HTTPError as e:
flash(e.message, 'dangerous') flash(e.message, 'dangerous')
return redirect(url_for('main.view_job', service_id=service_id, job_id=job_id)) return redirect(url_for('main.view_job', service_id=service_id, job_id=job_id))
flash("Cancelled {:,.0f} letters from {}".format( flash("Cancelled {} letters from {}".format(
number_of_letters, job['original_file_name'] format_thousands(number_of_letters), job['original_file_name']
), 'default_with_tick') ), 'default_with_tick')
return redirect(url_for('main.service_dashboard', service_id=service_id)) return redirect(url_for('main.service_dashboard', service_id=service_id))
+4 -7
View File
@@ -19,6 +19,7 @@ from app import (
billing_api_client, billing_api_client,
current_service, current_service,
email_branding_client, email_branding_client,
format_thousands,
inbound_number_client, inbound_number_client,
letter_branding_client, letter_branding_client,
notification_api_client, notification_api_client,
@@ -212,9 +213,9 @@ def submit_request_to_go_live(service_id):
service_dashboard=url_for('main.service_dashboard', service_id=current_service.id, _external=True), service_dashboard=url_for('main.service_dashboard', service_id=current_service.id, _external=True),
organisation_type=str(current_service.organisation_type).title(), organisation_type=str(current_service.organisation_type).title(),
agreement=current_service.organisation.as_human_readable(current_user.email_domain), agreement=current_service.organisation.as_human_readable(current_user.email_domain),
volume_email_formatted=format_if_number(current_service.volume_email), volume_email_formatted=format_thousands(current_service.volume_email),
volume_sms_formatted=format_if_number(current_service.volume_sms), volume_sms_formatted=format_thousands(current_service.volume_sms),
volume_letter_formatted=format_if_number(current_service.volume_letter), volume_letter_formatted=format_thousands(current_service.volume_letter),
research_consent='Yes' if current_service.consent_to_research else 'No', research_consent='Yes' if current_service.consent_to_research else 'No',
existing_live='Yes' if current_user.live_services else 'No', existing_live='Yes' if current_user.live_services else 'No',
email_address=current_user.email_address, email_address=current_user.email_address,
@@ -1121,7 +1122,3 @@ def check_contact_details_type(contact_details):
return 'email_address' return 'email_address'
else: else:
return 'phone_number' return 'phone_number'
def format_if_number(value):
return '{:,.0f}'.format(value) if isinstance(value, int) else ''