Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Extract enums for discounts

Marcin Gębala5c6ebe8f8b60
diff --git a/saleor/graphql/discount/enums.py b/saleor/graphql/discount/enums.py
new file mode 100644
index 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.VALUE
diff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.py
index 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, VoucherType
from ..core.mutations import ModelDeleteMutation, ModelMutation
from ..core.scalars import Decimal
-from .types import DiscountValueTypeEnum, VoucherTypeEnum
+from .enums import DiscountValueTypeEnum, VoucherTypeEnum
def validate_voucher(voucher_data):
diff --git a/saleor/graphql/discount/resolvers.py b/saleor/graphql/discount/resolvers.py
index 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_optimizer
from ...discount import models
from ..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.py
index abfd5a042e..ac330f0e89 100644
--- a/saleor/graphql/discount/schema.py
+++ b/saleor/graphql/discount/schema.py
@@ -2,12 +2,14 @@ import graphene
from graphql_jwt.decorators import permission_required
from ..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, Voucher
from .mutations import (
SaleCreate, SaleDelete, SaleUpdate, VoucherCreate, VoucherDelete,
VoucherUpdate)
from .resolvers import resolve_sales, resolve_vouchers
-from .types import Sale, Voucher
class 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.py
index 10945b57fd..fb7ec47adf 100644
--- a/saleor/graphql/discount/types.py
+++ b/saleor/graphql/discount/types.py
@@ -1,92 +1,78 @@
from textwrap import dedent
-import graphene
import graphene_django_optimizer as gql_optimizer
from graphene import relay
-from ...discount import DiscountValueType, VoucherType, models
+from ...discount import models
from ..core.connection import CountableDjangoObjectType
from ..core.fields import PrefetchingConnectionField
from ..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.Sale
def 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.Voucher
def 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)