Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Change shipping discount voucher choices
Pacu2adb7cc3f3302
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex ad5afb35d0..bfc7813b25 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -15,6 +15,7 @@ from . import AddressType, loggerfrom ..account.forms import get_address_formfrom ..account.models import Addressfrom ..account.utils import store_user_address+from ..core.i18n import ANY_COUNTRYfrom ..core.exceptions import InsufficientStockfrom ..core.utils import to_local_currencyfrom ..core.utils.taxes import ZERO_MONEY, get_taxes_for_country@@ -651,9 +652,9 @@ def _get_shipping_voucher_discount_for_cart(voucher, cart):'Voucher not applicable','Please select a shipping method first.')raise NotApplicable(msg)- not_valid_for_country = (- voucher.countries and- shipping_method.country_code not in voucher.countries)+ not_valid_for_country = all([+ voucher.countries, ANY_COUNTRY not in voucher.countries,+ shipping_method.country_code not in voucher.countries])if not_valid_for_country:msg = pgettext('Voucher not applicable',diff --git a/saleor/core/i18n.py b/saleor/core/i18n.pyindex d78786e9de..045359d95b 100644--- a/saleor/core/i18n.py+++ b/saleor/core/i18n.py@@ -2,7 +2,7 @@ from django.utils.translation import pgettext_lazyfrom django_countries import countriesANY_COUNTRY = ''-ANY_COUNTRY_DISPLAY = pgettext_lazy('Country choice', 'Rest of World')+ANY_COUNTRY_DISPLAY = pgettext_lazy('Country choice', 'Any Country')COUNTRY_CODE_CHOICES = [(ANY_COUNTRY, ANY_COUNTRY_DISPLAY)] + list(countries)VAT_RATE_TYPE_TRANSLATIONS = {diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.pyindex f8935485be..a505a4bb0b 100644--- a/saleor/dashboard/discount/forms.py+++ b/saleor/dashboard/discount/forms.py@@ -7,7 +7,7 @@ from django.utils.translation import pgettext_lazyfrom django_prices.forms import MoneyFieldfrom mptt.forms import TreeNodeMultipleChoiceField-from ...core.i18n import COUNTRY_CODE_CHOICES+from ...core.i18n import COUNTRY_CODE_CHOICES, ANY_COUNTRYfrom ...core.utils.taxes import ZERO_MONEYfrom ...discount import DiscountValueTypefrom ...discount.models import Sale, Voucher@@ -131,11 +131,14 @@ class VoucherForm(forms.ModelForm):def country_choices():country_codes = ShippingMethodCountry.objects.all()country_codes = country_codes.values_list('country_code', flat=True)- country_codes = country_codes.distinct()+ country_codes = list(country_codes.distinct())+ if ANY_COUNTRY in country_codes:+ return COUNTRY_CODE_CHOICEScountry_dict = dict(COUNTRY_CODE_CHOICES)- return [+ country_choices = [(country_code, country_dict[country_code])for country_code in country_codes]+ return country_choicesclass ShippingVoucherForm(forms.ModelForm):diff --git a/tests/dashboard/test_discounts.py b/tests/dashboard/test_discounts.pyindex a2d2f4f26e..51c20df99e 100644--- a/tests/dashboard/test_discounts.py+++ b/tests/dashboard/test_discounts.py@@ -7,10 +7,14 @@ import pytestfrom django.urls import reversefrom prices import Money, TaxedMoney+from saleor.core.i18n import ANY_COUNTRY, COUNTRY_CODE_CHOICES+from saleor.core.utils import get_country_name_by_code+from saleor.dashboard.discount.forms import country_choicesfrom saleor.dashboard.order.utils import get_voucher_discount_for_orderfrom saleor.discount import DiscountValueType, VoucherType-from saleor.discount.models import Sale, Voucher, NotApplicable+from saleor.discount.models import NotApplicable, Sale, Voucherfrom saleor.product.models import Collection+from saleor.shipping.models import ShippingMethodCountrydef test_sales_list(admin_client, sale):@@ -191,3 +195,15 @@ def test_ajax_voucher_list(admin_client, voucher):assert response.status_code == 200assert resp_decoded == {'results': vouchers_list}+++def test_country_choices(shipping_method):+ any_country_shipping_method = ShippingMethodCountry.objects.filter(+ country_code=ANY_COUNTRY)+ assert any_country_shipping_method.exists()+ assert country_choices() == COUNTRY_CODE_CHOICES++ any_country_shipping_method.delete()+ ShippingMethodCountry.objects.create(+ country_code='PL', price=10, shipping_method=shipping_method)+ assert country_choices() == [('PL', get_country_name_by_code('PL'))]