Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add PromoCodeAlreadyExists error
fowczarekcabbdaac958c
diff --git a/saleor/core/utils/promo_code.py b/saleor/core/utils/promo_code.pyindex 0e4e8437d4..b323dfc9a4 100644--- a/saleor/core/utils/promo_code.py+++ b/saleor/core/utils/promo_code.py@@ -13,6 +13,13 @@ class InvalidPromoCode(ValidationError):super().__init__(message, **kwargs)+class PromoCodeAlreadyExists(ValidationError):+ def __init__(self, message=None, **kwargs):+ if message is None:+ message = {"promo_code": "Promo code already exists."}+ 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/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.pyindex 08ddcc19ff..38c3a8ee77 100644--- a/saleor/graphql/discount/mutations.py+++ b/saleor/graphql/discount/mutations.py@@ -1,8 +1,7 @@import graphene-from django.core.exceptions import ValidationErrorfrom ...core.utils.promo_code import (- InvalidPromoCode,+ PromoCodeAlreadyExists,generate_promo_code,is_available_promo_code,)@@ -120,7 +119,7 @@ class VoucherCreate(ModelMutation):if code == "":data["code"] = generate_promo_code()elif not is_available_promo_code(code):- raise InvalidPromoCode()+ raise PromoCodeAlreadyExists()cleaned_input = super().clean_input(info, instance, data)return cleaned_inputdiff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.pyindex a6b5330c26..f603650117 100644--- a/saleor/graphql/giftcard/mutations.py+++ b/saleor/graphql/giftcard/mutations.py@@ -3,7 +3,7 @@ from django.core.exceptions import ObjectDoesNotExist, ValidationErrorfrom ...account.models import Userfrom ...core.utils.promo_code import (- InvalidPromoCode,+ PromoCodeAlreadyExists,generate_promo_code,is_available_promo_code,)@@ -48,7 +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 InvalidPromoCode()+ raise PromoCodeAlreadyExists()cleaned_input = super().clean_input(info, instance, data)balance = cleaned_input.get("balance", None)if balance: