Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Split up AddressForm to storefront and dashboard version

dominik-zeglen525c27cded22
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 81fe4600b3..74d42cdfda 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -7,6 +7,7 @@ from django_prices.forms import PriceField
from payments import PaymentError, PaymentStatus
from satchless.item import InsufficientStock
+from ..widgets import PhonePrefixWidget
from ...cart.forms import QuantityField
from ...core.forms import AjaxSelect2ChoiceField
from ...discount.utils import decrease_voucher_usage
@@ -18,6 +19,7 @@ from ...order.utils import (
recalculate_order, remove_empty_groups)
from ...product.models import Product, ProductVariant, Stock
from ...product.utils import allocate_stock, deallocate_stock
+from ...userprofile.i18n import AddressForm, PossiblePhoneNumberFormField
class OrderNoteForm(forms.ModelForm):
@@ -368,3 +370,8 @@ class AddVariantToDeliveryGroupForm(forms.Form):
add_variant_to_delivery_group(
self.group, variant, quantity, self.discounts)
recalculate_order(self.group.order)
+
+
+class DashboardAddressForm(AddressForm):
+ phone = PossiblePhoneNumberFormField(
+ widget=PhonePrefixWidget, required=False)
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index 2280e48688..074a80990a 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -17,7 +17,7 @@ from .forms import (
AddVariantToDeliveryGroupForm, CancelGroupForm, CancelOrderLineForm,
CancelOrderForm, CapturePaymentForm, ChangeStockForm, ChangeQuantityForm,
MoveLinesForm, OrderNoteForm, RefundPaymentForm, ReleasePaymentForm,
- RemoveVoucherForm, ShipGroupForm)
+ RemoveVoucherForm, ShipGroupForm, DashboardAddressForm)
from .utils import (
create_invoice_pdf, create_packing_slip_pdf, get_statics_absolute_url)
@@ -25,7 +25,6 @@ from ..views import staff_member_required
from ...core.utils import get_paginator_items
from ...order import GroupStatus
from ...order.models import Order, OrderLine, OrderNote
-from ...userprofile.i18n import AddressForm
@staff_member_required
@@ -352,7 +351,7 @@ def address_view(request, order_pk, address_type):
success_msg = pgettext_lazy(
'Dashboard message',
'Updated billing address')
- form = AddressForm(request.POST or None, instance=address)
+ form = DashboardAddressForm(request.POST or None, instance=address)
if form.is_valid():
form.save()
order.create_history_entry(comment=success_msg, user=request.user)
diff --git a/saleor/dashboard/widgets.py b/saleor/dashboard/widgets.py
index aa2818d84e..02795b4e25 100644
--- a/saleor/dashboard/widgets.py
+++ b/saleor/dashboard/widgets.py
@@ -2,6 +2,14 @@ from django import forms
from django.conf import settings
from django_filters.widgets import RangeWidget
from django_prices.widgets import PriceInput
+from django.forms import Select, TextInput
+from phonenumber_field.widgets import PhoneNumberPrefixWidget
+from phonenumbers import COUNTRY_CODE_TO_REGION_CODE
+
+
+phone_prefixes = [
+ ('+{}'.format(k), '+{}'.format(k)) for
+ (k, v) in sorted(COUNTRY_CODE_TO_REGION_CODE.items())]
class DateRangeWidget(RangeWidget):
@@ -15,3 +23,17 @@ class PriceRangeWidget(RangeWidget):
self.currency = getattr(settings, 'DEFAULT_CURRENCY')
widgets = (PriceInput(self.currency), PriceInput(self.currency))
super(RangeWidget, self).__init__(widgets, attrs)
+
+
+class PhonePrefixWidget(PhoneNumberPrefixWidget):
+ """
+ Overwrite widget to use choices with tuple in a simple form of "+XYZ: +XYZ"
+ Workaround for an issue:
+ https://github.com/stefanfoulis/django-phonenumber-field/issues/82
+ """
+
+ template_name = 'dashboard/order/widget/phone-prefix-widget.html'
+
+ def __init__(self, attrs=None):
+ widgets = (Select(attrs=attrs, choices=phone_prefixes), TextInput())
+ super(PhoneNumberPrefixWidget, self).__init__(widgets, attrs)
diff --git a/templates/dashboard/order/widget/phone-prefix-widget.html b/templates/dashboard/order/widget/phone-prefix-widget.html
new file mode 100644
index 0000000000..d51f4d03be
--- /dev/null
+++ b/templates/dashboard/order/widget/phone-prefix-widget.html
@@ -0,0 +1,7 @@
+<div class="phone-prefix-input">
+ {% for widget in widget.subwidgets %}
+ <div class="phone-prefix-input__field">
+ {% include widget.template_name %}
+ </div>
+ {% endfor %}
+</div>