Skip to content

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

Swap Product field on Orderline for Variant

Pacu2b51833f97292
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 28d6f55b01..036188b129 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -392,7 +392,6 @@ def create_order_line(order):
variant.quantity_allocated += quantity
variant.save()
return order.lines.create(
- product=product,
product_name=product.name,
product_sku=variant.sku,
is_shipping_required=product.product_type.is_shipping_required,
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index 889a487884..306ec47e6d 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -248,9 +248,9 @@ def orderline_change_quantity(request, order_pk, line_pk):
if form.is_valid():
msg = pgettext_lazy(
'Dashboard message related to an order line',
- 'Changed quantity for product %(product)s from'
+ 'Changed quantity for variant %(variant)s from'
' %(old_quantity)s to %(new_quantity)s') % {
- 'product': line.product, 'old_quantity': old_quantity,
+ 'variant': line.variant, 'old_quantity': old_quantity,
'new_quantity': line.quantity}
with transaction.atomic():
form.save()
diff --git a/saleor/order/migrations/0044_auto_20180329_0142.py b/saleor/order/migrations/0044_auto_20180329_0142.py
index 927a3ce2df..4dfdc5d4c6 100644
--- a/saleor/order/migrations/0044_auto_20180329_0142.py
+++ b/saleor/order/migrations/0044_auto_20180329_0142.py
@@ -25,4 +25,8 @@ class Migration(migrations.Migration):
name='variant',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='product.ProductVariant'),
),
+ migrations.RemoveField(
+ model_name='orderline',
+ name='product',
+ ),
]
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 429d9aad1c..a6102b157a 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -17,7 +17,7 @@ from . import FulfillmentStatus, OrderStatus
from ..account.models import Address
from ..core.utils import ZERO_TAXED_MONEY, build_absolute_uri
from ..discount.models import Voucher
-from ..product.models import Product, ProductVariant
+from ..product.models import ProductVariant
from ..shipping.models import ShippingMethodCountry
@@ -177,9 +177,6 @@ class Order(models.Model):
class OrderLine(models.Model):
order = models.ForeignKey(
Order, related_name='lines', editable=False, on_delete=models.CASCADE)
- product = models.ForeignKey(
- Product, blank=True, null=True, related_name='+',
- on_delete=models.SET_NULL)
variant = models.ForeignKey(
ProductVariant, related_name='+', on_delete=models.SET_NULL,
blank=True, null=True)
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 94207c4708..36178d3c37 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -153,7 +153,6 @@ def add_variant_to_order(
else quantity_left
)
order.lines.create(
- product=variant.product,
product_name=variant.display_product(),
product_sku=variant.sku,
is_shipping_required=(
@@ -201,9 +200,8 @@ def merge_duplicates_into_order_line(line):
If there are no duplicates, nothing will happen.
"""
lines = line.order.lines.filter(
- product=line.product, product_name=line.product_name,
- product_sku=line.product_sku, variant=line.variant,
- is_shipping_required=line.is_shipping_required)
+ product_name=line.product_name, product_sku=line.product_sku,
+ variant=line.variant, is_shipping_required=line.is_shipping_required)
if lines.count() > 1:
line.quantity = sum([line.quantity for line in lines])
line.save(update_fields=['quantity'])
diff --git a/saleor/order/views.py b/saleor/order/views.py
index 2cfc546b6c..b71c4e3543 100644
--- a/saleor/order/views.py
+++ b/saleor/order/views.py
@@ -50,7 +50,8 @@ def details(request, token):
def payment(request, token):
orders = Order.objects.confirmed().filter(billing_address__isnull=False)
- orders = orders.prefetch_related('lines__product').select_related(
+ orders = Order.objects.prefetch_related('lines__variant')
+ orders = orders.select_related(
'billing_address', 'shipping_address', 'user')
order = get_object_or_404(orders, token=token)
payments = order.payments.all()
diff --git a/saleor/seo/schema/email.py b/saleor/seo/schema/email.py
index f8e3099158..ed82aeeeb0 100644
--- a/saleor/seo/schema/email.py
+++ b/saleor/seo/schema/email.py
@@ -28,7 +28,7 @@ def get_product_data(line, organization):
},
'seller': organization}
- product = line.product
+ product = line.variant.product
product_url = build_absolute_uri(product.get_absolute_url())
product_data['itemOffered']['url'] = product_url
@@ -59,7 +59,8 @@ def get_order_confirmation_markup(order):
'orderStatus': 'http://schema.org/OrderProcessing',
'orderDate': order.created}
- for line in order.lines.all():
+ lines = order.lines.prefetch_related('variant')
+ for line in lines:
product_data = get_product_data(line=line, organization=organization)
data['acceptedOffer'].append(product_data)
return json.dumps(data, cls=DjangoJSONEncoder)
diff --git a/templates/dashboard/order/detail.html b/templates/dashboard/order/detail.html
index 0167ad6926..39da25f901 100644
--- a/templates/dashboard/order/detail.html
+++ b/templates/dashboard/order/detail.html
@@ -294,8 +294,8 @@
{% for line in order %}
<tr class="last-no-border">
<td>
- {% if line.product %}
- <a href="{% url 'dashboard:product-detail' pk=line.product.pk %}">{{ line.product_name }}</a>
+ {% if line.variant.product %}
+ <a href="{% url 'dashboard:product-detail' pk=line.variant.product.pk %}">{{ line.product_name }}</a>
{% else %}
{{ line.product_name }}
{% endif %}
diff --git a/templates/dashboard/order/fulfillment.html b/templates/dashboard/order/fulfillment.html
index 51ce8bbaee..53d9103f93 100644
--- a/templates/dashboard/order/fulfillment.html
+++ b/templates/dashboard/order/fulfillment.html
@@ -76,8 +76,8 @@
{% for line in unfulfilled_lines %}
<tr>
<td>
- {% if line.product %}
- <a href="{% url 'dashboard:product-detail' pk=line.product.pk %}">{{ line.product_name }}</a>
+ {% if line.variant %}
+ <a href="{% url 'dashboard:variant-detail' pk=line.variant.pk %}">{{ line.product_name }}</a>
{% else %}
{{ line.product_name }}
{% endif %}
diff --git a/templates/dashboard/product/product_image/modal/confirm_delete.html b/templates/dashboard/product/product_image/modal/confirm_delete.html
index 9ad0c5bb05..91bbde2017 100644
--- a/templates/dashboard/product/product_image/modal/confirm_delete.html
+++ b/templates/dashboard/product/product_image/modal/confirm_delete.html
@@ -1,7 +1,6 @@
{% extends "dashboard/base_modal.html" %}
{% load i18n %}
-{% load product_first_image from product_images %}
{% block form_class %}{% endblock %}
diff --git a/templates/order/_ordered_items_table.html b/templates/order/_ordered_items_table.html
index bdbf51d20a..1b75a1ecd7 100644
--- a/templates/order/_ordered_items_table.html
+++ b/templates/order/_ordered_items_table.html
@@ -49,9 +49,9 @@
<div class="table__row order-details__product{% if forloop.last %} order-details__last-row{% endif %}">
<div class="row">
<div class="col-md-8">
- <a class="link--clean" href="{% if line.product %}{{ line.product.get_absolute_url }}{% endif %}">
- <img data-src="{% product_first_image line.product size="60x60" method="crop" %}"
- data-srcset="{% product_first_image line.product size="60x60" method="crop" %} 1x, {% product_first_image line.product size="120x120" method="crop" %} 2x"
+ <a class="link--clean" href="{% if line.variant %}{{ line.variant.get_absolute_url }}{% endif %}">
+ <img data-src="{% product_first_image line.variant.product size="60x60" method="crop" %}"
+ data-srcset="{% product_first_image line.variant.product size="60x60" method="crop" %} 1x, {% product_first_image line.variant.product size="120x120" method="crop" %} 2x"
class="float-left lazyload lazypreload"
src="{% placeholder size=60 %}">
<span class="order-details__product__description">{{ line.product_name }}</span>
@@ -139,7 +139,7 @@
<div class="table__row fulfillment__row order-details__product{% if forloop.last %} order-details__last-row{% endif %}">
<div class="row">
<div class="col-md-6 col-9">
- <a class="link--clean" href="{% if line.product %}{{ line.product.get_absolute_url }}{% endif %}">
+ <a class="link--clean" href="{% if line.variant.product %}{{ line.variant.product.get_absolute_url }}{% endif %}">
<span class="order-details__product__description">{{ line.order_line.product_name }}</span>
</a>
</div>
diff --git a/tests/conftest.py b/tests/conftest.py
index bc1251d587..fae5333c92 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -257,7 +257,7 @@ def product(product_type, default_category):
variant_attributes = {
smart_text(variant_attr.pk): smart_text(variant_attr_value.pk)}
- variant = ProductVariant.objects.create(
+ ProductVariant.objects.create(
product=product, sku='123', attributes=variant_attributes,
cost_price=Money('1.00', 'USD'), quantity=10, quantity_allocated=1)
return product
@@ -367,7 +367,53 @@ def voucher(db): # pylint: disable=W0613
@pytest.fixture()
-def order_with_lines(order, product_type, default_category, shipping_method):
+def order_with_lines(order, product_type, default_category):
+ product = Product.objects.create(
+ name='Test product', price=Money('10.00', 'USD'),
+ product_type=product_type, category=default_category)
+ variant = ProductVariant.objects.create(
+ product=product, sku='SKU_A', cost_price=Money(1, 'USD'), quantity=0,
+ quantity_allocated=0)
+ order.lines.create(
+ product_name=product.name,
+ product_sku='SKU_%d' % (product.pk,),
+ is_shipping_required=product.product_type.is_shipping_required,
+ quantity=1,
+ unit_price_net=Decimal('10.00'),
+ unit_price_gross=Decimal('10.00'),
+ variant=variant)
+ product = Product.objects.create(
+ name='Test product 2', price=Money('20.00', 'USD'),
+ product_type=product_type, category=default_category)
+ variant = ProductVariant.objects.create(
+ product=product, sku='SKU_B', cost_price=Money(2, 'USD'), quantity=0,
+ quantity_allocated=0)
+ order.lines.create(
+ product_name=product.name,
+ product_sku='SKU_%d' % (product.pk,),
+ is_shipping_required=product.product_type.is_shipping_required,
+ quantity=1,
+ unit_price_net=Decimal('20.00'),
+ unit_price_gross=Decimal('20.00'),
+ variant=variant)
+ product = Product.objects.create(
+ name='Test product 3', price=Money('30.00', 'USD'),
+ product_type=product_type, category=default_category)
+
+ order.lines.create(
+ product_name=product.name,
+ product_sku='SKU_%d' % (product.pk,),
+ is_shipping_required=product.product_type.is_shipping_required,
+ quantity=1,
+ unit_price_net=Decimal('30.00'),
+ unit_price_gross=Decimal('30.00'))
+
+ recalculate_order(order)
+ return order
+
+
+def order_with_lines_and_stock(order, product_type, default_category):
product = Product.objects.create(
name='Test product', price=Money('10.00', 'USD'),
product_type=product_type, category=default_category)
@@ -375,7 +421,7 @@ def order_with_lines(order, product_type, default_category, shipping_method):
product=product, sku='SKU_A', cost_price=Money(1, 'USD'), quantity=5,
quantity_allocated=3)
order.lines.create(
- product=product,
+ order=order,
product_name=product.name,
product_sku='SKU_A',
is_shipping_required=product.product_type.is_shipping_required,
@@ -390,7 +436,7 @@ def order_with_lines(order, product_type, default_category, shipping_method):
product=product, sku='SKU_B', cost_price=Money(2, 'USD'), quantity=2,
quantity_allocated=2)
order.lines.create(
- product=product,
+ order=order,
product_name=product.name,
product_sku='SKU_B',
is_shipping_required=product.product_type.is_shipping_required,
@@ -432,23 +478,6 @@ def draft_order(order_with_lines):
return order_with_lines
-def order_with_lines_and_stock(order_with_lines):
- line = OrderLine.objects.get(product_sku='SKU_A')
- variant = ProductVariant.objects.get(sku=line.product_sku)
- order_with_lines.lines.create(
- order=order,
- product=variant.product,
- product_name=variant.product.name,
- product_sku=line.product_sku,
- is_shipping_required=variant.product.product_type.is_shipping_required,
- quantity=2,
- unit_price_net=Decimal('30.00'),
- unit_price_gross=Decimal('30.00'),
- quantity_allocated=0)
- return order_with_lines
-
-
@pytest.fixture()
def payment_waiting(order_with_lines):
return order_with_lines.payments.create(
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index 1b65f04f5b..543be968f2 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -409,7 +409,7 @@ def test_view_cancel_order_line(admin_client, draft_order):
line = lines_before.first()
line_quantity = line.quantity
quantity_allocated_before = line.variant.quantity_allocated
- product = line.product
+ product = line.variant.product
url = reverse(
'dashboard:orderline-cancel', kwargs={
diff --git a/tests/test_asynchronous_tasks.py b/tests/test_asynchronous_tasks.py
index 89be58df0e..60c0533e1e 100644
--- a/tests/test_asynchronous_tasks.py
+++ b/tests/test_asynchronous_tasks.py
@@ -7,8 +7,9 @@ from saleor.order.emails import (
@pytest.mark.django_db
@pytest.mark.integration
def test_email_sending_asynchronously(
- transactional_db, celery_app, celery_worker, order_with_lines):
- order = send_order_confirmation.delay(order_with_lines.pk)
- payment = send_payment_confirmation.delay(order_with_lines.pk)
+ transactional_db, celery_app, celery_worker,
+ order_with_lines_and_stock):
+ order = send_order_confirmation.delay(order_with_lines_and_stock.pk)
+ payment = send_payment_confirmation.delay(order_with_lines_and_stock.pk)
order.get()
payment.get()
diff --git a/tests/test_order.py b/tests/test_order.py
index 3b9820e5cf..c5e99267ff 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -66,7 +66,7 @@ def test_add_variant_to_order_edits_line_for_existing_variant(
order_with_lines):
order = order_with_lines
existing_line = order.lines.first()
- variant = existing_line.product.variants.get()
+ variant = existing_line.variant
lines_before = order.lines.count()
line_quantity_before = existing_line.quantity
@@ -82,7 +82,6 @@ def test_add_variant_to_order_allocates_stock_for_existing_variant(
order_with_lines):
order = order_with_lines
existing_line = order.lines.first()
- variant = existing_line.product.variants.get()
variant = existing_line.variant
stock_before = variant.quantity_allocated
diff --git a/tests/test_seo_schema.py b/tests/test_seo_schema.py
index eac9b817b6..60f79d7cb9 100644
--- a/tests/test_seo_schema.py
+++ b/tests/test_seo_schema.py
@@ -16,28 +16,32 @@ def test_get_organization(site_settings):
assert result['name'] == example_name
-def test_get_product_data_without_image(order_with_lines):
+def test_get_product_data_without_image(order_with_lines_and_stock):
"""Tested OrderLine Product has no image assigned."""
- order_line = order_with_lines.lines.first()
+ order_line = order_with_lines_and_stock.lines.first()
organization = get_organization()
result = get_product_data(order_line, organization)
assert 'image' not in result['itemOffered']
-def test_get_product_data_with_image(order_with_lines, product_with_image):
- order_line = order_with_lines.lines.first()
- order_line.product = product_with_image
- order_line.product_name = product_with_image.name
+def test_get_product_data_with_image(
+ order_with_lines_and_stock, product_with_image):
+ order = order_with_lines_and_stock
+ order_line = order.lines.first()
+ variant = product_with_image.variants.first()
+ order_line.variant = variant
+ order_line.product_name = variant.display_product()
order_line.save()
organization = get_organization()
result = get_product_data(order_line, organization)
assert 'image' in result['itemOffered']
- assert result['itemOffered']['name'] == product_with_image.name
+ assert result['itemOffered']['name'] == variant.display_product()
-def test_get_order_confirmation_markup(order_with_lines):
+def test_get_order_confirmation_markup(order_with_lines_and_stock):
+ order = order_with_lines_and_stock
try:
- result = get_order_confirmation_markup(order_with_lines)
+ result = get_order_confirmation_markup(order)
except TypeError:
pytest.fail('Function output is not JSON serializable')