Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move checkout modification logic to NoteForm
akjanik44d8d0ed610b
diff --git a/saleor/checkout/forms.py b/saleor/checkout/forms.pyindex 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 iscreated 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.pyindex 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_addressuser_addresses = request.user.addresses.all()diff --git a/tests/test_checkout.py b/tests/test_checkout.pyindex 052450718b..502aa13362 100644--- a/tests/test_checkout.py+++ b/tests/test_checkout.py@@ -2,6 +2,7 @@ from unittest.mock import MagicMock, Mock, patchfrom django.conf import settingsfrom django.contrib.auth.models import AnonymousUser+from django_countries.fields import Countryfrom django.test.client import RequestFactoryfrom prices import Priceimport pytest@@ -9,13 +10,14 @@ from satchless.item import InsufficientStockfrom saleor.checkout import viewsfrom saleor.checkout.core import STORAGE_SESSION_KEY, Checkout+from saleor.checkout.forms import NoteFormfrom 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, Userdef test_checkout_version():@@ -226,29 +228,11 @@ def test_checkout_create_order_insufficient_stock(checkout.create_order()[email protected]('view', [- 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'