Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add query for get gift cards for checkout
fowczarek5b4a41271e7b
diff --git a/saleor/checkout/models.py b/saleor/checkout/models.pyindex 303a2c2163..d63c4165e8 100644--- a/saleor/checkout/models.py+++ b/saleor/checkout/models.py@@ -13,6 +13,7 @@ from django_prices.models import MoneyFieldfrom ..account.models import Addressfrom ..core.utils.taxes import ZERO_TAXED_MONEY, zero_moneyfrom ..core.weight import zero_weight+from ..giftcard.models import GiftCardfrom ..shipping.models import ShippingMethodCENTS = Decimal("0.01")@@ -73,6 +74,7 @@ class Checkout(models.Model):discount_name = models.CharField(max_length=255, blank=True, null=True)translated_discount_name = models.CharField(max_length=255, blank=True, null=True)voucher_code = models.CharField(max_length=12, blank=True, null=True)+ gift_cards = models.ManyToManyField(GiftCard, blank=True, related_name="gift_cards")objects = CheckoutQueryset.as_manager()diff --git a/saleor/giftcard/models.py b/saleor/giftcard/models.pyindex 4b8bfdaf14..9486649e91 100644--- a/saleor/giftcard/models.py+++ b/saleor/giftcard/models.py@@ -41,3 +41,7 @@ class GiftCard(models.Model):pgettext_lazy("Permission description", "Manage gift cards."),),)++ @property+ def display_code(self):+ return "****%s" % self.code[-4:]diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.pyindex cc95897424..d59acaef95 100644--- a/saleor/graphql/checkout/types.py+++ b/saleor/graphql/checkout/types.py@@ -6,6 +6,7 @@ from ...checkout import modelsfrom ...core.utils.taxes import get_taxes_for_addressfrom ..core.connection import CountableDjangoObjectTypefrom ..core.types.money import TaxedMoney+from ..giftcard.types import GiftCardfrom ..order.utils import applicable_shipping_methodsfrom ..payment.enums import PaymentGatewayEnumfrom ..shipping.types import ShippingMethod@@ -49,6 +50,10 @@ class Checkout(CountableDjangoObjectType):required=True,)email = graphene.String(description="Email of a customer", required=True)+ gift_cards = gql_optimizer.field(+ graphene.List(GiftCard, description="List of userd gift cards"),+ model_field="gift_cards",+ )is_shipping_required = graphene.Boolean(description="Returns True, if checkout requires shipping.", required=True)@@ -84,6 +89,7 @@ class Checkout(CountableDjangoObjectType):"created","discount_amount","discount_name",+ "gift_cards","is_shipping_required","last_change","note",@@ -129,6 +135,10 @@ class Checkout(CountableDjangoObjectType):def resolve_available_payment_gateways(_: models.Checkout, _info):return settings.CHECKOUT_PAYMENT_GATEWAYS.keys()+ @staticmethod+ def resolve_gift_cards(root: models.Checkout, _info):+ return root.gift_cards.all()+@staticmethoddef resolve_is_shipping_required(root: models.Checkout, _info):return root.is_shipping_required()diff --git a/saleor/graphql/giftcard/types.py b/saleor/graphql/giftcard/types.pyindex a99e08f90d..1fa33e2c88 100644--- a/saleor/graphql/giftcard/types.py+++ b/saleor/graphql/giftcard/types.py@@ -1,12 +1,25 @@from textwrap import dedent-from graphene import relay+import graphene+from graphql_jwt.decorators import permission_requiredfrom ...giftcard import modelsfrom ..core.connection import CountableDjangoObjectTypeclass GiftCard(CountableDjangoObjectType):+ display_code = graphene.String(+ description="Code in format with allows displaying in a user interface."+ )+ buyer = graphene.Field(+ "saleor.graphql.account.types.User",+ description="The customer who bought a gift card.",+ )+ creator = graphene.Field(+ "saleor.graphql.account.types.User",+ description="The staff who created a gift card.",+ )+class Meta:description = dedent("""@@ -26,5 +39,16 @@ class GiftCard(CountableDjangoObjectType):"initial_balance","current_balance",]- interfaces = [relay.Node]+ interfaces = [graphene.relay.Node]model = models.GiftCard++ def resolve_display_code(self, _info):+ return self.display_code++ @permission_required("giftcard.manage_gift_card")+ def resolve_buyer(self, _info):+ return self.buyer++ @permission_required("giftcard.manage_gift_card")+ def resolve_creator(self, _info):+ return self.creatordiff --git a/tests/api/test_gift_card.py b/tests/api/test_gift_card.pyindex a9f233be6e..2649819961 100644--- a/tests/api/test_gift_card.py+++ b/tests/api/test_gift_card.py@@ -11,12 +11,6 @@ def test_query_own_gift_card(user_api_client, staff_user, gift_card):query giftCard($id: ID!) {giftCard(id: $id){code- buyer {- }- creator {- }createdstartDateexpirationDate@@ -37,8 +31,6 @@ def test_query_own_gift_card(user_api_client, staff_user, gift_card):content = get_graphql_content(response)data = content["data"]["giftCard"]assert data["code"] == gift_card.code- assert data["buyer"]["email"] == gift_card.buyer.email- assert data["creator"]["email"] == staff_user.emailassert data["created"] == gift_card.created.isoformat()assert data["startDate"] == gift_card.start_date.isoformat()assert data["expirationDate"] == gift_card.expiration_date