Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use get_or_create for creating orders
Marcin Gębala6cefaabdd607
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex cea0ea91d6..09a074ff86 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -866,6 +866,10 @@ def create_order(cart, tracking_code, discounts, taxes):Current user's language is saved in the order so we can later determinewhich language to use when sending email."""+ order = Order.objects.filter(checkout_token=cart.token).first()+ if order is not None:+ return order+# FIXME: save locale along with the languagetry:order_data = _process_voucher_data_for_order(cart)@@ -879,15 +883,15 @@ def create_order(cart, tracking_code, discounts, taxes):'tracking_client_id': tracking_code,'total': cart.get_total(discounts, taxes)})- order = Order.objects.create(**order_data)-+ order = Order.objects.create(**order_data, checkout_token=cart.token)_fill_order_with_cart_data(order, cart, discounts, taxes)return orderdef is_fully_paid(cart: Cart, taxes, discounts):"""Check if checkout is fully paid."""- payments = cart.payments.filter(is_active=True)+ payments = [+ payment for payment in cart.payments.all() if payment.is_active]total_paid = sum([p.total for p in payments])cart_total = cart.get_total(discounts=discounts, taxes=taxes).gross.amountreturn total_paid >= cart_totaldiff --git a/saleor/order/migrations/0068_order_checkout_token.py b/saleor/order/migrations/0068_order_checkout_token.pynew file mode 100644index 0000000000..ec55536161--- /dev/null+++ b/saleor/order/migrations/0068_order_checkout_token.py@@ -0,0 +1,18 @@+# Generated by Django 2.1.5 on 2019-02-11 16:30++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ('order', '0067_auto_20181102_1054'),+ ]++ operations = [+ migrations.AddField(+ model_name='order',+ name='checkout_token',+ field=models.CharField(blank=True, max_length=36),+ ),+ ]diff --git a/saleor/order/models.py b/saleor/order/models.pyindex 5d27dc44b2..d73dd58714 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -6,7 +6,7 @@ from django.conf import settingsfrom django.contrib.postgres.fields import JSONFieldfrom django.core.validators import MinValueValidatorfrom django.db import models-from django.db.models import ExpressionWrapper, F, Max, Sum+from django.db.models import F, Max, Sumfrom django.urls import reversefrom django.utils.timezone import nowfrom django.utils.translation import pgettext_lazy@@ -96,6 +96,8 @@ class Order(models.Model):shipping_method_name = models.CharField(max_length=255, null=True, default=None, blank=True, editable=False)token = models.CharField(max_length=36, unique=True, blank=True)+ # Token of a checkout instance that this order was created from+ checkout_token = models.CharField(max_length=36, blank=True)total_net = MoneyField(currency=settings.DEFAULT_CURRENCY,max_digits=settings.DEFAULT_MAX_DIGITS,diff --git a/tests/test_checkout.py b/tests/test_checkout.pyindex e2408ebef6..ebd6336fe5 100644--- a/tests/test_checkout.py+++ b/tests/test_checkout.py@@ -521,6 +521,7 @@ def test_create_order_insufficient_stock(add_variant_to_cart(request_cart, variant, 10, check_quantity=False)request_cart.user = customer_userrequest_cart.billing_address = customer_user.default_billing_address+ cart.shipping_address = customer_user.default_billing_addressrequest_cart.save()with pytest.raises(InsufficientStock):@@ -528,6 +529,21 @@ def test_create_order_insufficient_stock(request_cart, 'tracking_code', discounts=None, taxes=None)+def test_create_order_doesnt_duplicate_order(+ cart_with_item, customer_user, shipping_method):+ cart = cart_with_item+ cart.user = customer_user+ cart.billing_address = customer_user.default_billing_address+ cart.shipping_address = customer_user.default_billing_address+ cart.shipping_method = shipping_method+ cart.save()++ order_1 = create_order(cart, tracking_code='', discounts=None, taxes=None)+ assert order_1.checkout_token == cart_with_item.token+ order_2 = create_order(cart, tracking_code='', discounts=None, taxes=None)+ assert order_1.pk == order_2.pk++def test_note_in_created_order(request_cart_with_item, address):request_cart_with_item.shipping_address = addressrequest_cart_with_item.note = 'test_note'