Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
remove code usage (#16234)
Arturc18e40f5b6f3
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.pyindex b897e0893d..c96a6073d2 100644--- a/saleor/checkout/complete_checkout.py+++ b/saleor/checkout/complete_checkout.py@@ -307,22 +307,15 @@ def _create_line_for_order(prices_entered_with_tax,)- line_voucher_code = None- if checkout_line_info.voucher:- line_voucher_code = checkout_line_info.voucher.code- order_voucher_code = None- if checkout_info.voucher:- order_voucher_code = checkout_info.voucher.code-discount_price = undiscounted_unit_price - unit_priceif prices_entered_with_tax:discount_amount = discount_price.grosselse:discount_amount = discount_price.net- unit_discount_reason = _get_unit_discount_reason(- line_voucher_code, order_voucher_code- )+ voucher_code = checkout_info.checkout.voucher_code+ is_line_voucher_code = bool(checkout_line_info.voucher)+ unit_discount_reason = _get_unit_discount_reason(voucher_code, is_line_voucher_code)tax_class = Noneif product.tax_class_id:@@ -349,7 +342,7 @@ def _create_line_for_order(undiscounted_total_price=undiscounted_total_price, # money field not supported by mypy_django_plugin # noqa: E501total_price=total_line_price,tax_rate=tax_rate,- voucher_code=line_voucher_code,+ voucher_code=voucher_code if is_line_voucher_code else None,unit_discount=discount_amount, # money field not supported by mypy_django_plugin # noqa: E501unit_discount_reason=unit_discount_reason,unit_discount_value=discount_amount.amount, # we store value as fixed discount@@ -393,15 +386,14 @@ def _create_line_for_order(def _get_unit_discount_reason(- line_voucher_code: Optional[str],- order_voucher_code: Optional[str],+ voucher_code: Optional[str], is_line_voucher_code) -> Optional[str]:- unit_discount_reason = None- if line_voucher_code:- unit_discount_reason = f"Voucher code: {line_voucher_code}"- elif order_voucher_code:- unit_discount_reason = f"Entire order voucher code: {order_voucher_code}"- return unit_discount_reason+ if not voucher_code:+ return None+ return (+ f"{'Voucher code' if is_line_voucher_code else 'Entire order voucher code'}: "+ f"{voucher_code}"+ )def _create_order_line_discounts(diff --git a/saleor/discount/utils.py b/saleor/discount/utils.pyindex 0f81bb631d..69ed4b885f 100644--- a/saleor/discount/utils.py+++ b/saleor/discount/utils.py@@ -189,6 +189,21 @@ def get_voucher_code_instance(return code_instance+def get_active_voucher_code(voucher, channel_slug):+ """Return an active VoucherCode instance.++ This method along with `Voucher.code` should be removed in Saleor 4.0.+ """++ voucher_queryset = Voucher.objects.active_in_channel(timezone.now(), channel_slug)+ if not voucher_queryset.filter(pk=voucher.pk).exists():+ raise InvalidPromoCode()+ voucher_code = VoucherCode.objects.filter(voucher=voucher, is_active=True).first()+ if not voucher_code:+ raise InvalidPromoCode()+ return voucher_code++def apply_voucher_to_line(voucher_info: "VoucherInfo",lines_info: Iterable["LineInfo"],diff --git a/saleor/graphql/checkout/dataloaders.py b/saleor/graphql/checkout/dataloaders.pyindex 2c23aa7987..4c5e27c37d 100644--- a/saleor/graphql/checkout/dataloaders.py+++ b/saleor/graphql/checkout/dataloaders.py@@ -120,9 +120,9 @@ class CheckoutLinesInfoByCheckoutTokenLoader(DataLoader[str, list[CheckoutLineInlines_info_map = defaultdict(list)voucher_infos_map = {- voucher_info.voucher.code: voucher_info+ voucher_info.voucher_code: voucher_infofor voucher_info in voucher_infos- if voucher_info+ if voucher_info is not None and voucher_info.voucher_code}for checkout, lines in zip(checkouts, checkout_lines):lines_info_map[checkout.pk].extend(diff --git a/saleor/graphql/discount/mutations/bulk_mutations.py b/saleor/graphql/discount/mutations/bulk_mutations.pyindex ae977c041d..935db3cd57 100644--- a/saleor/graphql/discount/mutations/bulk_mutations.py+++ b/saleor/graphql/discount/mutations/bulk_mutations.py@@ -2,10 +2,11 @@ from typing import Optionalimport graphenefrom django.core.exceptions import ValidationError-from django.db.models import Exists, OuterRef, QuerySet+from django.db.models import Exists, OuterRef, QuerySet, Subqueryfrom ....discount import modelsfrom ....discount.error_codes import DiscountErrorCode+from ....discount.models import VoucherCodefrom ....permission.enums import DiscountPermissionsfrom ....product.utils.product import (get_channel_to_products_map_from_rules,@@ -147,8 +148,12 @@ class VoucherBulkDelete(ModelBulkDeleteMutation):@classmethoddef bulk_action(cls, info: ResolveInfo, queryset, /):manager = get_plugin_manager_promise(info.context).get()- vouchers = list(queryset)- codes = [voucher.code for voucher in vouchers]+ vouchers = queryset.annotate(+ last_code=Subquery(+ VoucherCode.objects.filter(voucher=OuterRef("pk")).values("code")[:1]+ )+ )+ codes = [voucher.last_code for voucher in vouchers]webhooks = get_webhooks_for_event(WebhookEventAsyncType.VOUCHER_DELETED)queryset.delete()diff --git a/saleor/graphql/order/mutations/draft_order_create.py b/saleor/graphql/order/mutations/draft_order_create.pyindex f602df889e..3912c5529c 100644--- a/saleor/graphql/order/mutations/draft_order_create.py+++ b/saleor/graphql/order/mutations/draft_order_create.py@@ -10,7 +10,11 @@ from ....core.taxes import TaxErrorfrom ....core.tracing import traced_atomic_transactionfrom ....core.utils.url import validate_storefront_urlfrom ....discount.models import Voucher, VoucherCode-from ....discount.utils import get_voucher_code_instance, increase_voucher_usage+from ....discount.utils import (+ get_active_voucher_code,+ get_voucher_code_instance,+ increase_voucher_usage,+)from ....order import OrderOrigin, OrderStatus, events, modelsfrom ....order.error_codes import OrderErrorCodefrom ....order.search import update_order_search_vector@@ -289,7 +293,7 @@ class DraftOrderCreate(if channel.include_draft_order_in_voucher_usage:# Validate voucher when it's included in voucher usage calculationtry:- code_instance = get_voucher_code_instance(voucher.code, channel.slug)+ code_instance = get_active_voucher_code(voucher, channel.slug)except ValidationError:raise ValidationError({