mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-03-26 21:22:32 -04:00
Add filter for formatting a number as currency
This is used on the usage page, but is likely to become useful in other places now that letter rates can be greater than £1.
This commit is contained in:
@@ -521,6 +521,13 @@ def nl2br(value):
|
||||
return ''
|
||||
|
||||
|
||||
def format_number_in_pounds_as_currency(number):
|
||||
if number >= 1:
|
||||
return f"£{number:,.2f}"
|
||||
|
||||
return f"{number * 100:.0f}p"
|
||||
|
||||
|
||||
@login_manager.user_loader
|
||||
def load_user(user_id):
|
||||
return User.from_id(user_id)
|
||||
@@ -798,6 +805,7 @@ def add_template_filters(application):
|
||||
format_notification_status_as_time,
|
||||
format_notification_status_as_field_status,
|
||||
format_notification_status_as_url,
|
||||
format_number_in_pounds_as_currency,
|
||||
formatters.formatted_list,
|
||||
nl2br,
|
||||
format_phone_number_human_readable,
|
||||
|
||||
@@ -456,7 +456,7 @@ def format_letter_details_for_month(letter_units_for_month):
|
||||
|
||||
letter_details = LetterDetails(
|
||||
billing_units=sum(x['billing_units'] for x in rate_group),
|
||||
rate=format_letter_rate(rate_group[0]['rate']),
|
||||
rate=rate_group[0]['rate'],
|
||||
cost=(sum(x['billing_units'] for x in rate_group) * rate_group[0]['rate']),
|
||||
postage_description=rate_group[0]['postage']
|
||||
)
|
||||
@@ -472,13 +472,6 @@ def get_postage_description(postage):
|
||||
return 'international'
|
||||
|
||||
|
||||
def format_letter_rate(number):
|
||||
if number >= 1:
|
||||
return f"£{number:,.2f}"
|
||||
|
||||
return f"{number * 100:.0f}p"
|
||||
|
||||
|
||||
def get_free_paid_breakdown_for_month(
|
||||
free_sms_fragment_limit,
|
||||
cumulative,
|
||||
|
||||
@@ -110,7 +110,7 @@
|
||||
{% for letter in month.letters%}
|
||||
{% if letter.billing_units %}
|
||||
<li class="tabular-numbers">{{ "{:,} {}".format(letter.billing_units, letter.postage_description) }} {{ message_count_label(letter.billing_units, 'letter', '') }}at
|
||||
{{ letter.rate }}</li>
|
||||
{{ letter.rate | format_number_in_pounds_as_currency }}</li>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% if not (month.free or month.paid or month.letters) %}
|
||||
|
||||
@@ -3,7 +3,10 @@ from functools import partial
|
||||
import pytest
|
||||
from flask import url_for
|
||||
|
||||
from app import format_notification_status_as_url
|
||||
from app import (
|
||||
format_notification_status_as_url,
|
||||
format_number_in_pounds_as_currency,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('status, notification_type, expected', (
|
||||
@@ -33,3 +36,19 @@ def test_format_notification_status_as_url(
|
||||
assert format_notification_status_as_url(
|
||||
status, notification_type
|
||||
) == expected()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('input_number, formatted_number', [
|
||||
(0, '0p'),
|
||||
(0.01, '1p'),
|
||||
(0.5, '50p'),
|
||||
(1, '£1.00'),
|
||||
(1.01, '£1.01'),
|
||||
(1.006, '£1.01'),
|
||||
(5.25, '£5.25'),
|
||||
(5.7, '£5.70'),
|
||||
(381, '£381.00'),
|
||||
(144820, '£144,820.00'),
|
||||
])
|
||||
def test_format_number_in_pounds_as_currency(input_number, formatted_number):
|
||||
assert format_number_in_pounds_as_currency(input_number) == formatted_number
|
||||
|
||||
Reference in New Issue
Block a user