Add order-level telephone field to core (#1872)

Co-authored-by: Martin Gross <gross@rami.io>
This commit is contained in:
Raphael Michel
2020-12-15 09:20:44 +01:00
committed by GitHub
parent c47e41ac8a
commit 4240ad43d0
27 changed files with 507 additions and 69 deletions

View File

@@ -442,7 +442,8 @@ class QuestionsStep(QuestionsViewMixin, CartMixin, TemplateFlowStep):
'email': (
self.cart_session.get('email', '') or
wd.get('email', '')
)
),
'phone': wd.get('phone', None)
}
initial.update(self.cart_session.get('contact_form_data', {}))
@@ -549,7 +550,10 @@ class QuestionsStep(QuestionsViewMixin, CartMixin, TemplateFlowStep):
_("We had difficulties processing your input. Please review the errors below."))
return self.render()
self.cart_session['email'] = self.contact_form.cleaned_data['email']
self.cart_session['contact_form_data'] = self.contact_form.cleaned_data
d = dict(self.contact_form.cleaned_data)
if d.get('phone'):
d['phone'] = str(d['phone'])
self.cart_session['contact_form_data'] = d
if self.address_asked or self.request.event.settings.invoice_name_required:
addr = self.invoice_form.save()
try:
@@ -820,6 +824,9 @@ class ConfirmStep(CartMixin, AsyncAction, TemplateFlowStep):
]
else:
ctx['contact_info'] = []
phone = self.cart_session.get('contact_form_data', {}).get('phone')
if phone:
ctx['contact_info'].append((_('Phone number'), phone))
responses = contact_form_fields.send(self.event, request=self.request)
for r, response in sorted(responses, key=lambda r: str(r[0])):
for key, value in response.items():

View File

@@ -1,13 +1,20 @@
from itertools import chain
from babel import localedata
from django import forms
from django.core.exceptions import ValidationError
from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _
from django.utils.translation import get_language, gettext_lazy as _
from phonenumber_field.formfields import PhoneNumberField
from phonenumber_field.phonenumber import PhoneNumber
from phonenumbers import NumberParseException
from phonenumbers.data import _COUNTRY_CODE_TO_REGION_CODE
from pretix.base.forms.questions import (
BaseInvoiceAddressForm, BaseQuestionsForm,
BaseInvoiceAddressForm, BaseQuestionsForm, WrappedPhoneNumberPrefixWidget,
guess_country,
)
from pretix.base.i18n import language
from pretix.base.validators import EmailBanlistValidator
from pretix.presale.signals import contact_form_fields
@@ -31,6 +38,35 @@ class ContactForm(forms.Form):
help_text=_('Please enter the same email address again to make sure you typed it correctly.'),
)
if self.event.settings.order_phone_asked:
babel_locale = 'en'
# Babel, and therefore django-phonenumberfield, do not support our custom locales such as de_Informal
if localedata.exists(get_language()):
babel_locale = get_language()
elif localedata.exists(get_language()[:2]):
babel_locale = get_language()[:2]
with language(babel_locale):
default_country = guess_country(self.event)
default_prefix = None
for prefix, values in _COUNTRY_CODE_TO_REGION_CODE.items():
if str(default_country) in values:
default_prefix = prefix
try:
initial = self.initial.pop('phone', None)
initial = PhoneNumber().from_string(initial) if initial else "+{}.".format(default_prefix)
except NumberParseException:
initial = None
self.fields['phone'] = PhoneNumberField(
label=_('Phone number'),
required=self.event.settings.order_phone_required,
help_text=self.event.settings.checkout_phone_helptext,
# 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=initial,
widget=WrappedPhoneNumberPrefixWidget()
)
if not self.request.session.get('iframe_session', False):
# There is a browser quirk in Chrome that leads to incorrect initial scrolling in iframes if there
# is an autofocus field. Who would have thought… See e.g. here:

View File

@@ -5,6 +5,7 @@
{% load money %}
{% load expiresformat %}
{% load eventurl %}
{% load phone_format %}
{% block title %}{% trans "Order details" %}{% endblock %}
{% block content %}
{% if "thanks" in request.GET or "paid" in request.GET %}
@@ -205,7 +206,7 @@
{% eventsignal event "pretix.presale.signals.order_info" order=order request=request %}
<div class="row">
{% if invoices %}
<div class="col-xs-12 {% if invoice_address_asked or request.event.settings.invoice_name_required %}col-md-6{% endif %}">
<div class="col-xs-12 col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">
@@ -226,7 +227,7 @@
</div>
</div>
{% elif can_generate_invoice %}
<div class="col-xs-12 {% if invoice_address_asked or request.event.settings.invoice_name_required %}col-md-6{% endif %}">
<div class="col-xs-12 col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">
@@ -256,10 +257,10 @@
</div>
</div>
{% endif %}
{% if invoice_address_asked or request.event.settings.invoice_name_required %}
<div class="col-xs-12 {% if invoices or can_generate_invoice %}col-md-6{% endif %}">
<div class="panel panel-primary">
<div class="panel-heading">
<div class="col-xs-12 {% if invoices or can_generate_invoice %}col-md-6{% endif %}">
<div class="panel panel-primary">
<div class="panel-heading">
{% if invoice_address_asked or request.event.settings.invoice_name_required %}
{% if order.can_modify_answers %}
<div class="pull-right flip">
<a href="{% eventurl event "presale:event.order.modify" secret=order.secret order=order.code %}">
@@ -268,49 +269,53 @@
</a>
</div>
{% endif %}
<h3 class="panel-title">
{% if request.event.settings.invoice_address_asked %}
{% trans "Invoice information" %}
{% else %}
{% trans "Contact information" %}
{% endif %}
<h3 class="panel-title">
{% trans "Your information" %}
</h3>
</div>
<div class="panel-body">
<dl class="dl-horizontal">
{% if order.email %}
<dt>{% trans "E-mail" %}</dt>
<dd>{{ order.email }}</dd>
{% endif %}
{% if order.phone %}
<dt>{% trans "Phone number" %}</dt>
<dd>{{ order.phone|phone_format }}</dd>
{% endif %}
{% if invoice_address_asked %}
<dt>{% trans "Company" %}</dt>
<dd>{{ order.invoice_address.company }}</dd>
{% endif %}
<dt>{% trans "Name" %}</dt>
<dd>{{ order.invoice_address.name }}</dd>
{% if invoice_address_asked %}
<dt>{% trans "Address" %}</dt>
<dd>{{ order.invoice_address.street|linebreaksbr }}</dd>
<dt>{% trans "ZIP code and city" %}</dt>
<dd>{{ order.invoice_address.zipcode }} {{ order.invoice_address.city }}</dd>
<dt>{% trans "Country" %}</dt>
<dd>{{ order.invoice_address.country.name|default:order.invoice_address.country_old }}</dd>
{% if order.invoice_address.state %}
<dt>{% trans "State" context "address" %}</dt>
<dd>{{ order.invoice_address.state_name }}</dd>
{% endif %}
</h3>
</div>
<div class="panel-body">
<dl class="dl-horizontal">
{% if invoice_address_asked %}
<dt>{% trans "Company" %}</dt>
<dd>{{ order.invoice_address.company }}</dd>
{% if request.event.settings.invoice_address_vatid %}
<dt>{% trans "VAT ID" %}</dt>
<dd>{{ order.invoice_address.vat_id }}</dd>
{% endif %}
<dt>{% trans "Name" %}</dt>
<dd>{{ order.invoice_address.name }}</dd>
{% if invoice_address_asked %}
<dt>{% trans "Address" %}</dt>
<dd>{{ order.invoice_address.street|linebreaksbr }}</dd>
<dt>{% trans "ZIP code and city" %}</dt>
<dd>{{ order.invoice_address.zipcode }} {{ order.invoice_address.city }}</dd>
<dt>{% trans "Country" %}</dt>
<dd>{{ order.invoice_address.country.name|default:order.invoice_address.country_old }}</dd>
{% if order.invoice_address.state %}
<dt>{% trans "State" context "address" %}</dt>
<dd>{{ order.invoice_address.state_name }}</dd>
{% endif %}
{% if request.event.settings.invoice_address_vatid %}
<dt>{% trans "VAT ID" %}</dt>
<dd>{{ order.invoice_address.vat_id }}</dd>
{% endif %}
{% if request.event.settings.invoice_address_custom_field and order.invoice_address.custom_field %}
<dt>{{ request.event.settings.invoice_address_custom_field }}</dt>
<dd>{{ order.invoice_address.custom_field }}</dd>
{% endif %}
<dt>{% trans "Internal Reference" %}</dt>
<dd>{{ order.invoice_address.internal_reference }}</dd>
{% if request.event.settings.invoice_address_custom_field and order.invoice_address.custom_field %}
<dt>{{ request.event.settings.invoice_address_custom_field }}</dt>
<dd>{{ order.invoice_address.custom_field }}</dd>
{% endif %}
</dl>
</div>
<dt>{% trans "Internal Reference" %}</dt>
<dd>{{ order.invoice_address.internal_reference }}</dd>
{% endif %}
</dl>
</div>
</div>
{% endif %}
</div>
<div class="clearfix"></div>
</div>
{% if user_change_allowed or user_cancel_allowed %}