app/formatters.py.
Date and Time Formatters
Date Formatting
format_date
Formats date in full format with day name.date(str): UTC datetime string
str - Full date with day name
Format: %A %d %B %Y (e.g., “Wednesday 25 December 2024”)
format_date_normal
Formats date without day name, no leading zeros.%d %B %Y with leading zeros stripped
format_date_short
Formats date in short format (day and month only).%d %B with leading zeros stripped
format_date_numeric
Formats date in ISO format (YYYY-MM-DD).%Y-%m-%d
format_date_human
Formats date as “today”, “yesterday”, “tomorrow”, or date.- Returns “tomorrow” for next day
- Returns “today” for current day
- Returns “yesterday” for previous day
- Returns short date otherwise
Time Formatting
format_time
Formats time in 12-hour format with special cases.- 12:00 AM → “midnight”
- 12:00 PM → “midday”
- Other times → 12-hour format (lowercase)
format_time_24h
Formats time in 24-hour format.%H:%M
Combined Date and Time
format_datetime
Combines full date and time.format_datetime_normal
Combines normal date and time.format_datetime_short
Combines short date and time.format_datetime_relative
Combines relative date and time.format_datetime_numeric
Combines numeric date and 24-hour time.format_datetime_human
Formats datetime with customizable relative date.date(str): UTC datetime stringdate_prefix(str): Prefix for non-relative dates (default: “on”)separator(str): Word between date and time (default: “at”)
Day of Week
format_day_of_week
Returns the day name.Relative Time
format_delta
Formats time elapsed since a date.- < 30 seconds: “just now”
- < 60 seconds: “in the last minute”
- Otherwise: humanized time (“1 minute ago”, “2 hours ago”, etc.)
format_delta_days
Formats days elapsed since a date.date(str): UTC datetime stringnumeric_prefix(str): Prefix for numeric output (default: "")
Notification Formatters
Notification Types
format_notification_type
Formats notification type for display.notification_type(str): “email”, “sms”, or “letter”
str - Display name
Notification Status
format_notification_status
Formats notification status for display based on type.status(str): Notification status codetemplate_type(str): “email”, “sms”, or “letter”
str - Human-readable status
Common Email Statuses:
delivered→ “Delivered”permanent-failure→ “Email address does not exist”temporary-failure→ “Inbox not accepting messages right now”technical-failure→ “Technical failure”sending/created→ “Delivering”
delivered→ “Delivered”sent→ “Sent to an international number”permanent-failure→ “Not delivered”temporary-failure→ “Phone not accepting messages right now”validation-failed→ “Validation failed”
virus-scan-failed→ “Virus detected”permanent-failure→ “Permanent failure”technical-failure→ “Technical failure”- Most others → "" (empty string)
format_notification_status_as_time
Returns appropriate time for status display.- For statuses “created”, “pending”, “sending”: Returns created time with ” since ” prefix
- For other statuses: Returns updated time
format_notification_status_as_field_status
Returns CSS class for status field styling.str | None - CSS class name or None
Values:
"error"- For failures"default"- For in-progress"sent-international"- For SMS sent internationallyNone- For successful/neutral statuses
format_notification_status_as_url
Returns guidance URL for failure statuses.str | None - URL to guidance page or None
Only returns URLs for:
- Email or SMS notification types
- Failure statuses (technical-failure, temporary-failure, permanent-failure)
Currency Formatters
format_pounds_as_currency
Formats pounds as currency.number(float): Amount in pounds
str - Formatted currency string
format_pennies_as_currency
Formats pennies as currency with optional long format.pennies(int | float): Amount in pennieslong(bool): Use long format for sub-pound amounts
str - Formatted currency string
Logic:
-
= 100 pennies: Shows as pounds (“£1.23”)
- < 100 pennies + long=True: Shows as “X pence”
- < 100 pennies + long=False: Shows as “Xp”
Number Formatters
format_thousands
Formats numbers with thousand separators.value(Number | None | any): Value to format
str - Formatted number or original value
format_billions
Formats large numbers as words.count(int): Number to format
str - Humanized large number
Text and String Formatters
nl2br
Converts newlines to HTML line breaks.value(str | None): Text with newlines
Markup | str - HTML with <br> tags or empty string
Note: Escapes HTML in input text for security
sentence_case
Converts text to sentence case (first letter capitalized).sentence(str): Text to convert
str - Sentence with first word capitalized
Logic: Only capitalizes first word, leaves rest unchanged
insert_wbr
Inserts word break opportunities after commas.string(str): String with commas
Markup - HTML with word break hints
Use Case: Prevents long numbers from breaking layout
Message Count Formatters
message_count
Formats message count with appropriate noun.count(int): Number of messagesmessage_type(str): “email”, “sms”, “letter”, “international_sms”, or other
str - Formatted count with plural noun
message_count_label
Formats message count with optional suffix.count(int): Number of messagesmessage_type(str): Message typesuffix(str): Suffix text (default: “sent”)
message_count_noun
Returns appropriate noun for message count (used by message_count).Recipient Count Formatters
recipient_count
Formats recipient count with appropriate noun.count(int): Number of recipientstemplate_type(str): Template type
str - Formatted count with plural noun
recipient_count_label
Returns appropriate label for recipient count.Other Formatters
iteration_count
Formats iteration count as words.character_count
Formats character count.format_pluralise
Returns “s” for plural file lists.format_yes_no
Formats boolean as Yes/No text.value(bool | None): Value to formatyes(str): Text for True (default: “Yes”)no(str): Text for False (default: “No”)none(str): Text for None (default: “No”)
format_auth_type
Formats authentication type for display.auth_type(str): “email_auth”, “sms_auth”, or “webauthn_auth”with_indefinite_article(bool): Include “a” or “an” prefix
Phone Number Formatters
valid_phone_number
Validates a phone number.format_phone_number_human_readable
Formats phone number for display.redact_mobile_number
Redacts middle digits of mobile number.mobile_number(str): Phone numberspacing(str): Spacing around redaction character (default: "")
format_provider
Formats SMS provider name.- “firetext” → Title case
- Others → Uppercase
Email Utilities
guess_name_from_email_address
Extracts and formats a name from an email address.- Extracts part before @ and +
- Returns empty if no dot or starts with initial
- Replaces dots with spaces
- Removes digits and middle initials
- Converts to title case
- Normalizes spaces
extract_path_from_url
Extracts path from full URL.Data Retention
get_time_left
Calculates time remaining for data retention.created_at(datetime | str): Creation datetimeservice_data_retention_days(int): Retention period (default: 7)
str - Human-readable time remaining