Skip to content

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

Move note from checkout to cart

Anna Siwikff6dbfe68ecf
diff --git a/saleor/cart/checkout/core.py b/saleor/cart/checkout/core.py
index 49a7ee0a35..df861655fb 100644
--- a/saleor/cart/checkout/core.py
+++ b/saleor/cart/checkout/core.py
@@ -7,7 +7,6 @@ from django.utils.encoding import smart_text
from django.utils.translation import get_language
from prices import Money
-from ...account.models import Address
from ...account.utils import store_user_address
from ...core import analytics
from ...core.utils.taxes import get_taxes_for_country
@@ -77,36 +76,6 @@ class Checkout:
self.storage = None
self.modified = True
- def _get_address_from_storage(self, key):
- address_data = self.storage.get(key)
- if address_data is not None and address_data.get('id'):
- try:
- return Address.objects.get(id=address_data['id'])
- except Address.DoesNotExist:
- return None
- elif address_data:
- return Address(**address_data)
- return None
-
- @property
- def email(self):
- """Return the customer email if any."""
- return self.storage.get('email')
-
- @email.setter
- def email(self, email):
- self.storage['email'] = email
- self.modified = True
-
- @property
- def note(self):
- return self.storage.get('note')
-
- @note.setter
- def note(self, note):
- self.storage['note'] = note
- self.modified = True
-
@property
def discount(self):
"""Return a discount if any."""
@@ -242,8 +211,8 @@ class Checkout:
if voucher is not None:
increase_voucher_usage(voucher)
- if self.note is not None and self.note:
- order.notes.create(user=order.user, content=self.note)
+ if self.cart.note:
+ order.notes.create(user=order.user, content=self.cart.note)
return order
diff --git a/saleor/cart/checkout/forms.py b/saleor/cart/checkout/forms.py
index 2e779dc42f..abcc26e736 100644
--- a/saleor/cart/checkout/forms.py
+++ b/saleor/cart/checkout/forms.py
@@ -127,16 +127,13 @@ class CartShippingMethodForm(forms.ModelForm):
method_field.empty_label = None
-class NoteForm(forms.Form):
- """Form to add a note to an order."""
+class CartNoteForm(forms.ModelForm):
+ """Save note in cart."""
note = forms.CharField(
- max_length=250, required=False, strip=True, label=False)
- note.widget = forms.Textarea({'rows': 3})
+ max_length=250, required=False, strip=True, label=False,
+ widget=forms.Textarea({'rows': 3}))
- def __init__(self, *args, **kwargs):
- self.checkout = kwargs.pop('checkout', None)
- super().__init__(*args, **kwargs)
-
- def set_checkout_note(self):
- self.checkout.note = self.cleaned_data.get('note', '')
+ class Meta:
+ model = Cart
+ fields = ['note']
diff --git a/saleor/cart/checkout/views/summary.py b/saleor/cart/checkout/views/summary.py
index 35ba753b6b..8cc77f8885 100644
--- a/saleor/cart/checkout/views/summary.py
+++ b/saleor/cart/checkout/views/summary.py
@@ -9,7 +9,7 @@ from ....core.exceptions import InsufficientStock
from ....order.emails import send_order_confirmation
from ..forms import (
AddressChoiceForm, AnonymousUserBillingForm, BillingAddressChoiceForm,
- NoteForm)
+ CartNoteForm)
from ..utils import get_checkout_data, save_billing_address_in_cart
@@ -93,9 +93,9 @@ def summary_with_shipping_view(request, cart, checkout):
Will create an order if all data is valid.
"""
- note_form = NoteForm(request.POST or None, checkout=checkout)
+ note_form = CartNoteForm(request.POST or None, instance=cart)
if note_form.is_valid():
- note_form.set_checkout_note()
+ note_form.save()
if request.user.is_authenticated:
additional_addresses = request.user.addresses.all()
@@ -126,9 +126,10 @@ def anonymous_summary_without_shipping(request, cart, checkout):
Will create an order if all data is valid.
"""
- note_form = NoteForm(request.POST or None, checkout=checkout)
+ note_form = CartNoteForm(request.POST or None, instance=cart)
if note_form.is_valid():
- note_form.set_checkout_note()
+ note_form.save()
+
user_form = AnonymousUserBillingForm(request.POST or None, instance=cart)
billing_address = cart.billing_address
if billing_address:
@@ -161,9 +162,9 @@ def summary_without_shipping(request, cart, checkout):
Will create an order if all data is valid.
"""
- note_form = NoteForm(request.POST or None, checkout=checkout)
+ note_form = CartNoteForm(request.POST or None, instance=cart)
if note_form.is_valid():
- note_form.set_checkout_note()
+ note_form.save()
billing_address = cart.billing_address
user_addresses = request.user.addresses.all()
diff --git a/saleor/cart/migrations/0007_merge_checkout_into_cart.py b/saleor/cart/migrations/0007_merge_checkout_into_cart.py
index 60232c1918..b4d5fd984c 100644
--- a/saleor/cart/migrations/0007_merge_checkout_into_cart.py
+++ b/saleor/cart/migrations/0007_merge_checkout_into_cart.py
@@ -1,4 +1,4 @@
-# Generated by Django 2.0.3 on 2018-05-21 12:00
+# Generated by Django 2.0.3 on 2018-05-22 16:00
from django.db import migrations, models
import django.db.models.deletion
@@ -37,4 +37,9 @@ class Migration(migrations.Migration):
model_name='cart',
name='checkout_data',
),
+ migrations.AddField(
+ model_name='cart',
+ name='note',
+ field=models.TextField(blank=True, default=''),
+ ),
]
diff --git a/saleor/cart/models.py b/saleor/cart/models.py
index ab1457ecf9..cffc68ce57 100644
--- a/saleor/cart/models.py
+++ b/saleor/cart/models.py
@@ -89,6 +89,7 @@ class Cart(models.Model):
shipping_method = models.ForeignKey(
ShippingMethodCountry, blank=True, null=True, related_name='carts',
on_delete=models.SET_NULL)
+ note = models.TextField(blank=True, default='')
objects = CartQueryset.as_manager()
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 69e46a3cce..ce78406a2b 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -6,7 +6,6 @@ from django.urls import reverse
from prices import Money, TaxedMoney
from saleor.cart.checkout import views
-from saleor.cart.checkout.forms import NoteForm
from saleor.cart.checkout.utils import get_voucher_discount_for_checkout
from saleor.cart.checkout.core import STORAGE_SESSION_KEY, Checkout
from saleor.core.exceptions import InsufficientStock
@@ -103,23 +102,15 @@ def test_checkout_taxes(checkout_with_items, shipping_method, vatlayer):
assert checkout_with_items.get_subtotal() == subtotal
[email protected]('note_value', [
- '',
- ' ',
- ' test_note ',
- 'test_note'])
-def test_note_form(checkout, note_value):
- form = NoteForm({'note': note_value}, checkout=checkout)
- form.is_valid()
- form.set_checkout_note()
- assert checkout.note == note_value.strip()
-
-
def test_note_in_created_order(checkout_with_items):
- checkout_with_items.note = ''
+ cart = checkout_with_items.cart
+ cart.note = ''
+ cart.save()
order = checkout_with_items.create_order()
assert not order.notes.all()
- checkout_with_items.note = 'test_note'
+
+ cart.note = 'test_note'
+ cart.save()
order = checkout_with_items.create_order()
assert order.notes.filter(content='test_note').exists()