Phone number fields: Do not crash on invalid default value

This commit is contained in:
Raphael Michel
2019-11-15 08:48:14 +01:00
parent 35ac2f0d2b
commit ac2ff6c2aa

View File

@@ -25,6 +25,7 @@ from django_countries.fields import Country, CountryField
from phonenumber_field.formfields import PhoneNumberField from phonenumber_field.formfields import PhoneNumberField
from phonenumber_field.phonenumber import PhoneNumber from phonenumber_field.phonenumber import PhoneNumber
from phonenumber_field.widgets import PhoneNumberPrefixWidget from phonenumber_field.widgets import PhoneNumberPrefixWidget
from phonenumbers import NumberParseException
from phonenumbers.data import _COUNTRY_CODE_TO_REGION_CODE from phonenumbers.data import _COUNTRY_CODE_TO_REGION_CODE
from pretix.base.forms.widgets import ( from pretix.base.forms.widgets import (
@@ -375,13 +376,17 @@ class BaseQuestionsForm(forms.Form):
for prefix, values in _COUNTRY_CODE_TO_REGION_CODE.items(): for prefix, values in _COUNTRY_CODE_TO_REGION_CODE.items():
if str(default_country) in values: if str(default_country) in values:
default_prefix = prefix default_prefix = prefix
try:
initial = PhoneNumber().from_string(initial.answer) if initial else "+{}.".format(default_prefix)
except NumberParseException:
initial = None
field = PhoneNumberField( field = PhoneNumberField(
label=label, required=required, label=label, required=required,
help_text=help_text, help_text=help_text,
# We now exploit an implementation detail in PhoneNumberPrefixWidget to allow us to pass just # We now exploit an implementation detail in PhoneNumberPrefixWidget to allow us to pass just
# a country code but no number as an initial value. It's a bit hacky, but should be stable for # a country code but no number as an initial value. It's a bit hacky, but should be stable for
# the future. # the future.
initial=PhoneNumber().from_string(initial.answer) if initial else "+{}.".format(default_prefix), initial=initial,
widget=WrappedPhoneNumberPrefixWidget() widget=WrappedPhoneNumberPrefixWidget()
) )
field.question = q field.question = q