Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move add promo code to checkout to utils
fowczarekcf1dcf328db3
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex 421761e2e3..3a3d58d9d9 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -17,6 +17,7 @@ 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_voucherfrom ..core.utils.taxes import ZERO_MONEY, get_taxes_for_countryfrom ..discount import VoucherTypefrom ..discount.models import NotApplicable, Voucher@@ -781,6 +782,28 @@ def recalculate_checkout_discount(checkout, discounts, taxes):remove_voucher_from_checkout(checkout)+def add_promo_code_to_checkout(checkout, promo_code):+ if promo_code_is_voucher(promo_code):+ add_voucher_code_to_checkout(checkout, promo_code)+ elif promo_code_is_gift_card(promo_code):+ raise ValidationError({"promo_code": "Not implemented yet"})+ else:+ raise ValidationError({"promo_code": "Promo code does not exists."})+++def add_voucher_code_to_checkout(checkout, voucher_code):+ try:+ voucher = Voucher.objects.active(date=date.today()).get(code=voucher_code)+ except Voucher.DoesNotExist:+ raise ValidationError({"promo_code": "Voucher with given code does not exist."})+ try:+ add_voucher_to_checkout(voucher, checkout)+ except NotApplicable:+ raise ValidationError(+ {"promo_code": "Voucher is not applicable to that checkout."}+ )++def add_voucher_to_checkout(voucher, checkout):"""Add voucher data to checkout.diff --git a/saleor/core/utils/promo_code.py b/saleor/core/utils/promo_code.pyindex a941f9015f..ba4f1ac5e3 100644--- a/saleor/core/utils/promo_code.py+++ b/saleor/core/utils/promo_code.py@@ -13,7 +13,12 @@ def generate_promo_code(length=12):def is_avaible_promo_code(code):- return not (- GiftCard.objects.filter(code=code).exists()- or Voucher.objects.filter(code=code).exists()- )+ return not (promo_code_is_gift_card(code) or promo_code_is_voucher(code))+++def promo_code_is_voucher(code):+ return Voucher.objects.filter(code=code).exists()+++def promo_code_is_gift_card(code):+ return GiftCard.objects.filter(code=code).exists()diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.pyindex b171e96e21..34c4aa6657 100644--- a/saleor/graphql/checkout/mutations.py+++ b/saleor/graphql/checkout/mutations.py@@ -7,6 +7,7 @@ from django.db import transactionfrom ...checkout import modelsfrom ...checkout.utils import (+ add_promo_code_to_checkout,add_variant_to_checkout,add_voucher_to_checkout,change_billing_address_in_checkout,@@ -583,21 +584,7 @@ class CheckoutAddPromoCode(BaseMutation):checkout = cls.get_node_or_error(info, checkout_id, only_type=Checkout, field="checkout_id")- try:- voucher = voucher_model.Voucher.objects.active(date=date.today()).get(- code=promo_code- )- except voucher_model.Voucher.DoesNotExist:- raise ValidationError(- {"promo_code": "Voucher with given code does not exist."}- )-- try:- add_voucher_to_checkout(voucher, checkout)- except voucher_model.NotApplicable:- raise ValidationError(- {"promo_code": "Voucher is not applicable to that checkout."}- )+ add_promo_code_to_checkout(checkout, promo_code)return CheckoutAddPromoCode(checkout=checkout)