From 462294c9d1dea7f6f73fba30684c255c356033ca Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 6 Jan 2021 12:59:48 +0000 Subject: [PATCH] Make message and recipient counters formatters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As formatters we can use them in Jinja or Python code. It also means we don’t need to import them every time we want to use them – they’re always available in the template context. For now this doesn’t remove the macros, it just aliases them to the formatters. This gives us confidence that the formatters are working the same way the old macros did, and reduces the diff size of each commit. --- app/__init__.py | 12 +++ app/formatters.py | 88 +++++++++++++++++++ .../components/message-count-label.html | 72 +-------------- 3 files changed, 104 insertions(+), 68 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index d0bb4a387..1e9ce53e5 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -62,8 +62,14 @@ from app.formatters import ( format_thousands, format_time, id_safe, + iteration_count, linkable_name, + message_count, + message_count_label, + message_count_noun, nl2br, + recipient_count, + recipient_count_label, valid_phone_number, ) from app.models.organisation import Organisation @@ -548,6 +554,12 @@ def add_template_filters(application): id_safe, convert_to_boolean, format_list_items, + iteration_count, + recipient_count, + recipient_count_label, + message_count_label, + message_count, + message_count_noun, ]: application.add_template_filter(fn) diff --git a/app/formatters.py b/app/formatters.py index bf36351c8..9e7f0de33 100644 --- a/app/formatters.py +++ b/app/formatters.py @@ -415,3 +415,91 @@ def guess_name_from_email_address(email_address): ).then( normalize_spaces ) + + +def message_count_label(count, template_type, suffix='sent'): + if suffix: + return f'{message_count_noun(count, template_type)} {suffix}' + return message_count_noun(count, template_type) + + +def message_count_noun(count, template_type): + if template_type is None: + if count == 1: + return 'message' + else: + return 'messages' + + if template_type == 'sms': + if count == 1: + return 'text message' + else: + return 'text messages' + + elif template_type == 'email': + if count == 1: + return 'email' + else: + return 'emails' + + elif template_type == 'letter': + if count == 1: + return 'letter' + else: + return 'letters' + + elif template_type == 'broadcast': + if count == 1: + return 'broadcast' + else: + return 'broadcasts' + + +def message_count(count, template_type): + return ( + f'{format_thousands(count)} ' + f'{message_count_noun(count, template_type)}' + ) + + +def recipient_count_label(count, template_type): + + if template_type is None: + if count == 1: + return 'recipient' + else: + return 'recipients' + + if template_type == 'sms': + if count == 1: + return 'phone number' + else: + return 'phone numbers' + + elif template_type == 'email': + if count == 1: + return 'email address' + else: + return 'email addresses' + + elif template_type == 'letter': + if count == 1: + return 'address' + else: + return 'addresses' + + +def recipient_count(count, template_type): + return ( + f'{format_thousands(count)} ' + f'{recipient_count_label(count, template_type)}' + ) + + +def iteration_count(count): + if count == 1: + return 'once' + elif count == 2: + return 'twice' + else: + return f'{count} times' diff --git a/app/templates/components/message-count-label.html b/app/templates/components/message-count-label.html index b85d8f420..87b294d94 100644 --- a/app/templates/components/message-count-label.html +++ b/app/templates/components/message-count-label.html @@ -1,79 +1,15 @@ {% macro message_count_label(count, template_type, suffix='sent') -%} - {% if template_type == None %} - {%- if count == 1 -%} - message - {%- else -%} - messages - {%- endif -%} - {% endif %} - {%- if template_type == 'sms' -%} - {%- if count == 1 -%} - text message - {%- else -%} - text messages - {%- endif -%} - {%- elif template_type == 'email' -%} - {%- if count == 1 -%} - email - {%- else -%} - emails - {%- endif -%} - {%- elif template_type == 'letter' -%} - {%- if count == 1 -%} - letter - {%- else -%} - letters - {%- endif -%} - {%- elif template_type == 'broadcast' -%} - {%- if count == 1 -%} - broadcast - {%- else -%} - broadcasts - {%- endif -%} - {%- endif %} {{ suffix }} + {{ count|message_count_noun(template_type) }} {{ suffix }} {%- endmacro %} {% macro recipient_count_label(count, template_type) -%} - {% if template_type == None %} - {%- if count == 1 -%} - recipient - {%- else -%} - recipients - {%- endif -%} - {% endif %} - {%- if template_type == 'sms' -%} - {%- if count == 1 -%} - phone number - {%- else -%} - phone numbers - {%- endif -%} - {%- elif template_type == 'email' -%} - {%- if count == 1 -%} - email address - {%- else -%} - email addresses - {%- endif -%} - {%- elif template_type == 'letter' -%} - {%- if count == 1 -%} - address - {%- else -%} - addresses - {%- endif -%} - {%- endif %} + {{ count|recipient_count_label(template_type) }} {%- endmacro %} - {% macro recipient_count(count, template_type, prefix='') -%} - {{ count|format_thousands }} {{ prefix }} {{ recipient_count_label(count, template_type)}} + {{ count|format_thousands }} {{ prefix }} {{ count|recipient_count_label(template_type) }} {% endmacro %} - {% macro iteration_count(count) -%} - {% if count == 1 %} - once - {% elif count == 2 %} - twice - {% else %} - {{ count }} times - {% endif %} + {{ count|iteration_count }} {% endmacro %}