From ac2ff6c2aaf146c10267714ba0cf8020a001627f Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Fri, 15 Nov 2019 08:48:14 +0100 Subject: [PATCH] Phone number fields: Do not crash on invalid default value --- src/pretix/base/forms/questions.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/pretix/base/forms/questions.py b/src/pretix/base/forms/questions.py index 6b9ef0c8d..f6a021338 100644 --- a/src/pretix/base/forms/questions.py +++ b/src/pretix/base/forms/questions.py @@ -25,6 +25,7 @@ from django_countries.fields import Country, CountryField from phonenumber_field.formfields import PhoneNumberField from phonenumber_field.phonenumber import PhoneNumber from phonenumber_field.widgets import PhoneNumberPrefixWidget +from phonenumbers import NumberParseException from phonenumbers.data import _COUNTRY_CODE_TO_REGION_CODE from pretix.base.forms.widgets import ( @@ -375,13 +376,17 @@ class BaseQuestionsForm(forms.Form): for prefix, values in _COUNTRY_CODE_TO_REGION_CODE.items(): if str(default_country) in values: default_prefix = prefix + try: + initial = PhoneNumber().from_string(initial.answer) if initial else "+{}.".format(default_prefix) + except NumberParseException: + initial = None field = PhoneNumberField( label=label, required=required, help_text=help_text, # 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 # the future. - initial=PhoneNumber().from_string(initial.answer) if initial else "+{}.".format(default_prefix), + initial=initial, widget=WrappedPhoneNumberPrefixWidget() ) field.question = q