From bec5276bf32dcabf9f162243706b7e28deaa0ad5 Mon Sep 17 00:00:00 2001 From: Bruce Schubert Date: Tue, 3 Nov 2020 16:36:25 -0800 Subject: [PATCH] Updated phone number display formatting to use configuration settings. - User defined phone number formats supportted. - Raw support (no formatting) supported. - Handles variable length phone numbers. - Closes #112 --- callattendant/app.cfg.example | 36 +++++++++++++++++++++++++++ callattendant/config.py | 3 +++ callattendant/userinterface/webapp.py | 35 +++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/callattendant/app.cfg.example b/callattendant/app.cfg.example index baad0b4..05ac58b 100644 --- a/callattendant/app.cfg.example +++ b/callattendant/app.cfg.example @@ -24,6 +24,42 @@ TESTING = False # This should not be changed/overrriden except during development/testing #DATABASE = "callattendant.db" +# PHONE_DISPLAY_SEPARATOR: Specify the character used to format phone numbers, e.g, a space, hyphen or period, +PHONE_DISPLAY_SEPARATOR = "-" + +# PHONE_DISPLAY_FORMAT: Define the formatting of phone numbers in the various lists. You must use the +# separator character defined by PHONE_DISPLAY_SEPARATOR above in the format string. +# +# The phone display format handles variable length phone numbers. Excess digits not included +# in the format string are prepended to the number with a separator. +# For example, the AUS number 006173XXXYYYY would be formatted as follows: +# General format: 006173-XXX-YYYY +# AU format: 00-61-73-XXX-YYYY +# US format: 006-173-XXX-YYYY +# UK format: 00-6173-XXX-YYYY +# FR format: 0061-73X-XX-YY-YY +# +# Example: General +# PHONE_DISPLAY_FORMAT = "###-####" +# +# Example: US +# PHONE_DISPLAY_FORMAT = "###-###-####" +# +# Example: UK +# PHONE_DISPLAY_FORMAT = "####-###-####" +# +# Example: FR +# PHONE_DISPLAY_FORMAT = "###-##-##-##" +# +# Example: AU +# PHONE_DISPLAY_FORMAT = "##-##-###-####" +# +# Example: Raw - no formatting +# PHONE_DISPLAY_FORMAT = "" +# +PHONE_DISPLAY_FORMAT = "###-###-####" + + # SCREENING_MODE: A tuple containing: "whitelist" and/or "blacklist", or empty SCREENING_MODE = ("whitelist", "blacklist") diff --git a/callattendant/config.py b/callattendant/config.py index 392efa5..902fa5f 100644 --- a/callattendant/config.py +++ b/callattendant/config.py @@ -28,6 +28,9 @@ "DATABASE": "callattendant.db", "SCREENING_MODE": ("whitelist", "blacklist"), + "PHONE_DISPLAY_SEPARATOR": "-", + "PHONE_DISPLAY_FORMAT": "###-###-####", + "BLOCK_ENABLED": True, "BLOCK_NAME_PATTERNS": {"V[0-9]{15}": "Telemarketer Caller ID", }, "BLOCK_NUMBER_PATTERNS": {}, diff --git a/callattendant/userinterface/webapp.py b/callattendant/userinterface/webapp.py index e060ebc..0cdc3cc 100644 --- a/callattendant/userinterface/webapp.py +++ b/callattendant/userinterface/webapp.py @@ -865,7 +865,40 @@ def settings(): def format_phone_no(number): - return'{}-{}-{}'.format(number[0:3], number[3:6], number[6:]) + ''' Format the phone number based on a template.''' + + config = current_app.config.get("MASTER_CONFIG") + template = config.get("PHONE_DISPLAY_FORMAT") + separator = config.get("PHONE_DISPLAY_SEPARATOR") + if separator == "" or template == "": + return number + + # Get the template and split into reverse ordered parts for processing + tmpl_parts = template.split(separator) + tmpl_parts.reverse() + + # Piece together the phone no from right to left to handle variable len numbers + number_len = len(number) + end = number_len + total_digits = 0 + phone_parts = [] + for tmpl in tmpl_parts: + # Assemble parts from right to left + start = max(0, end - len(tmpl)) + digits = number[start: end] + phone_parts.insert(0, digits) + # Prepare for next part + end = start + total_digits += len(digits) + # if number is shorter than template then exit loop + if start == 0: + break + # If number is longer then template, then capture remaining digits + if total_digits < number_len: + # Prepend remaining digits to parts + phone_parts.insert(0, number[0: number_len - total_digits]) + # Return the formatted number + return separator.join(phone_parts) def get_db():