Skip to content

Commit

Permalink
Change shipping discount voucher choices
Browse files Browse the repository at this point in the history
  • Loading branch information
Pacu2 committed Aug 2, 2018
1 parent c44682a commit adb7cc3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
7 changes: 4 additions & 3 deletions saleor/checkout/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ..account.forms import get_address_form
from ..account.models import Address
from ..account.utils import store_user_address
from ..core.i18n import ANY_COUNTRY
from ..core.exceptions import InsufficientStock
from ..core.utils import to_local_currency
from ..core.utils.taxes import ZERO_MONEY, get_taxes_for_country
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion saleor/core/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django_countries import countries

ANY_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 = {
Expand Down
9 changes: 6 additions & 3 deletions saleor/dashboard/discount/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django_prices.forms import MoneyField
from mptt.forms import TreeNodeMultipleChoiceField

from ...core.i18n import COUNTRY_CODE_CHOICES
from ...core.i18n import COUNTRY_CODE_CHOICES, ANY_COUNTRY
from ...core.utils.taxes import ZERO_MONEY
from ...discount import DiscountValueType
from ...discount.models import Sale, Voucher
Expand Down Expand Up @@ -131,11 +131,14 @@ def _generate_code(self):
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_CHOICES
country_dict = dict(COUNTRY_CODE_CHOICES)
return [
country_choices = [
(country_code, country_dict[country_code])
for country_code in country_codes]
return country_choices


class ShippingVoucherForm(forms.ModelForm):
Expand Down
18 changes: 17 additions & 1 deletion tests/dashboard/test_discounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

from django.urls import reverse
from 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_choices
from saleor.dashboard.order.utils import get_voucher_discount_for_order
from saleor.discount import DiscountValueType, VoucherType
from saleor.discount.models import Sale, Voucher, NotApplicable
from saleor.discount.models import NotApplicable, Sale, Voucher
from saleor.product.models import Collection
from saleor.shipping.models import ShippingMethodCountry


def test_sales_list(admin_client, sale):
Expand Down Expand Up @@ -191,3 +195,15 @@ def test_ajax_voucher_list(admin_client, voucher):

assert response.status_code == 200
assert 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'))]

0 comments on commit adb7cc3

Please sign in to comment.