Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add new InvalidPromoCodeError
fowczarek564b80b9115b
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex dd995230ab..2b266207a7 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -17,7 +17,11 @@ from ..account.models import Address, Userfrom ..account.utils import store_user_addressfrom ..core.exceptions import InsufficientStockfrom ..core.utils import to_local_currency-from ..core.utils.promo_code import promo_code_is_gift_card, promo_code_is_voucher+from ..core.utils.promo_code import (+ InvalidPromoCode,+ promo_code_is_gift_card,+ promo_code_is_voucher,+)from ..core.utils.taxes import ZERO_MONEY, get_taxes_for_countryfrom ..discount import VoucherTypefrom ..discount.models import NotApplicable, Voucher@@ -789,25 +793,25 @@ def recalculate_checkout_discount(checkout, discounts, taxes):def add_promo_code_to_checkout(checkout: Checkout, promo_code: str):"""Add gift card or voucher data to checkout.- Raise ValidationError if promo code does not match to any voucher or gift card.+ Raise InvalidPromoCode if promo code does not match to any voucher or gift card."""if promo_code_is_voucher(promo_code):add_voucher_code_to_checkout(checkout, promo_code)elif promo_code_is_gift_card(promo_code):add_gift_card_code_to_checkout(checkout, promo_code)else:- raise ValidationError({"promo_code": "Promo code does not exist."})+ raise InvalidPromoCode()def add_voucher_code_to_checkout(checkout: Checkout, voucher_code: str):"""Add voucher data to checkout by code.- Raise ValidationError if voucher of given type cannot be applied.+ Raise InvalidPromoCode() if voucher of given type cannot be applied."""try:voucher = Voucher.objects.active(date=date.today()).get(code=voucher_code)except Voucher.DoesNotExist:- raise ValidationError({"promo_code": "Voucher with given code is invalid."})+ raise InvalidPromoCode()try:add_voucher_to_checkout(checkout, voucher)except NotApplicable:diff --git a/saleor/core/utils/promo_code.py b/saleor/core/utils/promo_code.pyindex a595de5437..0e4e8437d4 100644--- a/saleor/core/utils/promo_code.py+++ b/saleor/core/utils/promo_code.py@@ -1,9 +1,18 @@import uuid+from django.core.exceptions import ValidationError+from ...discount.models import Voucherfrom ...giftcard.models import GiftCard+class InvalidPromoCode(ValidationError):+ def __init__(self, message=None, **kwargs):+ if message is None:+ message = {"promo_code": "Promo code is invalid"}+ super().__init__(message, **kwargs)++def generate_promo_code():"""Generate a promo unique code that can be used as a voucher or gift card code."""code = str(uuid.uuid4()).replace("-", "").upper()[:12]diff --git a/saleor/giftcard/utils.py b/saleor/giftcard/utils.pyindex b18ea3d0de..37973b3b51 100644--- a/saleor/giftcard/utils.py+++ b/saleor/giftcard/utils.py@@ -1,7 +1,6 @@from datetime import date-from django.core.exceptions import ValidationError-+from ..core.utils.promo_code import InvalidPromoCodefrom ..checkout.models import Checkoutfrom .models import GiftCard@@ -9,12 +8,12 @@ from .models import GiftCarddef add_gift_card_code_to_checkout(checkout: Checkout, promo_code: str):"""Add gift card data to checkout by code.- Raise ValidationError if gift card cannot be applied.+ Raise InvalidPromoCode if gift card cannot be applied."""try:gift_card = GiftCard.objects.active(date=date.today()).get(code=promo_code)except GiftCard.DoesNotExist:- raise ValidationError({"promo_code": "Gift card with given code is invalid."})+ raise InvalidPromoCode()checkout.gift_cards.add(gift_card)diff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.pyindex e7a81d029d..08ddcc19ff 100644--- a/saleor/graphql/discount/mutations.py+++ b/saleor/graphql/discount/mutations.py@@ -1,7 +1,11 @@import graphenefrom django.core.exceptions import ValidationError-from ...core.utils.promo_code import generate_promo_code, is_available_promo_code+from ...core.utils.promo_code import (+ InvalidPromoCode,+ generate_promo_code,+ is_available_promo_code,+)from ...discount import modelsfrom ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutationfrom ..core.scalars import Decimal@@ -116,7 +120,7 @@ class VoucherCreate(ModelMutation):if code == "":data["code"] = generate_promo_code()elif not is_available_promo_code(code):- raise ValidationError({"code": "Voucher with this code is not available."})+ raise InvalidPromoCode()cleaned_input = super().clean_input(info, instance, data)return cleaned_inputdiff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.pyindex 1490cb8689..a6b5330c26 100644--- a/saleor/graphql/giftcard/mutations.py+++ b/saleor/graphql/giftcard/mutations.py@@ -2,7 +2,11 @@ import graphenefrom django.core.exceptions import ObjectDoesNotExist, ValidationErrorfrom ...account.models import User-from ...core.utils.promo_code import generate_promo_code, is_available_promo_code+from ...core.utils.promo_code import (+ InvalidPromoCode,+ generate_promo_code,+ is_available_promo_code,+)from ...giftcard import modelsfrom ...giftcard.utils import activate_gift_card, deactivate_gift_cardfrom ..core.mutations import BaseMutation, ModelMutation@@ -44,9 +48,7 @@ class GiftCardCreate(ModelMutation):if not code and not instance.pk:data["code"] = generate_promo_code()elif not is_available_promo_code(code):- raise ValidationError(- {"code": "Gift card with this code is not available."}- )+ raise InvalidPromoCode()cleaned_input = super().clean_input(info, instance, data)balance = cleaned_input.get("balance", None)if balance:diff --git a/tests/api/test_discount.py b/tests/api/test_discount.pyindex cbc445d852..c256c54db6 100644--- a/tests/api/test_discount.py+++ b/tests/api/test_discount.py@@ -241,8 +241,7 @@ def test_create_voucher_with_existing_gift_card_code(assert content["data"]["voucherCreate"]["errors"]errors = content["data"]["voucherCreate"]["errors"]assert len(errors) == 1- assert errors[0]["field"] == "code"- assert errors[0]["message"] == "Voucher with this code is not available."+ assert errors[0]["field"] == "promoCode"def test_create_voucher_with_existing_voucher_code(@@ -269,8 +268,7 @@ def test_create_voucher_with_existing_voucher_code(assert content["data"]["voucherCreate"]["errors"]errors = content["data"]["voucherCreate"]["errors"]assert len(errors) == 1- assert errors[0]["field"] == "code"- assert errors[0]["message"] == "Voucher with this code is not available."+ assert errors[0]["field"] == "promoCode"def test_update_voucher(staff_api_client, voucher, permission_manage_discounts):diff --git a/tests/api/test_gift_card.py b/tests/api/test_gift_card.pyindex ffde75c0c0..f0ab8f90c4 100644--- a/tests/api/test_gift_card.py+++ b/tests/api/test_gift_card.py@@ -214,8 +214,7 @@ def test_create_gift_card_with_existing_voucher_code(assert content["data"]["giftCardCreate"]["errors"]errors = content["data"]["giftCardCreate"]["errors"]assert len(errors) == 1- assert errors[0]["field"] == "code"- assert errors[0]["message"] == "Gift card with this code is not available."+ assert errors[0]["field"] == "promoCode"def test_create_gift_card_with_existing_gift_card_code(@@ -230,8 +229,7 @@ def test_create_gift_card_with_existing_gift_card_code(assert content["data"]["giftCardCreate"]["errors"]errors = content["data"]["giftCardCreate"]["errors"]assert len(errors) == 1- assert errors[0]["field"] == "code"- assert errors[0]["message"] == "Gift card with this code is not available."+ assert errors[0]["field"] == "promoCode"def test_create_gift_card_without_user(staff_api_client, permission_manage_gift_card):