Skip to content

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

Move logic to new function

Mira Szewczykdda898e44358
diff --git a/saleor/cart/forms.py b/saleor/cart/forms.py
index cec6659cec..6b66c089b5 100644
--- a/saleor/cart/forms.py
+++ b/saleor/cart/forms.py
@@ -6,6 +6,7 @@ from django.core.exceptions import ObjectDoesNotExist, NON_FIELD_ERRORS
from django.utils.translation import pgettext_lazy, ugettext_lazy
from satchless.item import InsufficientStock
from ..shipping.models import COUNTRY_CODE_CHOICES, ShippingMethodCountry
+from ..shipping.utils import get_shipment_options
class QuantityField(forms.IntegerField):
@@ -121,5 +122,4 @@ class CountryForm(forms.Form):
def get_shipment_options(self):
code = self.cleaned_data['country']
- return ShippingMethodCountry.objects.filter(
- Q(country_code=code) | Q(country_code='')).order_by('price')
+ return get_shipment_options(code)
diff --git a/saleor/cart/views.py b/saleor/cart/views.py
index 8aca761c07..04df3ccc2d 100644
--- a/saleor/cart/views.py
+++ b/saleor/cart/views.py
@@ -9,6 +9,7 @@ from django.template.response import TemplateResponse
from . import decorators
from ..core.utils import to_local_currency, get_user_shipping_country
from ..product.models import ProductVariant
+from ..shipping.utils import get_shipment_options
from .forms import ReplaceCartLineForm, CountryForm
from .models import Cart
from .utils import check_product_availability_and_warn
@@ -38,6 +39,7 @@ def index(request, cart):
default_country = get_user_shipping_country(request)
country_form = CountryForm(initial={'country': default_country})
+ default_country_options = get_shipment_options(default_country)
return TemplateResponse(
request, 'cart/index.html',
@@ -45,7 +47,8 @@ def index(request, cart):
'cart_lines': cart_lines,
'cart_total': cart_total,
'local_cart_total': local_cart_total,
- 'country_form': country_form})
+ 'country_form': country_form,
+ 'default_country_options': default_country_options})
@decorators.get_or_empty_db_cart()
diff --git a/saleor/shipping/utils.py b/saleor/shipping/utils.py
new file mode 100644
index 0000000000..8e13039d84
--- /dev/null
+++ b/saleor/shipping/utils.py
@@ -0,0 +1,8 @@
+from django.db.models import Q
+from .models import ShippingMethodCountry
+
+
+def get_shipment_options(country_code):
+ return ShippingMethodCountry.objects.select_related(
+ 'shipping_method').filter(
+ Q(country_code=country_code) | Q(country_code='')).order_by('price')