Skip to content

Commit

Permalink
Removed phone number input validation mask to accomodate various numb…
Browse files Browse the repository at this point in the history
…er 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
  • Loading branch information
emxsys committed Nov 4, 2020
1 parent bec5276 commit abbb9d1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
5 changes: 2 additions & 3 deletions callattendant/userinterface/templates/callers_blocked.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ <h5 class="modal-title" id="addModalLabel">Add Blocked Number</h5>
<form>
<div class="modal-body">
<div class="form-group">
<label for="add-phone" class="col-form-label">Phone: ...-...-....</label>
<input name="phone" type="tel" class="form-control" id="add-phone" required="required"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" >
<label for="add-phone" class="col-form-label">Phone: {{ phone_no_format }}</label>
<input name="phone" type="tel" class="form-control" id="add-phone" required="required" >
</div>
<div class="form-group">
<label for="add-name" class="col-form-label">Name:</label>
Expand Down
5 changes: 2 additions & 3 deletions callattendant/userinterface/templates/callers_permitted.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ <h5 class="modal-title" id="addModalLabel">Add Permitted Number</h5>
<form>
<div class="modal-body">
<div class="form-group">
<label for="add-phone" class="col-form-label">Phone: ...-...-....</label>
<input name="phone" type="tel" class="form-control" id="add-phone" required="required"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" >
<label for="add-phone" class="col-form-label">Phone: {{ phone_no_format }}</label>
<input name="phone" type="tel" class="form-control" id="add-phone" required="required" >
</div>
<div class="form-group">
<label for="add-name" class="col-form-label">Name:</label>
Expand Down
42 changes: 28 additions & 14 deletions callattendant/userinterface/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ def callers_manage(call_no):

# Post changes to the blacklist or whitelist table before rendering
if request.method == 'POST':
number = request.form['phone_no'].replace('-', '')
number = strip_phone_no(request.form['phone_no'])
if request.form['action'] == 'add-permit':
caller = {}
caller['NMBR'] = number
Expand Down Expand Up @@ -564,6 +564,7 @@ def callers_blocked():
return render_template(
'callers_blocked.html',
active_nav_item='blocked',
phone_no_format=current_app.config.get("MASTER_CONFIG").get("PHONE_DISPLAY_FORMAT"),
blacklist=records,
page=page,
per_page=per_page,
Expand All @@ -577,8 +578,7 @@ def callers_blocked_add():
Add a new blacklist entry
"""
caller = {}
# TODO: Strip all none digits from phone via regex
number = request.form["phone"].replace('-', '')
number = strip_phone_no(request.form["phone"])
caller['NMBR'] = number
caller['NAME'] = request.form["name"]
print("Adding " + number + " to blacklist")
Expand All @@ -596,7 +596,7 @@ def callers_blocked_update(phone_no):
"""
Update the blacklist entry associated with the phone number.
"""
number = phone_no.replace('-', '')
number = strip_phone_no(phone_no)
print("Updating " + number + " in blacklist")
blacklist = Blacklist(get_db(), current_app.config)
blacklist.update_number(number, request.form['name'], request.form['reason'])
Expand All @@ -609,7 +609,7 @@ def callers_blocked_delete(phone_no):
"""
Delete the blacklist entry associated with the phone number.
"""
number = phone_no.replace('-', '')
number = strip_phone_no(phone_no)

print("Removing " + number + " from blacklist")
blacklist = Blacklist(get_db(), current_app.config)
Expand Down Expand Up @@ -655,6 +655,7 @@ def callers_permitted():
return render_template(
'callers_permitted.html',
active_nav_item='permitted',
phone_no_format=current_app.config.get("MASTER_CONFIG").get("PHONE_DISPLAY_FORMAT"),
whitelist=records,
total_calls=total,
page=page,
Expand All @@ -669,8 +670,7 @@ def callers_permitted_add():
Add a new whitelist entry
"""
caller = {}
# TODO: Strip all none digits from phone via regex
number = request.form['phone'].replace('-', '')
number = strip_phone_no(request.form['phone'])
caller['NMBR'] = number
caller['NAME'] = request.form['name']
print("Adding " + number + " to whitelist")
Expand All @@ -688,7 +688,8 @@ def callers_permitted_update(phone_no):
"""
Update the whitelist entry associated with the phone number.
"""
number = phone_no.replace('-', '')
number = strip_phone_no(phone_no)

print("Updating " + number + " in whitelist")
whitelist = Whitelist(get_db(), current_app.config)
whitelist.update_number(number, request.form['name'], request.form['reason'])
Expand All @@ -701,7 +702,7 @@ def callers_permitted_delete(phone_no):
"""
Delete the whitelist entry associated with the phone number.
"""
number = phone_no.replace('-', '')
number = strip_phone_no(phone_no)

print("Removing " + number + " from whitelist")
whitelist = Whitelist(get_db(), current_app.config)
Expand Down Expand Up @@ -865,8 +866,9 @@ def settings():


def format_phone_no(number):
''' Format the phone number based on a template.'''

'''
Returns a formatted the phone number based on the PHONE_DISPLAY_FORMAT configuration setting.
'''
config = current_app.config.get("MASTER_CONFIG")
template = config.get("PHONE_DISPLAY_FORMAT")
separator = config.get("PHONE_DISPLAY_SEPARATOR")
Expand Down Expand Up @@ -900,9 +902,17 @@ def format_phone_no(number):
# Return the formatted number
return separator.join(phone_parts)

def strip_phone_no(phone_no):
'''
Returns the phone no stripped of all non-alphanumeric characters.
'''
return "".join(filter(str.isalnum, phone_no))


def get_db():
'''Get a connection to the database'''
'''
Get a connection to the database
'''
# Flask template for database connections
if 'db' not in g:
master_config = current_app.config.get("MASTER_CONFIG")
Expand All @@ -916,7 +926,9 @@ def get_db():


def close_db(e=None):
'''Clost the connection to the database'''
'''
Clost the connection to the database
'''
# Flask template for database connections
db = g.pop('db', None)

Expand All @@ -925,7 +937,9 @@ def close_db(e=None):


def get_row_count(table_name):
'''Returns the row count for the given table'''
'''
Returns the row count for the given table
'''
# Using the current request's db connection
sql = 'select count(*) from {}'.format(table_name)
g.cur.execute(sql)
Expand Down

0 comments on commit abbb9d1

Please sign in to comment.