From a750426ae0302a20903105a09549a37d1d8a6f1e Mon Sep 17 00:00:00 2001 From: Bruce Schubert Date: Tue, 3 Nov 2020 11:48:37 -0800 Subject: [PATCH 1/6] Refactored phone_no diplay formatting to use a single method. - Re: #112 --- callattendant/userinterface/webapp.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/callattendant/userinterface/webapp.py b/callattendant/userinterface/webapp.py index 1555834..e060ebc 100644 --- a/callattendant/userinterface/webapp.py +++ b/callattendant/userinterface/webapp.py @@ -320,7 +320,7 @@ def calls(): calls = [] for row in result_set: number = row[2] - phone_no = '{}-{}-{}'.format(number[0:3], number[3:6], number[6:]) + phone_no = format_phone_no(number) # Flask pages use the static folder to get resources. # In the static folder we have created a soft-link to the # data/messsages folder containing the actual messages. @@ -403,7 +403,7 @@ def calls_view(call_no): caller = {} if len(row) > 0: number = row[2] - phone_no = '{}-{}-{}'.format(number[0:3], number[3:6], number[6:]) + phone_no = format_phone_no(number) # Flask pages use the static folder to get resources. # In the static folder we have created a soft-link to the # data/messsages folder containing the actual messages. @@ -503,7 +503,7 @@ def callers_manage(call_no): number = record[2] caller.update(dict( call_no=record[0], - phone_no='{}-{}-{}'.format(number[0:3], number[3:6], number[6:]), + phone_no=format_phone_no(number), name=record[1], whitelisted=record[3], blacklisted=record[4], @@ -544,7 +544,7 @@ def callers_blocked(): records = [] for record in result_set: number = record[0] - phone_no = '{}-{}-{}'.format(number[0:3], number[3:6], number[6:]) + phone_no = format_phone_no(number) records.append(dict( Phone_Number=phone_no, Name=record[1], @@ -636,7 +636,7 @@ def callers_permitted(): records = [] for record in result_set: number = record[0] - phone_no = '{}-{}-{}'.format(number[0:3], number[3:6], number[6:]) + phone_no = format_phone_no(number) records.append(dict( Phone_Number=phone_no, Name=record[1], @@ -763,7 +763,7 @@ def messages(): msg_no=row[0], call_no=row[1], name=row[2], - phone_no='{}-{}-{}'.format(number[0:3], number[3:6], number[6:]), + phone_no=format_phone_no(number), wav_file=filepath, msg_played=row[5], date=date_time.strftime('%d-%b-%y'), From bec5276bf32dcabf9f162243706b7e28deaa0ad5 Mon Sep 17 00:00:00 2001 From: Bruce Schubert Date: Tue, 3 Nov 2020 16:36:25 -0800 Subject: [PATCH 2/6] 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(): From abbb9d131403a91a99d30f51716a339ef4a9d74e Mon Sep 17 00:00:00 2001 From: Bruce Schubert Date: Wed, 4 Nov 2020 09:27:27 -0800 Subject: [PATCH 3/6] Removed phone number input validation mask to accomodate various number formats. - Now using a common method to strip phone numbers of non-alphanumeric chars. - Removed input pattern mask from the Add Permitted Number and Add Blocked Number dialogs. - Displaying phone number format string in dialogs for guidance only. - closes #111 --- .../templates/callers_blocked.html | 5 +-- .../templates/callers_permitted.html | 5 +-- callattendant/userinterface/webapp.py | 42 ++++++++++++------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/callattendant/userinterface/templates/callers_blocked.html b/callattendant/userinterface/templates/callers_blocked.html index cbf4537..cafabb2 100644 --- a/callattendant/userinterface/templates/callers_blocked.html +++ b/callattendant/userinterface/templates/callers_blocked.html @@ -68,9 +68,8 @@