Skip to content

Commit

Permalink
fail silently if package 'phonenumbers' is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed Jan 16, 2024
1 parent fb0850d commit f0099e7
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions formset/templatetags/phonenumber.py
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)

0 comments on commit f0099e7

Please sign in to comment.