Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add customer_note field to Order model and drop OrderNote from order views
oldPadavan39907ce02db1
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex e8bb953d63..4f6acbf10f 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -828,7 +828,8 @@ def _fill_order_with_cart_data(order, cart, discounts, taxes):order, line.variant, line.quantity, discounts, taxes)if cart.note:- order.notes.create(user=order.user, content=cart.note)+ order.customer_note = cart.note+ order.save()@transaction.atomicdiff --git a/saleor/order/forms.py b/saleor/order/forms.pyindex 0602ccfeed..67f8abec06 100644--- a/saleor/order/forms.py+++ b/saleor/order/forms.py@@ -4,7 +4,7 @@ from django.utils.translation import pgettext_lazyfrom payments import PaymentStatusfrom ..account.forms import SignupForm-from .models import OrderNote, Payment+from .models import Order, OrderNote, Paymentclass PaymentMethodsForm(forms.Form):@@ -49,11 +49,11 @@ class PasswordForm(SignupForm):self.fields['email'].widget = forms.HiddenInput()-class OrderNoteForm(forms.ModelForm):+class CustomerNoteForm(forms.ModelForm):+ customer_note = forms.CharField(+ max_length=250, required=False, strip=True, label=False,+ widget=forms.Textarea({'rows': 3}))+class Meta:- model = OrderNote- fields = ['content']- widgets = {- 'content': forms.Textarea({'rows': 3, 'placeholder': False})}- labels = {- 'content': pgettext_lazy('Order note', 'Add note to order')}+ model = Order+ fields = ['customer_note']diff --git a/saleor/order/migrations/0051_order_customer_note.py b/saleor/order/migrations/0051_order_customer_note.pynew file mode 100644index 0000000000..c90d536942--- /dev/null+++ b/saleor/order/migrations/0051_order_customer_note.py@@ -0,0 +1,18 @@+# Generated by Django 2.0.3 on 2018-07-31 14:05++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ('order', '0050_remove_ordernote_is_public'),+ ]++ operations = [+ migrations.AddField(+ model_name='order',+ name='customer_note',+ field=models.TextField(blank=True, default=''),+ ),+ ]diff --git a/saleor/order/models.py b/saleor/order/models.pyindex 592887b981..465c7aee57 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -90,6 +90,7 @@ class Order(models.Model):decimal_places=settings.DEFAULT_DECIMAL_PLACES, default=0)discount_name = models.CharField(max_length=255, default='', blank=True)display_gross_prices = models.BooleanField(default=True)+ customer_note = models.TextField(blank=True, default='')objects = OrderQueryset.as_manager()diff --git a/saleor/order/views.py b/saleor/order/views.pyindex fdaf48a528..0d3b79ee31 100644--- a/saleor/order/views.py+++ b/saleor/order/views.py@@ -17,8 +17,8 @@ from ..account.forms import LoginFormfrom ..account.models import Userfrom ..core.utils import get_client_ipfrom .forms import (- OrderNoteForm, PasswordForm, PaymentDeleteForm, PaymentMethodsForm)-from .models import Order, OrderNote, Payment+ CustomerNoteForm, PasswordForm, PaymentDeleteForm, PaymentMethodsForm)+from .models import Order, Paymentfrom .utils import attach_order_to_user, check_order_statuslogger = logging.getLogger(__name__)@@ -31,12 +31,9 @@ def details(request, token):orders = orders.select_related('billing_address', 'shipping_address', 'user')order = get_object_or_404(orders, token=token)- user = request.user if request.user.is_authenticated else None- notes = order.notes.filter(user=user)- ctx = {'order': order, 'notes': notes}- if order.is_open():- note = OrderNote(order=order, user=user)- note_form = OrderNoteForm(request.POST or None, instance=note)+ ctx = {'order': order}+ if order.is_open() and not order.customer_note:+ note_form = CustomerNoteForm(request.POST or None, instance=order)ctx.update({'note_form': note_form})if request.method == 'POST':if note_form.is_valid():diff --git a/templates/dashboard/order/detail.html b/templates/dashboard/order/detail.htmlindex 012c75b4c0..15e90246e2 100644--- a/templates/dashboard/order/detail.html+++ b/templates/dashboard/order/detail.html@@ -518,6 +518,22 @@{% endif %}</div>{% endfor %}++ <div class="card order-notes{% if not order.customer_note %} print-hide{% endif %}" id="customer-note">+ <div class="card-content">+ <span class="card-title black-text">+ {% trans "Customer note" context "Customer order note card title" %}+ </span>+ {% if order.customer_note %}+ <p>{{ order.customer_note }}</p>+ {% else %}+ <p>+ {% trans "There's no note from customer associated with this order." context "No customer order note card message" %}+ </p>+ {% endif %}+ </div>+ </div>+<div class="card order-notes{% if not notes %} print-hide{% endif %}" id="order-notes"><div class="card-content"><span class="card-title black-text">diff --git a/templates/order/details.html b/templates/order/details.htmlindex 3381c61678..025319b899 100644--- a/templates/order/details.html+++ b/templates/order/details.html@@ -63,35 +63,11 @@{% endif %}{% include "order/_ordered_items_table.html" %}<div class="order-notes">- {% if notes %}- <h2 class="order-notes__header">{% trans "Notes" context "Order notes list" %}</h2>- {% for note in notes %}- <small>- {% if note.user %}- {% if note.user == user %}- {% blocktrans with date=note.date %}- {{ date }} by you- {% endblocktrans %}- {% elif note.user.is_staff %}- {% trans 'staff' context 'Order note author is staff' as note_author_staff %}- {% blocktrans with author=note_author_staff date=note.date %}- {{ date }} by <span class="text-muted">{{ author }}</span>- {% endblocktrans %}- {% else %}- {% blocktrans with author=note.user date=note.date %}- {{ date }} by {{ author }}- {% endblocktrans %}- {% endif %}- {% else %}- {% blocktrans with date=note.date %}- {{ date }} by anonymous customer- {% endblocktrans %}- {% endif %}- </small>- <p>{{ note.content }}</p>- {% endfor %}+ {% if order.customer_note %}+ <h2 class="order-notes__header">{% trans "Customer note" context "Customer order note" %}</h2>+ <p>{{ order.customer_note }}</p>{% endif %}- {% if order.is_open %}+ {% if order.is_open and not order.customer_note %}<form method="post" class="form-group">{% csrf_token %}{% bootstrap_form note_form display_labels=False %}diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex e95afa8565..0b06e9d694 100644--- a/tests/dashboard/test_order.py+++ b/tests/dashboard/test_order.py@@ -10,7 +10,7 @@ from tests.utils import get_form_errors, get_redirect_locationfrom saleor.checkout import AddressTypefrom saleor.core.utils.taxes import ZERO_MONEY, ZERO_TAXED_MONEY-from saleor.dashboard.order.forms import ChangeQuantityForm, OrderNoteForm+from saleor.dashboard.order.forms import ChangeQuantityFormfrom saleor.dashboard.order.utils import (fulfill_order_line, remove_customer_from_order, save_address_in_order,update_order_with_user_addresses)@@ -1196,3 +1196,17 @@ def test_render_cancel_fulfillment_page(admin_client, fulfilled_order):kwargs={'order_pk': fulfilled_order.pk})response = admin_client.get(url)assert response.status_code == 200+++def test_view_add_order_note(admin_client, order_with_lines):+ url = reverse(+ 'dashboard:order-add-note',+ kwargs={'order_pk': order_with_lines.pk})+ note_content = 'this is a note'+ data = {+ 'csrfmiddlewaretoken': 'hello',+ 'content': note_content}+ response = admin_client.post(url, data)+ assert response.status_code == 200+ order_with_lines.refresh_from_db()+ assert order_with_lines.notes.first().content == note_contentdiff --git a/tests/test_checkout.py b/tests/test_checkout.pyindex eee1848405..d3cb25fede 100644--- a/tests/test_checkout.py+++ b/tests/test_checkout.py@@ -493,16 +493,7 @@ def test_note_in_created_order(request_cart_with_item, address):request_cart_with_item.save()order = create_order(request_cart_with_item, 'tracking_code', discounts=None, taxes=None)- assert order.notes.filter(content='test_note').exists()---def test_note_in_created_order_empty_note(request_cart_with_item, address):- request_cart_with_item.shipping_address = address- request_cart_with_item.note = ''- request_cart_with_item.save()- order = create_order(- request_cart_with_item, 'tracking_code', discounts=None, taxes=None)- assert not order.notes.all()+ assert order.customer_note == request_cart_with_item.note@pytest.mark.parametrize(diff --git a/tests/test_order.py b/tests/test_order.pyindex be1e4a6e71..46f9775076 100644--- a/tests/test_order.py+++ b/tests/test_order.py@@ -12,7 +12,6 @@ from saleor.checkout.utils import create_orderfrom saleor.core.utils.taxes import (DEFAULT_TAX_RATE_NAME, get_tax_rate_by_name, get_taxes_for_country)from saleor.order import FulfillmentStatus, OrderStatus, models-from saleor.order.forms import OrderNoteFormfrom saleor.order.models import Orderfrom saleor.order.utils import (add_variant_to_order, cancel_fulfillment, cancel_order, recalculate_order,@@ -160,15 +159,6 @@ def test_view_connect_order_with_user_different_email(assert order.user is None-def test_add_note_to_order(order_with_lines):- order = order_with_lines- note = models.OrderNote(order=order, user=order.user)- note_form = OrderNoteForm({'content': 'test_note'}, instance=note)- note_form.is_valid()- note_form.save()- assert order.notes.first().content == 'test_note'--@pytest.mark.parametrize('track_inventory', (True, False))def test_restock_order_lines(order_with_lines, track_inventory):@@ -455,27 +445,12 @@ def test_add_order_note_view(order, authorized_client, customer_user):order.user_email = customer_user.emailorder.save()url = reverse('order:details', kwargs={'token': order.token})- note_content = 'bla-bla note'- data = {'content': note_content}+ customer_note = 'bla-bla note'+ data = {'customer_note': customer_note}response = authorized_client.post(url, data)redirect_url = reverse('order:details', kwargs={'token': order.token})assert get_redirect_location(response) == redirect_url- note = order.notes.first()- assert note.content == note_content---def test_user_cant_view_staff_order_notes(- order, authorized_client, staff_user):- note_content = 'bla-bla note'- note = models.OrderNote(- user=staff_user, content=note_content, order=order)- note.save()-- url = reverse('order:details', kwargs={'token': order.token})-- response = authorized_client.get(url)-- assert response.status_code == 200- assert note_content not in str(response.content)+ order.refresh_from_db()+ assert order.customer_note == customer_note