Skip to content

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

Fix review remarks

Marcin Gębalafb37a3107644
diff --git a/saleor/checkout/models.py b/saleor/checkout/models.py
index c39b7ab5f8..cdd70c62f9 100644
--- a/saleor/checkout/models.py
+++ b/saleor/checkout/models.py
@@ -14,6 +14,7 @@ from measurement.measures import Weight
from ..account.models import Address
from ..core.utils.taxes import ZERO_TAXED_MONEY, zero_money
from ..shipping.models import ShippingMethod
+from ..core.weight import zero_weight
CENTS = Decimal('0.01')
@@ -102,7 +103,7 @@ class Cart(models.Model):
def get_total_weight(self):
# Cannot use `sum` as it parses an empty Weight to an int
- weights = Weight(kg=0)
+ weights = zero_weight()
for line in self:
weights += line.variant.get_weight() * line.quantity
return weights
diff --git a/saleor/core/weight.py b/saleor/core/weight.py
index de730bad83..e91822406f 100644
--- a/saleor/core/weight.py
+++ b/saleor/core/weight.py
@@ -42,7 +42,7 @@ WeightUnitsEnum = Enum(
def zero_weight():
- """Function used as a model's default."""
+ """Represent the zero weight value."""
return Weight(kg=0)
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index c4dff3dec1..eac66ad1e4 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -82,19 +82,13 @@ def recalculate_order(order, **kwargs):
def recalculate_order_weight(order):
- """Recalculate order weights.
-
- It removes lines with non-existing variants.
- """
- lines_to_remove = order.lines.filter(variant=None)
- if lines_to_remove.exists():
- lines_to_remove.delete()
-
+ """Recalculate order weights."""
weight = zero_weight()
for line in order:
- weight += line.variant.get_weight() * line.quantity
+ if line.variant:
+ weight += line.variant.get_weight() * line.quantity
order.weight = weight
- order.save()
+ order.save(update_fields=['weight'])
def update_order_prices(order, discounts):
@@ -181,6 +175,7 @@ def attach_order_to_user(order, user):
order.save(update_fields=['user'])
def add_variant_to_order(
order,
variant,
@@ -199,29 +194,25 @@ def add_variant_to_order(
if not allow_overselling:
variant.check_quantity(quantity)
- order.weight += variant.get_weight() * quantity
try:
line = order.lines.get(variant=variant)
line.quantity += quantity
- with transaction.atomic():
- line.save(update_fields=['quantity'])
- order.save(update_fields=['weight'])
+ line.save(update_fields=['quantity'])
except OrderLine.DoesNotExist:
product_name = variant.display_product()
translated_product_name = variant.display_product(translated=True)
if translated_product_name == product_name:
translated_product_name = ''
- with transaction.atomic():
- line = order.lines.create(
- product_name=product_name,
- translated_product_name=translated_product_name,
- product_sku=variant.sku,
- is_shipping_required=variant.is_shipping_required(),
- quantity=quantity,
- variant=variant,
- unit_price=variant.get_price(discounts, taxes),
- tax_rate=get_tax_rate_by_name(variant.product.tax_rate, taxes))
- order.save(update_fields=['weight'])
+ line = order.lines.create(
+ product_name=product_name,
+ translated_product_name=translated_product_name,
+ product_sku=variant.sku,
+ is_shipping_required=variant.is_shipping_required(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=variant.get_price(discounts, taxes),
+ tax_rate=get_tax_rate_by_name(variant.product.tax_rate, taxes))
+ order.save(update_fields=['weight'])
if variant.track_inventory and track_inventory:
allocate_stock(variant, quantity)
@@ -238,7 +229,7 @@ def change_order_line_quantity(line, new_quantity):
def delete_order_line(line):
- """Delete order line from order"""
+ """Delete an order line from an order."""
line.delete()
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index 752f30deb4..72a8ab3fca 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -673,25 +673,6 @@ def test_draft_order_line_update(
assert data['errors'][0]['field'] == 'quantity'
-def test_draft_order_update_with_non_existing_product(
- draft_order, staff_api_client, permission_manage_orders):
- # To keep data integrity, delete broken lines from draft order
- query = DRAFT_ORDER_LINE_UPDATE_MUTATION
- order = draft_order
- line = order.lines.first()
- variant = line.variant
- variant.delete()
- line_id = graphene.Node.to_global_id('OrderLine', line.id)
- variables = {'lineId': line_id, 'quantity': 2}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_orders])
- content = get_graphql_content(response)
- data = content['data']['draftOrderLineUpdate']
- assert not data['errors']
- with pytest.raises(line._meta.model.DoesNotExist):
- line.refresh_from_db()
-
-
def test_require_draft_order_when_updating_lines(
order_with_lines, staff_api_client, permission_manage_orders):
query = DRAFT_ORDER_LINE_UPDATE_MUTATION