Skip to content

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

Refactor get_checkout_data functions

Michał Jelonek171ffa26216c
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 671e06dd28..619410140c 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -237,32 +237,6 @@ def get_or_empty_db_checkout(checkout_queryset=Checkout.objects.all()):
return get_checkout
-def get_checkout_data(checkout, shipping_range, currency, discounts, taxes):
- """Return a JSON-serializable representation of the checkout."""
- checkout_total = None
- local_checkout_total = None
- shipping_required = False
- total_with_shipping = None
- local_total_with_shipping = None
- if checkout:
- checkout_total = checkout.get_subtotal(discounts, taxes)
- local_checkout_total = to_local_currency(checkout_total, currency)
- shipping_required = checkout.is_shipping_required()
- total_with_shipping = TaxedMoneyRange(
- start=checkout_total, stop=checkout_total)
- if shipping_required and shipping_range:
- total_with_shipping = shipping_range + checkout_total
- local_total_with_shipping = to_local_currency(
- total_with_shipping, currency)
-
- return {
- 'checkout_total': checkout_total,
- 'local_checkout_total': local_checkout_total,
- 'shipping_required': shipping_required,
- 'total_with_shipping': total_with_shipping,
- 'local_total_with_shipping': local_total_with_shipping}
-
-
def find_open_checkout_for_user(user):
"""Find an open checkout for the given user."""
checkouts = user.checkouts.all()
@@ -608,19 +582,38 @@ def change_shipping_address_in_checkout(checkout, address):
checkout.save(update_fields=['shipping_address'])
-def get_checkout_data_for_checkout(checkout, discounts, taxes):
+def get_checkout_context(checkout, discounts, taxes, currency=None, shipping_range=None):
"""Data shared between views in checkout process."""
- lines = [(line, line.get_total(discounts, taxes)) for line in checkout]
- subtotal = checkout.get_subtotal(discounts, taxes)
- total = checkout.get_total(discounts, taxes)
- shipping_price = checkout.get_shipping_price(taxes)
- return {
+ checkout_total = checkout.get_total(discounts, taxes)
+ checkout_subtotal = checkout.get_subtotal(discounts, taxes)
+ shipping_required = checkout.is_shipping_required()
+ checkout_subtotal = checkout.get_subtotal(discounts, taxes)
+ total_with_shipping = TaxedMoneyRange(
+ start=checkout_subtotal, stop=checkout_subtotal)
+ if shipping_required and shipping_range:
+ total_with_shipping = shipping_range + checkout_subtotal
+
+ context = {
'checkout': checkout,
'checkout_are_taxes_handled': bool(taxes),
- 'checkout_lines': lines,
- 'checkout_shipping_price': shipping_price,
- 'checkout_subtotal': subtotal,
- 'checkout_total': total}
+ 'checkout_lines': [
+ (line, line.get_total(discounts, taxes)) for line in checkout],
+ 'checkout_shipping_price': checkout.get_shipping_price(taxes),
+ 'checkout_subtotal': checkout_subtotal,
+ 'checkout_total': checkout_total,
+ 'shipping_required': checkout.is_shipping_required(),
+ 'total_with_shipping': total_with_shipping}
+
+ if currency:
+ context.update(
+ local_checkout_total=to_local_currency(
+ checkout_total, currency),
+ local_checkout_subtotal=to_local_currency(
+ checkout_subtotal, currency),
+ local_total_with_shipping=to_local_currency(
+ total_with_shipping, currency))
+
+ return context
def _get_shipping_voucher_discount_for_checkout(voucher, checkout):
diff --git a/saleor/checkout/views/__init__.py b/saleor/checkout/views/__init__.py
index 5d98e724c5..11e501385e 100644
--- a/saleor/checkout/views/__init__.py
+++ b/saleor/checkout/views/__init__.py
@@ -12,9 +12,9 @@ from ..forms import (
CheckoutShippingMethodForm, CountryForm, ReplaceCheckoutLineForm)
from ..models import Checkout
from ..utils import (
- check_product_availability_and_warn, get_checkout_data,
- get_checkout_data_for_checkout, get_or_empty_db_checkout,
- get_taxes_for_checkout, is_valid_shipping_method, update_checkout_quantity)
+ check_product_availability_and_warn, get_checkout_context,
+ get_or_empty_db_checkout, get_taxes_for_checkout, is_valid_shipping_method,
+ update_checkout_quantity)
from .discount import add_voucher_form, validate_voucher
from .shipping import (
anonymous_user_shipping_address_view, user_shipping_address_view)
@@ -75,7 +75,7 @@ def checkout_shipping_method(request, checkout):
form.save()
return redirect('checkout:summary')
- ctx = get_checkout_data_for_checkout(checkout, discounts, taxes)
+ ctx = get_checkout_context(checkout, discounts, taxes)
ctx.update({'shipping_method_form': form})
return TemplateResponse(request, 'checkout/shipping_method.html', ctx)
@@ -135,15 +135,15 @@ def checkout_index(request, checkout):
weight=checkout.get_total_weight(), country_code=default_country,
taxes=taxes)
- checkout_data = get_checkout_data(
- checkout, shipping_price_range, request.currency, discounts, taxes)
- ctx = {
+ context = get_checkout_context(
+ checkout, discounts, taxes,
+ currency=request.currency, shipping_range=shipping_price_range)
+ context.update({
'checkout_lines': checkout_lines,
'country_form': country_form,
- 'shipping_price_range': shipping_price_range}
- ctx.update(checkout_data)
+ 'shipping_price_range': shipping_price_range})
- return TemplateResponse(request, 'checkout/index.html', ctx)
+ return TemplateResponse(request, 'checkout/index.html', context)
@get_or_empty_db_checkout(checkout_queryset=Checkout.objects.for_display())
@@ -159,9 +159,9 @@ def checkout_shipping_options(request, checkout):
ctx = {
'shipping_price_range': shipping_price_range,
'country_form': country_form}
- checkout_data = get_checkout_data(
- checkout, shipping_price_range, request.currency, request.discounts,
- request.taxes)
+ checkout_data = get_checkout_context(
+ checkout, request.discounts,request.taxes,
+ currency=request.currency, shipping_range=shipping_price_range)
ctx.update(checkout_data)
return TemplateResponse(request, 'checkout/_subtotal_table.html', ctx)
diff --git a/saleor/checkout/views/shipping.py b/saleor/checkout/views/shipping.py
index 0fae66c901..33dd91cb5e 100644
--- a/saleor/checkout/views/shipping.py
+++ b/saleor/checkout/views/shipping.py
@@ -2,7 +2,7 @@ from django.shortcuts import redirect
from django.template.response import TemplateResponse
from ..utils import (
- get_checkout_data_for_checkout, get_taxes_for_checkout,
+ get_checkout_context, get_taxes_for_checkout,
update_shipping_address_in_anonymous_checkout,
update_shipping_address_in_checkout)
@@ -17,7 +17,7 @@ def anonymous_user_shipping_address_view(request, checkout):
return redirect('checkout:shipping-method')
taxes = get_taxes_for_checkout(checkout, request.taxes)
- ctx = get_checkout_data_for_checkout(checkout, request.discounts, taxes)
+ ctx = get_checkout_context(checkout, request.discounts, taxes)
ctx.update({
'address_form': address_form,
'user_form': user_form})
@@ -41,7 +41,7 @@ def user_shipping_address_view(request, checkout):
return redirect('checkout:shipping-method')
taxes = get_taxes_for_checkout(checkout, request.taxes)
- ctx = get_checkout_data_for_checkout(checkout, request.discounts, taxes)
+ ctx = get_checkout_context(checkout, request.discounts, taxes)
ctx.update({
'additional_addresses': user_addresses,
'address_form': address_form,
diff --git a/saleor/checkout/views/summary.py b/saleor/checkout/views/summary.py
index 72e6dfe6c8..7939bdf668 100644
--- a/saleor/checkout/views/summary.py
+++ b/saleor/checkout/views/summary.py
@@ -11,7 +11,7 @@ from ...order import OrderEvents, OrderEventsEmails
from ...order.emails import send_order_confirmation
from ..forms import CheckoutNoteForm
from ..utils import (
- create_order, get_checkout_data_for_checkout, get_taxes_for_checkout,
+ create_order, get_checkout_context, get_taxes_for_checkout,
update_billing_address_in_anonymous_checkout,
update_billing_address_in_checkout,
update_billing_address_in_checkout_with_shipping)
@@ -70,7 +70,7 @@ def summary_with_shipping_view(request, checkout):
return handle_order_placement(request, checkout)
taxes = get_taxes_for_checkout(checkout, request.taxes)
- ctx = get_checkout_data_for_checkout(checkout, request.discounts, taxes)
+ ctx = get_checkout_context(checkout, request.discounts, taxes)
ctx.update({
'additional_addresses': user_addresses,
'address_form': address_form,
@@ -96,7 +96,7 @@ def anonymous_summary_without_shipping(request, checkout):
return handle_order_placement(request, checkout)
taxes = get_taxes_for_checkout(checkout, request.taxes)
- ctx = get_checkout_data_for_checkout(checkout, request.discounts, taxes)
+ ctx = get_checkout_context(checkout, request.discounts, taxes)
ctx.update({
'address_form': address_form,
'note_form': note_form,
@@ -123,7 +123,7 @@ def summary_without_shipping(request, checkout):
return handle_order_placement(request, checkout)
taxes = get_taxes_for_checkout(checkout, request.taxes)
- ctx = get_checkout_data_for_checkout(checkout, request.discounts, taxes)
+ ctx = get_checkout_context(checkout, request.discounts, taxes)
ctx.update({
'additional_addresses': user_addresses,
'address_form': address_form,
diff --git a/tests/test_cart.py b/tests/test_cart.py
index 383fef001b..a6c39050a3 100644
--- a/tests/test_cart.py
+++ b/tests/test_cart.py
@@ -614,8 +614,8 @@ def test_get_checkout_data(request_checkout_with_item, shipping_zone, vatlayer):
checkout = request_checkout_with_item
shipment_option = get_shipping_price_estimate(
checkout.get_subtotal().gross, checkout.get_total_weight(), 'PL', vatlayer)
- checkout_data = utils.get_checkout_data(
- checkout, shipment_option, 'USD', None, vatlayer)
+ checkout_data = utils.get_checkout_context(
+ checkout, None, vatlayer, currency='USD', shipping_range=shipment_option)
assert checkout_data['checkout_total'] == TaxedMoney(
net=Money('8.13', 'USD'), gross=Money(10, 'USD'))
assert checkout_data['total_with_shipping'].start == TaxedMoney(
@@ -626,8 +626,9 @@ def test_get_checkout_data_no_shipping(request_checkout_with_item, vatlayer):
checkout = request_checkout_with_item
shipment_option = get_shipping_price_estimate(
checkout.get_subtotal().gross, checkout.get_total_weight(), 'PL', vatlayer)
- checkout_data = utils.get_checkout_data(
- checkout, shipment_option, 'USD', None, vatlayer)
+ checkout_data = utils.get_checkout_context(
+ checkout, None, vatlayer,
+ currency='USD', shipping_range=shipment_option)
checkout_total = checkout_data['checkout_total']
assert checkout_total == TaxedMoney(
net=Money('8.13', 'USD'), gross=Money(10, 'USD'))
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 91d4fc01e8..b59f90d81a 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -5,7 +5,7 @@ import pytest
from django.urls import reverse
from django_countries.fields import Country
from freezegun import freeze_time
-from prices import Money, TaxedMoney
+from prices import Money, TaxedMoney, TaxedMoneyRange
from saleor.account.models import Address
from saleor.checkout import views
@@ -13,7 +13,7 @@ from saleor.checkout.forms import CheckoutVoucherForm, CountryForm
from saleor.checkout.utils import (
add_variant_to_checkout, add_voucher_to_checkout,
change_billing_address_in_checkout, change_shipping_address_in_checkout,
- clear_shipping_method, create_order, get_checkout_data_for_checkout,
+ clear_shipping_method, create_order, get_checkout_context,
get_prices_of_products_in_discounted_categories, get_taxes_for_checkout,
get_voucher_discount_for_checkout, get_voucher_for_checkout,
is_valid_shipping_method, recalculate_checkout_discount,
@@ -874,7 +874,7 @@ def test_recalculate_checkout_discount_expired_voucher(checkout_with_voucher, vo
assert checkout.discount_amount == ZERO_MONEY
-def test_get_checkout_data_for_checkout(checkout_with_voucher, vatlayer):
+def test_get_checkout_context(checkout_with_voucher, vatlayer):
line_price = TaxedMoney(
net=Money('24.39', 'USD'), gross=Money('30.00', 'USD'))
expected_data = {
@@ -883,9 +883,12 @@ def test_get_checkout_data_for_checkout(checkout_with_voucher, vatlayer):
'checkout_lines': [(checkout_with_voucher.lines.first(), line_price)],
'checkout_shipping_price': ZERO_TAXED_MONEY,
'checkout_subtotal': line_price,
- 'checkout_total': line_price - checkout_with_voucher.discount_amount}
+ 'checkout_total': line_price - checkout_with_voucher.discount_amount,
+ 'shipping_required': checkout_with_voucher.is_shipping_required(),
+ 'total_with_shipping': TaxedMoneyRange(
+ start=line_price, stop=line_price)}
- data = get_checkout_data_for_checkout(
+ data = get_checkout_context(
checkout_with_voucher, discounts=None, taxes=vatlayer)
assert data == expected_data