Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use AddressType to determine type
Anna Siwik9f053bfaa520
diff --git a/saleor/account/forms.py b/saleor/account/forms.pyindex 63cd65f72d..564fd41a9e 100644--- a/saleor/account/forms.py+++ b/saleor/account/forms.py@@ -25,10 +25,8 @@ def get_address_form(address_form_class = get_address_form_class(country_code)if not preview and instance is not None:- address_form_class = get_address_form_class(- instance.country.code)- address_form = address_form_class(- data, instance=instance, **kwargs)+ address_form_class = get_address_form_class(instance.country.code)+ address_form = address_form_class(data, instance=instance, **kwargs)else:initial_address = (initial if not previewdiff --git a/saleor/checkout/__init__.py b/saleor/checkout/__init__.pyindex eea436a379..c650761f77 100644--- a/saleor/checkout/__init__.py+++ b/saleor/checkout/__init__.py@@ -1,3 +1,20 @@import logging+from django.utils.translation import pgettext_lazy+logger = logging.getLogger(__name__)+++class AddressType:+ BILLING = 'billing'+ SHIPPING = 'shipping'++ CHOICES = [+ (BILLING, pgettext_lazy(+ 'Type of address used to fulfill order',+ 'Billing'+ )),+ (SHIPPING, pgettext_lazy(+ 'Type of address used to fulfill order',+ 'Shipping'+ ))]diff --git a/saleor/checkout/views.py b/saleor/checkout/views.pydeleted file mode 100644index 4d5520891e..0000000000--- a/saleor/checkout/views.py+++ /dev/null@@ -1,142 +0,0 @@-"""Cart-related views."""-from django.http import JsonResponse-from django.shortcuts import get_object_or_404, redirect, render-from django.template.response import TemplateResponse--from ..core.utils import (- format_money, get_user_shipping_country, to_local_currency)-from ..product.models import ProductVariant-from ..shipping.utils import get_shipment_options-from .forms import CountryForm, ReplaceCartLineForm-from .models import Cart-from .utils import (- check_product_availability_and_warn, get_cart_data, get_or_empty_db_cart)---@get_or_empty_db_cart(cart_queryset=Cart.objects.for_display())-def index(request, cart):- """Display cart details."""- discounts = request.discounts- taxes = request.taxes- cart_lines = []- check_product_availability_and_warn(request, cart)-- # refresh required to get updated cart lines and it's quantity- try:- cart = Cart.objects.prefetch_related(- 'lines__variant__product__category').get(pk=cart.pk)- except Cart.DoesNotExist:- pass-- lines = cart.lines.select_related(- 'variant__product__product_type',- 'variant__product__category')- lines = lines.prefetch_related(- 'variant__product__images',- 'variant__product__product_type__variant_attributes')- for line in lines:- initial = {'quantity': line.quantity}- form = ReplaceCartLineForm(- None, cart=cart, variant=line.variant, initial=initial,- discounts=discounts, taxes=taxes)- cart_lines.append({- 'variant': line.variant,- 'get_price': line.variant.get_price(discounts, taxes),- 'get_total': line.get_total(discounts, taxes),- 'form': form})-- default_country = get_user_shipping_country(request)- country_form = CountryForm(initial={'country': default_country})- default_country_options = get_shipment_options(default_country, taxes)-- cart_data = get_cart_data(- cart, default_country_options, request.currency, discounts, taxes)- ctx = {- 'cart_lines': cart_lines,- 'country_form': country_form,- 'default_country_options': default_country_options}- ctx.update(cart_data)-- return TemplateResponse(request, 'cart/index.html', ctx)---@get_or_empty_db_cart(cart_queryset=Cart.objects.for_display())-def get_shipping_options(request, cart):- """Display shipping options to get a price estimate."""- country_form = CountryForm(request.POST or None, taxes=request.taxes)- if country_form.is_valid():- shipments = country_form.get_shipment_options()- else:- shipments = None- ctx = {- 'default_country_options': shipments,- 'country_form': country_form}- cart_data = get_cart_data(- cart, shipments, request.currency, request.discounts, request.taxes)- ctx.update(cart_data)- return TemplateResponse(request, 'cart/_subtotal_table.html', ctx)---@get_or_empty_db_cart()-def update(request, cart, variant_id):- """Update the line quantities."""- if not request.is_ajax():- return redirect('cart:index')- variant = get_object_or_404(ProductVariant, pk=variant_id)- discounts = request.discounts- taxes = request.taxes- status = None- form = ReplaceCartLineForm(- request.POST, cart=cart, variant=variant, discounts=discounts,- taxes=taxes)- if form.is_valid():- form.save()- response = {- 'variantId': variant_id,- 'subtotal': 0,- 'total': 0,- 'cart': {- 'numItems': cart.quantity,- 'numLines': len(cart)}}- updated_line = cart.get_line(form.cart_line.variant)- if updated_line:- response['subtotal'] = format_money(- updated_line.get_total(discounts, taxes).gross)- if cart:- cart_total = cart.get_subtotal(discounts, taxes)- response['total'] = format_money(cart_total.gross)- local_cart_total = to_local_currency(cart_total, request.currency)- if local_cart_total is not None:- response['localTotal'] = format_money(local_cart_total.gross)- status = 200- elif request.POST is not None:- response = {'error': form.errors}- status = 400- return JsonResponse(response, status=status)---@get_or_empty_db_cart(cart_queryset=Cart.objects.for_display())-def summary(request, cart):- """Display a cart summary suitable for displaying on all pages."""- discounts = request.discounts- taxes = request.taxes-- def prepare_line_data(line):- first_image = line.variant.get_first_image()- return {- 'product': line.variant.product,- 'variant': line.variant.name,- 'quantity': line.quantity,- 'image': first_image,- 'line_total': line.get_total(discounts, taxes),- 'variant_url': line.variant.get_absolute_url()}-- if cart.quantity == 0:- data = {'quantity': 0}- else:- data = {- 'quantity': cart.quantity,- 'total': cart.get_subtotal(discounts, taxes),- 'lines': [prepare_line_data(line) for line in cart.lines.all()]}-- return render(request, 'cart_dropdown.html', data)diff --git a/saleor/dashboard/order/urls.py b/saleor/dashboard/order/urls.pyindex d1bd755560..039d191c1d 100644--- a/saleor/dashboard/order/urls.py+++ b/saleor/dashboard/order/urls.py@@ -14,7 +14,7 @@ urlpatterns = [url(r'^(?P<order_pk>\d+)/cancel/$',views.cancel_order, name='order-cancel'),url(r'^(?P<order_pk>\d+)/address/(?P<address_type>billing|shipping)/$',- views.address_view, name='address-edit'),+ views.order_address, name='address-edit'),url(r'^(?P<order_pk>\d+)/edit-customer/$',views.order_customer_edit, name='order-customer-edit'),url(r'^(?P<order_pk>\d+)/remove-customer/$',diff --git a/saleor/dashboard/order/utils.py b/saleor/dashboard/order/utils.pyindex faf910c032..44ebb215b2 100644--- a/saleor/dashboard/order/utils.py+++ b/saleor/dashboard/order/utils.py@@ -2,6 +2,7 @@ from django.conf import settingsfrom django.contrib.sites.shortcuts import get_current_sitefrom django.template.loader import get_template+from ...checkout import AddressTypefrom ...core.utils.taxes import ZERO_MONEYfrom ...discount import VoucherTypefrom ...discount.models import NotApplicable@@ -156,7 +157,7 @@ def save_address_in_order(order, address, address_type):If the other type of address is empty, copy it."""- if address_type == 'shipping':+ if address_type == AddressType.SHIPPING:order.shipping_address = addressif not order.billing_address:order.billing_address = address.get_copy()diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex 3ba6c61317..f7a93df329 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -330,7 +330,7 @@ def add_variant_to_order(request, order_pk):@staff_member_required@permission_required('order.edit_order')-def address_view(request, order_pk, address_type):+def order_address(request, order_pk, address_type):order = get_object_or_404(Order, pk=order_pk)update_prices = Falseif address_type == 'shipping':diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex 03cffb6de5..4a6dbe46c1 100644--- a/tests/dashboard/test_order.py+++ b/tests/dashboard/test_order.py@@ -5,8 +5,10 @@ from django.conf import settingsfrom django.urls import reversefrom payments import PaymentStatusfrom prices import Money+from tests.utils import get_form_errors, get_redirect_location+from saleor.checkout import AddressTypefrom saleor.core.utils.taxes import ZERO_MONEY, ZERO_TAXED_MONEYfrom saleor.dashboard.order.forms import ChangeQuantityForm, OrderNoteFormfrom saleor.dashboard.order.utils import (@@ -949,7 +951,7 @@ def test_save_address_in_order_shipping_address(order, address):address.first_name = 'Jane'address.save()- save_address_in_order(order, address, 'shipping')+ save_address_in_order(order, address, AddressType.SHIPPING)assert order.shipping_address == addressassert order.shipping_address.pk == address.pk@@ -960,7 +962,7 @@ def test_save_address_in_order_billing_address(order, address):address.first_name = 'Jane'address.save()- save_address_in_order(order, address, 'billing')+ save_address_in_order(order, address, AddressType.BILLING)assert order.billing_address == addressassert order.billing_address.pk == address.pk