Skip to content

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

Connect an order to a user account or an email

Anna Siwik8b5d528e036c
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 14697da971..0745b2eb05 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -27,6 +27,7 @@ from .utils import fulfill_order_line
class ConfirmDraftOrderForm(forms.ModelForm):
+ """Save draft order as a ready to fulfill."""
class Meta:
model = Order
fields = []
@@ -44,6 +45,24 @@ class ConfirmDraftOrderForm(forms.ModelForm):
return super().save(commit)
+class OrderCustomerForm(forms.ModelForm):
+ """Set customer details in an order."""
+ class Meta:
+ model = Order
+ fields = ['user', 'user_email']
+
+ def clean(self):
+ cleaned_data = super().clean()
+ user_email = cleaned_data.get('user_email')
+ user = cleaned_data.get('user')
+ if user and user_email:
+ raise forms.ValidationError(pgettext_lazy(
+ 'Edit customer details in order form error',
+ 'An order can be related either with an email or an existing '
+ 'user account'))
+ return self.cleaned_data
+
+
class OrderNoteForm(forms.ModelForm):
class Meta:
model = OrderNote
diff --git a/saleor/dashboard/order/urls.py b/saleor/dashboard/order/urls.py
index 11cbe0418d..0a427ca77a 100644
--- a/saleor/dashboard/order/urls.py
+++ b/saleor/dashboard/order/urls.py
@@ -15,6 +15,8 @@ urlpatterns = [
views.cancel_order, name='order-cancel'),
url(r'^(?P<order_pk>\d+)/address/(?P<address_type>billing|shipping)/$',
views.address_view, name='address-edit'),
+ url(r'^(?P<order_pk>\d+)/customer/edit/$',
+ views.order_customer_edit, name='order-customer-edit'),
url(r'^(?P<order_pk>\d+)/payment/(?P<payment_pk>\d+)/capture/$',
views.capture_payment, name='capture-payment'),
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index 1207d29645..c500bd764e 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -29,8 +29,8 @@ from .forms import (
CancelFulfillmentForm, CancelOrderForm, CancelOrderLineForm,
CapturePaymentForm, ChangeQuantityForm, ChangeStockForm,
ConfirmDraftOrderForm, FulfillmentForm, FulfillmentLineForm,
- FulfillmentTrackingNumberForm, OrderNoteForm, RefundPaymentForm,
- ReleasePaymentForm, RemoveVoucherForm)
+ FulfillmentTrackingNumberForm, OrderCustomerForm, OrderNoteForm,
+ RefundPaymentForm, ReleasePaymentForm, RemoveVoucherForm)
from .utils import (
create_invoice_pdf, create_packing_slip_pdf, get_statics_absolute_url)
@@ -319,6 +319,26 @@ def address_view(request, order_pk, address_type):
return TemplateResponse(request, 'dashboard/order/address_form.html', ctx)
+@staff_member_required
+@permission_required('order.edit_order')
+def order_customer_edit(request, order_pk):
+ order = Order.objects.get(pk=order_pk)
+ form = OrderCustomerForm(request.POST or None, instance=order)
+ status = 200
+ if form.is_valid():
+ form.save()
+ msg = pgettext_lazy('Dashboard message', '')
+ order.history.create(content=msg, user=request.user)
+ messages.success(request, msg)
+ return redirect('dashboard:order-details', order_pk=order_pk)
+ elif form.errors:
+ status = 400
+ ctx = {'order': order, 'form': form}
+ return TemplateResponse(
+ request, 'dashboard/order/modal/edit_customer.html', ctx,
+ status=status)
+
+
@staff_member_required
@permission_required('order.edit_order')
def cancel_order(request, order_pk):
diff --git a/saleor/order/migrations/0043_auto_20180319_1016.py b/saleor/order/migrations/0043_auto_20180320_0928.py
similarity index 77%
rename from saleor/order/migrations/0043_auto_20180319_1016.py
rename to saleor/order/migrations/0043_auto_20180320_0928.py
index 050acac9f1..55bccdb94f 100644
--- a/saleor/order/migrations/0043_auto_20180319_1016.py
+++ b/saleor/order/migrations/0043_auto_20180320_0928.py
@@ -1,4 +1,4 @@
-# Generated by Django 2.0.3 on 2018-03-19 15:16
+# Generated by Django 2.0.3 on 2018-03-20 14:28
from django.db import migrations, models
import django.db.models.deletion
@@ -21,4 +21,9 @@ class Migration(migrations.Migration):
name='status',
field=models.CharField(choices=[('draft', 'Draft'), ('unfulfilled', 'Unfulfilled'), ('partially fulfilled', 'Partially fulfilled'), ('fulfilled', 'Fulfilled'), ('canceled', 'Canceled')], default='unfulfilled', max_length=32),
),
+ migrations.AlterField(
+ model_name='order',
+ name='user_email',
+ field=models.EmailField(blank=True, default='', max_length=254),
+ ),
]
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 0e6e2b4149..e66408d57f 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -39,8 +39,7 @@ class Order(models.Model):
shipping_address = models.ForeignKey(
Address, related_name='+', editable=False, null=True,
on_delete=models.PROTECT)
- user_email = models.EmailField(
- blank=True, default='', editable=False)
+ user_email = models.EmailField(blank=True, default='')
shipping_price_net = MoneyField(
currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
default=0, editable=False)
diff --git a/templates/dashboard/order/detail.html b/templates/dashboard/order/detail.html
index 984f31416a..60cd83e87f 100644
--- a/templates/dashboard/order/detail.html
+++ b/templates/dashboard/order/detail.html
@@ -177,6 +177,14 @@
<div class="card-content card-border">
<span class="card-title black-text">
{% trans "Customer" context "" %}
+ {% if perms.order.edit_order and order.is_draft %}
+ <span class="right">
+ <a href="#base-modal" data-href="{% url "dashboard:order-customer-edit" order_pk=order.pk %}" class="btn-flat waves-effect modal-trigger-custom"
+ title="{% trans "Edit customer details" context "Order customer details action title" %}">
+ {% trans "Edit" context "Dashboard edit action" %}
+ </a>
+ </span>
+ {% endif %}
</span>
{% if order.user %}
<a href="{% url "dashboard:customer-details" pk=order.user.pk %}">{{ order.user }}</a>
@@ -343,8 +351,14 @@
<td class="right-align">
<p>{{ order.get_subtotal.gross|amount:'html' }}</p>
<p>{{ order.shipping_price.gross|amount:'html' }}</p>
- {% if order.discount_amount %}
- <p>{% discount_as_negative order.discount_amount html=True %}</p>
+ {% if order.is_draft or order.discount_amount %}
+ <p>
+ {% if order.discount_amount %}
+ {% discount_as_negative order.discount_amount html=True %}
+ {% else %}
+ {% trans "-" context "Order details empty discount" %}
+ {% endif %}
+ </p>
{% endif %}
<p><strong>{{ order.total.gross|amount:'html' }}</strong></p>
</td>
diff --git a/templates/dashboard/order/modal/edit_customer.html b/templates/dashboard/order/modal/edit_customer.html
new file mode 100644
index 0000000000..4601e76241
--- /dev/null
+++ b/templates/dashboard/order/modal/edit_customer.html
@@ -0,0 +1,32 @@
+{% extends "dashboard/base_modal.html" %}
+
+{% load i18n %}
+{% load materializecss %}
+
+{% block action_url %}
+{% url 'dashboard:order-customer-edit' order_pk=order.pk %}
+{% endblock %}
+
+{% block title %}
+{% trans "Edit customer details" context "Modal edit customer title" %}
+{% endblock %}
+
+{% block content %}
+ <div class="row">
+ <div class="col s12">
+ {% blocktrans trimmed context "Modal edit customer text" %}
+ Connect an order with an account of an existing customer or provide a contact email address.
+ {% endblocktrans %}
+ </div>
+ </div>
+ <div class="row">
+ <div class="col s12">
+ {{ form.user|materializecss }}
+ {{ form.user_email|materializecss }}
+ </div>
+ </div>
+{% endblock %}
+
+{% block primary_action %}
+{% trans "Update" context "Modal edit customer primary action" %}
+{% endblock %}