-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fail silently if package 'phonenumbers' is not installed
- Loading branch information
Showing
1 changed file
with
12 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
from django import template | ||
from phonenumbers import PhoneNumberFormat, format_number, parse | ||
|
||
|
||
def format_phonenumber(value, arg='international'): | ||
"""Formats a phonenumber according to the country's convenience.""" | ||
phone_number = parse(value, None) | ||
num_format = PhoneNumberFormat.NATIONAL if arg == 'national' else PhoneNumberFormat.INTERNATIONAL | ||
return format_number(phone_number, num_format) | ||
|
||
try: | ||
from phonenumbers import PhoneNumberFormat, format_number, parse | ||
except ImportError: | ||
def format_phonenumber(value, arg=None): | ||
"""As backup, render phonenumber in E.164 format.""" | ||
return value | ||
else: | ||
def format_phonenumber(value, arg='international'): | ||
"""Formats a phonenumber according to the country's convenience.""" | ||
phone_number = parse(value, None) | ||
num_format = PhoneNumberFormat.NATIONAL if arg == 'national' else PhoneNumberFormat.INTERNATIONAL | ||
return format_number(phone_number, num_format) | ||
|
||
register = template.Library() | ||
register.filter('format_phonenumber', format_phonenumber) |