Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
/r/item/line in saler/order
akjanik7eec45c30433
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.pyindex cd857f88a2..af5b43a1b2 100644--- a/saleor/checkout/core.py+++ b/saleor/checkout/core.py@@ -16,7 +16,7 @@ from ..cart.utils import get_or_empty_db_cartfrom ..core import analyticsfrom ..discount.models import NotApplicable, Voucherfrom ..order.models import Order-from ..order.utils import add_items_to_delivery_group+from ..order.utils import create_order_lines_in_delivery_groupfrom ..shipping.models import ANY_COUNTRY, ShippingMethodCountryfrom ..userprofile.models import Addressfrom ..userprofile.utils import store_user_address@@ -331,7 +331,7 @@ class Checkout(object):group = order.groups.create(shipping_price=shipping_price,shipping_method_name=shipping_method_name)- add_items_to_delivery_group(+ create_order_lines_in_delivery_group(group, partition, discounts=self.cart.discounts)if voucher is not None:diff --git a/saleor/order/models.py b/saleor/order/models.pyindex 2302d32c01..81941eb51d 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -114,7 +114,7 @@ class Order(models.Model, ItemSet):self.status = statusself.save()- def get_items(self):+ def get_lines(self):return OrderLine.objects.filter(delivery_group__order=self)def is_fully_paid(self):@@ -206,7 +206,7 @@ class Order(models.Model, ItemSet):self.total_tax = Price(price.tax, currency=price.currency)def get_subtotal_without_voucher(self):- if self.get_items():+ if self.get_lines():return super(Order, self).get_total()return Price(net=0, currency=settings.DEFAULT_CURRENCY)@@ -313,7 +313,7 @@ class OrderLineManager(models.Manager):line.delete()if not source_group.get_total_quantity() or force:source_group.delete()- if not order.get_items():+ if not order.get_lines():order.change_status(OrderStatus.CANCELLED)order.create_history_entry(status=OrderStatus.CANCELLED, comment=pgettext_lazy(@@ -403,23 +403,23 @@ class Payment(BasePayment):emails.send_payment_confirmation.delay(email, order_url)def get_purchased_items(self):- items = [+ lines = [PurchasedItem(- name=item.product_name, sku=item.product_sku,- quantity=item.quantity,- price=item.unit_price_gross.quantize(Decimal('0.01')),+ name=line.product_name, sku=line.product_sku,+ quantity=line.quantity,+ price=line.unit_price_gross.quantize(Decimal('0.01')),currency=settings.DEFAULT_CURRENCY)- for item in self.order.get_items()]+ for line in self.order.get_lines()]voucher = self.order.voucherif voucher is not None:- items.append(PurchasedItem(+ lines.append(PurchasedItem(name=self.order.discount_name,sku='DISCOUNT',quantity=1,price=-self.order.discount_amount.net,currency=self.currency))- return items+ return linesdef get_total_price(self):net = self.total - self.taxdiff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 2a3925210e..1be2340022 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -53,11 +53,12 @@ def attach_order_to_user(order, user):order.save(update_fields=['user'])-def add_items_to_delivery_group(delivery_group, partition, discounts=None):- for item_line in partition:- product_variant = item_line.variant- price = item_line.get_price_per_item(discounts)- total_quantity = item_line.get_quantity()+def create_order_lines_in_delivery_group(+ delivery_group, partition, discounts=None):+ for order_line in partition:+ product_variant = order_line.variant+ price = order_line.get_price_per_item(discounts)+ total_quantity = order_line.get_quantity()while total_quantity > 0:stock = product_variant.select_stockrecord()@@ -106,17 +107,17 @@ def cancel_order(order):order.save()-def merge_duplicated_lines(item):- """ Merges duplicated items in delivery group into one (given) item.+def merge_duplicated_lines(line):+ """ Merges duplicated lines in delivery group into one (given) line.If there are no duplicates, nothing will happen."""- lines = item.delivery_group.items.filter(- product=item.product, product_name=item.product_name,- product_sku=item.product_sku, stock=item.stock)+ lines = line.delivery_group.items.filter(+ product=line.product, product_name=line.product_name,+ product_sku=line.product_sku, stock=line.stock)if lines.count() > 1:- item.quantity = sum([line.quantity for line in lines])- item.save()- lines.exclude(pk=item.pk).delete()+ line.quantity = sum([line.quantity for line in lines])+ line.save()+ lines.exclude(pk=line.pk).delete()def change_order_line_quantity(line, new_quantity):@@ -128,7 +129,7 @@ def change_order_line_quantity(line, new_quantity):line.delivery_group.delete()order = line.delivery_group.order- if not order.get_items():+ if not order.get_lines():order.change_status(OrderStatus.CANCELLED)order.create_history_entry(status=OrderStatus.CANCELLED, comment=pgettext_lazy(diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex 15166ba74f..1b54a932bd 100644--- a/tests/dashboard/test_order.py+++ b/tests/dashboard/test_order.py@@ -9,7 +9,7 @@ from saleor.dashboard.order.forms import ChangeQuantityForm, MoveItemsFormfrom saleor.order.models import (DeliveryGroup, Order, OrderLine, OrderHistoryEntry)from saleor.order.utils import (- add_items_to_delivery_group, change_order_line_quantity)+ create_order_lines_in_delivery_group, change_order_line_quantity)from saleor.product.models import ProductVariant, Stock, StockLocationfrom tests.utils import get_redirect_location, get_url_path@@ -17,7 +17,7 @@ from tests.utils import get_redirect_location, get_url_path@pytest.mark.integration@pytest.mark.django_dbdef test_view_cancel_order_line(admin_client, order_with_items_and_stock):- lines_before = order_with_items_and_stock.get_items()+ lines_before = order_with_items_and_stock.get_lines()lines_before_count = lines_before.count()line = lines_before.first()line_quantity = line.quantity@@ -36,7 +36,7 @@ def test_view_cancel_order_line(admin_client, order_with_items_and_stock):assert get_redirect_location(response) == reverse('dashboard:order-details', args=[order_with_items_and_stock.pk])# check ordered item removal- lines_after = Order.objects.get().get_items()+ lines_after = Order.objects.get().get_lines()assert lines_before_count - 1 == lines_after.count()# check stock deallocationassert Stock.objects.first().quantity_allocated == quantity_allocated_before - line_quantity@@ -50,7 +50,7 @@ def test_view_cancel_order_line(admin_client, order_with_items_and_stock):response = admin_client.post(url, {'csrfmiddlewaretoken': 'hello'}, follow=True)# check delivery group removal if it becomes empty- assert Order.objects.get().get_items().count() == 0+ assert Order.objects.get().get_lines().count() == 0assert DeliveryGroup.objects.count() == 0# check success messages after redirectassert response.context['messages']@@ -63,7 +63,7 @@ def test_view_change_order_line_quantity(admin_client, order_with_items_and_stocuser goes to order details pageuser selects first order line with quantity 3 and changes it to 2"""- lines_before_quantity_change = order_with_items_and_stock.get_items()+ lines_before_quantity_change = order_with_items_and_stock.get_lines()lines_before_quantity_change_count = lines_before_quantity_change.count()line = lines_before_quantity_change.first()line_quantity_before_quantity_change = line.quantity@@ -84,7 +84,7 @@ def test_view_change_order_line_quantity(admin_client, order_with_items_and_stocargs=[order_with_items_and_stock.pk])# success messages should appear after redirectassert response.context['messages']- lines_after = Order.objects.get().get_items()+ lines_after = Order.objects.get().get_lines()# order should have the same linesassert lines_before_quantity_change_count == lines_after.count()# stock allocation should be 2 now@@ -113,7 +113,7 @@ def test_view_change_order_line_quantity_with_invalid_data(user selects the first order line with quantity 3 and try to change it to 0 and -1user gets an error."""- lines = order_with_items_and_stock.get_items()+ lines = order_with_items_and_stock.get_lines()line = lines.first()url = reverse('dashboard:orderline-change-quantity', kwargs={@@ -127,7 +127,7 @@ def test_view_change_order_line_quantity_with_invalid_data(def test_dashboard_change_quantity_form(request_cart_with_item, order):cart = request_cart_with_itemgroup = DeliveryGroup.objects.create(order=order)- add_items_to_delivery_group(group, cart.lines.all())+ create_order_lines_in_delivery_group(group, cart.lines.all())order_line = group.items.get()# Check max quantity validation@@ -178,7 +178,7 @@ def test_view_split_order_line(admin_client, order_with_items_and_stock):user selects the line from the new shipment and moves all itemsback to the first shipment"""- lines_before_split = order_with_items_and_stock.get_items()+ lines_before_split = order_with_items_and_stock.get_lines()lines_before_split_count = lines_before_split.count()line = lines_before_split.first()line_quantity_before_split = line.quantity@@ -203,7 +203,7 @@ def test_view_split_order_line(admin_client, order_with_items_and_stock):args=[order_with_items_and_stock.pk])# success messages should appear after redirectassert response.context['messages']- lines_after = Order.objects.get().get_items()+ lines_after = Order.objects.get().get_lines()# order should have one more lineassert lines_before_split_count + 1 == lines_after.count()# stock allocation should not be changed@@ -241,7 +241,7 @@ def test_view_split_order_line(admin_client, order_with_items_and_stock):# the new shipment should be removedassert order_with_items_and_stock.groups.count() == 1# the related order line should be removed- assert lines_before_split_count == Order.objects.get().get_items().count()+ assert lines_before_split_count == Order.objects.get().get_lines().count()line.refresh_from_db()# the initial line should get the quantity restored to its initial valueassert line_quantity_before_split == line.quantity@@ -256,7 +256,7 @@ def test_view_split_order_line_with_invalid_data(admin_client, order_with_items_user selects first order line with quantity 3 and try move 0 and 4 items to a new shipmentuser gets an error and no delivery groups are created."""- lines = order_with_items_and_stock.get_items()+ lines = order_with_items_and_stock.get_lines()line = lines.first()url = reverse('dashboard:orderline-split', kwargs={@@ -344,7 +344,7 @@ def test_view_order_packing_slips(def test_view_change_order_line_stock_valid(admin_client, order_with_items_and_stock):order = order_with_items_and_stock- line = order.get_items().last()+ line = order.get_lines().last()old_stock = line.stockvariant = ProductVariant.objects.get(sku=line.product_sku)stock_location = StockLocation.objects.create(name='Warehouse 2')@@ -375,7 +375,7 @@ def test_view_change_order_line_stock_valid(def test_view_change_order_line_stock_insufficient_stock(admin_client, order_with_items_and_stock):order = order_with_items_and_stock- line = order.get_items().last()+ line = order.get_lines().last()old_stock = line.stockvariant = ProductVariant.objects.get(sku=line.product_sku)stock_location = StockLocation.objects.create(name='Warehouse 2')@@ -406,7 +406,7 @@ def test_view_change_order_line_stock_insufficient_stock(def test_view_change_order_line_stock_merges_lines(admin_client, order_with_items_and_stock):order = order_with_items_and_stock- line = order.get_items().first()+ line = order.get_lines().first()group = line.delivery_groupold_stock = line.stockvariant = ProductVariant.objects.get(sku=line.product_sku)diff --git a/tests/test_order.py b/tests/test_order.pyindex 178fe9e71a..3f91832adb 100644--- a/tests/test_order.py+++ b/tests/test_order.py@@ -2,7 +2,7 @@ from prices import Pricefrom saleor.cart.models import Cartfrom saleor.order import models-from saleor.order.utils import add_items_to_delivery_group+from saleor.order.utils import create_order_lines_in_delivery_groupdef test_total_property():@@ -32,7 +32,7 @@ def test_stock_allocation(billing_address, product_in_stock):cart.add(variant, quantity=2)order = models.Order.objects.create(billing_address=billing_address)delivery_group = models.DeliveryGroup.objects.create(order=order)- add_items_to_delivery_group(delivery_group, cart.lines.all())+ create_order_lines_in_delivery_group(delivery_group, cart.lines.all())order_line = delivery_group.items.get()stock = order_line.stockassert stock.quantity_allocated == 2@@ -41,7 +41,7 @@ def test_stock_allocation(billing_address, product_in_stock):def test_order_discount(sale, order, request_cart_with_item):cart = request_cart_with_itemgroup = models.DeliveryGroup.objects.create(order=order)- add_items_to_delivery_group(+ create_order_lines_in_delivery_group(group, cart.lines.all(), discounts=cart.discounts)item = group.items.first()assert item.get_price_per_item() == Price(currency="USD", net=5)diff --git a/tests/test_payment.py b/tests/test_payment.pyindex a4c7c59845..7cb9c7417b 100644--- a/tests/test_payment.py+++ b/tests/test_payment.py@@ -8,9 +8,9 @@ def test_get_purchased_items(order_with_items, settings, voucher):payment = Payment.objects.create(order=order_with_items, variant='paypal')discount = Price('10.0', currency=settings.DEFAULT_CURRENCY)- assert len(payment.get_purchased_items()) == len(order_with_items.get_items())+ assert len(payment.get_purchased_items()) == len(order_with_items.get_lines())- for p, o in zip(payment.get_purchased_items(), order_with_items.get_items()):+ for p, o in zip(payment.get_purchased_items(), order_with_items.get_lines()):assert p.sku == o.product_skuassert p.quantity == o.quantity@@ -21,9 +21,9 @@ def test_get_purchased_items(order_with_items, settings, voucher):settings.PAYMENT_VARIANTS = { 'paypal': ('PaypalProvider', {})}- assert len(payment.get_purchased_items()) == len(order_with_items.get_items()) + 1+ assert len(payment.get_purchased_items()) == len(order_with_items.get_lines()) + 1- for p, o in zip(payment.get_purchased_items()[:-1], order_with_items.get_items()):+ for p, o in zip(payment.get_purchased_items()[:-1], order_with_items.get_lines()):assert p.sku == o.product_skuassert p.quantity == o.quantity