Skip to content

Commit 89b06b3

Browse files
Remove non-geo phone numbers from the SplitPhoneNumberWidget
The COUNTRY_CODE_TO_REGION_CODE dict contains values that are not specific to a region code: the COUNTRY_CODES_FOR_NON_GEO_REGIONS. These country codes are international, and are not a good fit for the SplitPhoneNumberWidget, which offers to select a region, then fill in the national number.
1 parent 8c38db4 commit 89b06b3

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

phonenumber_field/formfields.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from django.utils import translation
77
from django.utils.text import format_lazy
88
from django.utils.translation import pgettext, pgettext_lazy
9-
from phonenumbers import COUNTRY_CODE_TO_REGION_CODE
9+
from phonenumbers import COUNTRY_CODE_TO_REGION_CODE, COUNTRY_CODES_FOR_NON_GEO_REGIONS
1010

1111
from phonenumber_field import widgets
1212
from phonenumber_field.phonenumber import to_python, validate_region
@@ -17,10 +17,13 @@
1717
except ModuleNotFoundError:
1818
babel = None # type: ignore
1919

20+
GEO_COUNTRY_CODE_TO_REGION_CODE = COUNTRY_CODE_TO_REGION_CODE.copy()
21+
for country_code in COUNTRY_CODES_FOR_NON_GEO_REGIONS:
22+
del GEO_COUNTRY_CODE_TO_REGION_CODE[country_code]
2023
# ISO 3166-1 alpha-2 to national prefix
2124
REGION_CODE_TO_COUNTRY_CODE = {
2225
region_code: country_code
23-
for country_code, region_codes in COUNTRY_CODE_TO_REGION_CODE.items()
26+
for country_code, region_codes in GEO_COUNTRY_CODE_TO_REGION_CODE.items()
2427
for region_code in region_codes
2528
}
2629

tests/test_formfields.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.test import SimpleTestCase, override_settings
88
from django.utils import translation
99
from django.utils.functional import lazy
10+
from phonenumbers import COUNTRY_CODES_FOR_NON_GEO_REGIONS
1011

1112
from phonenumber_field.formfields import PhoneNumberField, SplitPhoneNumberField
1213
from phonenumber_field.phonenumber import PhoneNumber
@@ -175,6 +176,8 @@ class TestForm(forms.Form):
175176
rendered = str(TestForm())
176177
self.assertIn('<option value="" selected>---------</option>', rendered)
177178
self.assertIn('<option value="CN">China +86</option>', rendered)
179+
for prefix in COUNTRY_CODES_FOR_NON_GEO_REGIONS:
180+
self.assertNotIn(f"+{prefix}", rendered)
178181

179182
def test_initial(self):
180183
class TestForm(forms.Form):

0 commit comments

Comments
 (0)