Skip to content

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

Move checkout modification logic to NoteForm

akjanik44d8d0ed610b
diff --git a/saleor/checkout/forms.py b/saleor/checkout/forms.py
index 4bd5d22a1e..dcb0a4cb51 100644
--- a/saleor/checkout/forms.py
+++ b/saleor/checkout/forms.py
@@ -119,3 +119,10 @@ class NoteForm(forms.Form):
""" Form to add a note to an order as it is
created for shop staff to see """
note = forms.CharField(max_length=250, required=False)
+
+ def __init__(self, *args, **kwargs):
+ self.checkout = kwargs.pop('checkout', None)
+ super().__init__(*args, **kwargs)
+
+ def update_checkout(self):
+ self.checkout.note = self.cleaned_data.get('note', '')
diff --git a/saleor/checkout/views/summary.py b/saleor/checkout/views/summary.py
index 7f0cf8eef9..b7c203223a 100644
--- a/saleor/checkout/views/summary.py
+++ b/saleor/checkout/views/summary.py
@@ -93,9 +93,9 @@ def summary_with_shipping_view(request, checkout):
Will create an order if all data is valid.
"""
- note_form = NoteForm(request.POST or None)
+ note_form = NoteForm(request.POST or None, checkout=checkout)
if note_form.is_valid():
- checkout.note = note_form.cleaned_data['note']
+ note_form.update_checkout()
if request.user.is_authenticated:
additional_addresses = request.user.addresses.all()
@@ -121,9 +121,9 @@ def anonymous_summary_without_shipping(request, checkout):
Will create an order if all data is valid.
"""
- note_form = NoteForm(request.POST or None)
+ note_form = NoteForm(request.POST or None, checkout=checkout)
if note_form.is_valid():
- checkout.note = note_form.cleaned_data['note']
+ note_form.update_checkout()
user_form = AnonymousUserBillingForm(
request.POST or None, initial={'email': checkout.email})
billing_address = checkout.billing_address
@@ -151,9 +151,9 @@ def summary_without_shipping(request, checkout):
Will create an order if all data is valid.
"""
- note_form = NoteForm(request.POST or None)
+ note_form = NoteForm(request.POST or None, checkout=checkout)
if note_form.is_valid():
- checkout.note = note_form.cleaned_data['note']
+ note_form.update_checkout()
billing_address = checkout.billing_address
user_addresses = request.user.addresses.all()
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 052450718b..502aa13362 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -2,6 +2,7 @@ from unittest.mock import MagicMock, Mock, patch
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
+from django_countries.fields import Country
from django.test.client import RequestFactory
from prices import Price
import pytest
@@ -9,13 +10,14 @@ from satchless.item import InsufficientStock
from saleor.checkout import views
from saleor.checkout.core import STORAGE_SESSION_KEY, Checkout
+from saleor.checkout.forms import NoteForm
from saleor.checkout.views.summary import (
anonymous_summary_without_shipping,
summary_with_shipping_view,
summary_without_shipping)
from saleor.shipping.models import ShippingMethodCountry
-from saleor.userprofile.models import Address
+from saleor.userprofile.models import Address, User
def test_checkout_version():
@@ -226,29 +228,11 @@ def test_checkout_create_order_insufficient_stock(
checkout.create_order()
- summary_without_shipping,
- summary_with_shipping_view,
- anonymous_summary_without_shipping,
-])
-@patch('saleor.checkout.views.summary.get_address_form',
- return_value=(Mock(), Mock()))
-@patch('saleor.checkout.views.summary.BillingWithoutShippingAddressForm')
-@patch('saleor.checkout.views.summary.get_billing_forms_with_shipping',
- return_value=(Mock(), Mock(), None))
-def test_note_ins_summary_views(
- mock_get_billing_forms_with_shipping,
- mock_BillingWithoutShippingAddressForm, mock_get_address_form, view):
- checkout = Checkout(Mock(), Mock(
- default_billing_address=Mock(),
- addresses=Mock(is_authenticated=Mock(return_value=True))),
- 'tracking_code')
- rf = RequestFactory()
- request = rf.post('/test/', {'note': 'test_note'})
- request.user = Mock()
- request.country = Mock()
- mock_BillingWithoutShippingAddressForm().is_valid.return_value = False
- view(request, checkout)
+def test_note_form():
+ checkout = Checkout(Mock(), AnonymousUser(), 'tracking_code')
+ form = NoteForm({'note': 'test_note'}, checkout=checkout)
+ form.is_valid()
+ form.update_checkout()
assert checkout.note == 'test_note'