Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Extract enums for discounts
Marcin Gębala5c6ebe8f8b60
diff --git a/saleor/graphql/discount/enums.py b/saleor/graphql/discount/enums.pynew file mode 100644index 0000000000..8494016e37--- /dev/null+++ b/saleor/graphql/discount/enums.py@@ -0,0 +1,16 @@+import graphene++from ...discount import DiscountValueType, VoucherType+++class DiscountValueTypeEnum(graphene.Enum):+ FIXED = DiscountValueType.FIXED+ PERCENTAGE = DiscountValueType.PERCENTAGE+++class VoucherTypeEnum(graphene.Enum):+ PRODUCT = VoucherType.PRODUCT+ COLLECTION = VoucherType.COLLECTION+ CATEGORY = VoucherType.CATEGORY+ SHIPPING = VoucherType.SHIPPING+ VALUE = VoucherType.VALUEdiff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.pyindex adbd7545fe..07c5c16759 100644--- a/saleor/graphql/discount/mutations.py+++ b/saleor/graphql/discount/mutations.py@@ -1,9 +1,9 @@import graphene-from ...discount import VoucherType, models+from ...discount import models, VoucherTypefrom ..core.mutations import ModelDeleteMutation, ModelMutationfrom ..core.scalars import Decimal-from .types import DiscountValueTypeEnum, VoucherTypeEnum+from .enums import DiscountValueTypeEnum, VoucherTypeEnumdef validate_voucher(voucher_data):diff --git a/saleor/graphql/discount/resolvers.py b/saleor/graphql/discount/resolvers.pyindex 3eae48685a..bc850242c2 100644--- a/saleor/graphql/discount/resolvers.py+++ b/saleor/graphql/discount/resolvers.py@@ -3,11 +3,7 @@ import graphene_django_optimizer as gql_optimizerfrom ...discount import modelsfrom ..utils import filter_by_query_param-VOUCHER_SEARCH_FIELDS = (- 'name', 'code', 'discount_value', 'categories__name', 'collections__name',- 'products__name', 'min_amount_spent', 'countries', 'discount_value_type',- 'apply_one_per_order')-+VOUCHER_SEARCH_FIELDS = ('name', 'code',)SALE_SEARCH_FIELDS = ('name', 'value', 'type')diff --git a/saleor/graphql/discount/schema.py b/saleor/graphql/discount/schema.pyindex abfd5a042e..ac330f0e89 100644--- a/saleor/graphql/discount/schema.py+++ b/saleor/graphql/discount/schema.py@@ -2,12 +2,14 @@ import graphenefrom graphql_jwt.decorators import permission_requiredfrom ..core.fields import PrefetchingConnectionField-from ..descriptions import DESCRIPTIONS+# FIXME: Types are imported before mutations on purpose. Otherwise these types+# are missing in Graphene's type registry and mutations cannot be created+# properly. This happens only in the `saleor.graphql.discount` module.+from .types import Sale, Voucherfrom .mutations import (SaleCreate, SaleDelete, SaleUpdate, VoucherCreate, VoucherDelete,VoucherUpdate)from .resolvers import resolve_sales, resolve_vouchers-from .types import Sale, Voucherclass DiscountQueries(graphene.ObjectType):@@ -15,14 +17,16 @@ class DiscountQueries(graphene.ObjectType):Sale, id=graphene.Argument(graphene.ID, required=True),description='Lookup a sale by ID.')sales = PrefetchingConnectionField(- Sale, query=graphene.String(description=DESCRIPTIONS['sale']),- description="List of the shop\'s sales.")+ Sale, query=graphene.String(+ description='Search sales by name, value or type.'),+ description='List of the shop\'s sales.')voucher = graphene.Field(Voucher, id=graphene.Argument(graphene.ID, required=True),description='Lookup a voucher by ID.')vouchers = PrefetchingConnectionField(- Voucher, query=graphene.String(description=DESCRIPTIONS['product']),- description="List of the shop\'s vouchers.")+ Voucher, query=graphene.String(+ description='Search vouchers by name or code.'),+ description='List of the shop\'s vouchers.')@permission_required('discount.manage_discounts')def resolve_sale(self, info, id):diff --git a/saleor/graphql/discount/types.py b/saleor/graphql/discount/types.pyindex 10945b57fd..fb7ec47adf 100644--- a/saleor/graphql/discount/types.py+++ b/saleor/graphql/discount/types.py@@ -1,92 +1,78 @@from textwrap import dedent-import grapheneimport graphene_django_optimizer as gql_optimizerfrom graphene import relay-from ...discount import DiscountValueType, VoucherType, models+from ...discount import modelsfrom ..core.connection import CountableDjangoObjectTypefrom ..core.fields import PrefetchingConnectionFieldfrom ..product.types import Category, Collection, Product-class Voucher(CountableDjangoObjectType):+class Sale(CountableDjangoObjectType):categories = gql_optimizer.field(PrefetchingConnectionField(Category,- description='List of categories this voucher applies to.'),+ description='List of categories this sale applies to.'),model_field='categories')collections = gql_optimizer.field(PrefetchingConnectionField(Collection,- description='List of collections this voucher applies to.'),+ description='List of collections this sale applies to.'),model_field='collections')products = gql_optimizer.field(PrefetchingConnectionField(Product,- description='List of products this voucher applies to.'),+ description='List of products this sale applies to.'),model_field='products')class Meta:description = dedent("""- Vouchers allow giving discounts to particular customers on categories,- collections or specific products. They can be used during checkout by- providing valid voucher codes.""")+ Sales allow creating discounts for categories, collections or+ products and are visible to all the customers.""")interfaces = [relay.Node]- model = models.Voucher+ model = models.Saledef resolve_categories(self, info, **kwargs):return self.categories.all()def resolve_collections(self, info, **kwargs):- return self.collections.all()+ return self.collections.visible_to_user(info.context.user)def resolve_products(self, info, **kwargs):- return self.products.all()+ return self.products.visible_to_user(info.context.user)-class Sale(CountableDjangoObjectType):+class Voucher(CountableDjangoObjectType):categories = gql_optimizer.field(PrefetchingConnectionField(Category,- description='List of categories this sale applies to.'),+ description='List of categories this voucher applies to.'),model_field='categories')collections = gql_optimizer.field(PrefetchingConnectionField(Collection,- description='List of collections this sale applies to.'),+ description='List of collections this voucher applies to.'),model_field='collections')products = gql_optimizer.field(PrefetchingConnectionField(Product,- description='List of products this sale applies to.'),+ description='List of products this voucher applies to.'),model_field='products')class Meta:description = dedent("""- Sales allow creating discounts for categories, collections or- products and are visible to all the customers.""")+ Vouchers allow giving discounts to particular customers on categories,+ collections or specific products. They can be used during checkout by+ providing valid voucher codes.""")interfaces = [relay.Node]- model = models.Sale+ model = models.Voucherdef resolve_categories(self, info, **kwargs):return self.categories.all()def resolve_collections(self, info, **kwargs):- return self.collections.all()+ return self.collections.visible_to_user(info.context.user)def resolve_products(self, info, **kwargs):- return self.products.all()---class VoucherTypeEnum(graphene.Enum):- PRODUCT = VoucherType.PRODUCT- COLLECTION = VoucherType.COLLECTION- CATEGORY = VoucherType.CATEGORY- SHIPPING = VoucherType.SHIPPING- VALUE = VoucherType.VALUE---class DiscountValueTypeEnum(graphene.Enum):- FIXED = DiscountValueType.FIXED- PERCENTAGE = DiscountValueType.PERCENTAGE+ return self.products.visible_to_user(info.context.user)