diff --git a/src/pretix/base/forms/questions.py b/src/pretix/base/forms/questions.py index 711159b14..2cd593679 100644 --- a/src/pretix/base/forms/questions.py +++ b/src/pretix/base/forms/questions.py @@ -277,6 +277,10 @@ class NamePartsFormField(forms.MultiValueField): return value +def name_parts_is_empty(name_parts_dict): + return not any(k != "_scheme" and v for k, v in name_parts_dict.items()) + + class WrappedPhonePrefixSelect(Select): initial = None @@ -1149,12 +1153,12 @@ class BaseInvoiceAddressForm(forms.ModelForm): data['vat_id'] = '' if data.get('is_business') and not ask_for_vat_id(data.get('country')): data['vat_id'] = '' - if self.event.settings.invoice_address_required: + if self.address_validation and self.event.settings.invoice_address_required and not self.all_optional: if data.get('is_business') and not data.get('company'): raise ValidationError({"company": _('You need to provide a company name.')}) - if not data.get('is_business') and not data.get('name_parts'): + if not data.get('is_business') and name_parts_is_empty(data.get('name_parts', {})): raise ValidationError(_('You need to provide your name.')) - if not self.all_optional and 'street' in self.fields and not data.get('street') and not data.get('zipcode') and not data.get('city'): + if not data.get('street') and not data.get('zipcode') and not data.get('city'): raise ValidationError({"street": _('This field is required.')}) if 'vat_id' in self.changed_data or not data.get('vat_id'): @@ -1167,7 +1171,7 @@ class BaseInvoiceAddressForm(forms.ModelForm): if all( not v for k, v in data.items() if k not in ('is_business', 'country', 'name_parts') - ) and len(data.get('name_parts', {})) == 1: + ) and name_parts_is_empty(data.get('name_parts', {})): # Do not save the country if it is the only field set -- we don't know the user even checked it! self.cleaned_data['country'] = '' diff --git a/src/tests/base/test_questions.py b/src/tests/base/test_questions.py new file mode 100644 index 000000000..ca5101d82 --- /dev/null +++ b/src/tests/base/test_questions.py @@ -0,0 +1,32 @@ +# +# This file is part of pretix (Community Edition). +# +# Copyright (C) 2014-2020 Raphael Michel and contributors +# Copyright (C) 2020-2021 rami.io GmbH and contributors +# +# This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General +# Public License as published by the Free Software Foundation in version 3 of the License. +# +# ADDITIONAL TERMS APPLY: Pursuant to Section 7 of the GNU Affero General Public License, additional terms are +# applicable granting you additional permissions and placing additional restrictions on your usage of this software. +# Please refer to the pretix LICENSE file to obtain the full terms applicable to this work. If you did not receive +# this file, see . +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied +# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more +# details. +# +# You should have received a copy of the GNU Affero General Public License along with this program. If not, see +# . +# +from pretix.base.forms.questions import name_parts_is_empty + + +def test_name_parts_is_empty(): + assert name_parts_is_empty({}) is True + assert name_parts_is_empty({"_scheme": "foo"}) is True + assert name_parts_is_empty({"_scheme": "foo", "full_name": ""}) is True + assert name_parts_is_empty({"full_name": None}) is True + assert name_parts_is_empty({"full_name": "Flora Nord"}) is False + assert name_parts_is_empty({"_scheme": "foo", "given_name": "Alice"}) is False + assert name_parts_is_empty({"_legacy": "Alice"}) is False