mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-05-30 19:10:42 -04:00
Make message and recipient counters formatters
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.
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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 %}
|
||||
|
||||
Reference in New Issue
Block a user