Skip to content

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

Extend ProductType with type field

IKarbowiake1fc900ce43f
diff --git a/saleor/core/tests/test_core.py b/saleor/core/tests/test_core.py
index c5d6d8ccc4..24bf8efc72 100644
--- a/saleor/core/tests/test_core.py
+++ b/saleor/core/tests/test_core.py
@@ -17,6 +17,7 @@ from ...channel.models import Channel
from ...discount.models import Sale, SaleChannelListing, Voucher, VoucherChannelListing
from ...giftcard.models import GiftCard
from ...order.models import Order
+from ...product import ProductTypeKind
from ...product.models import ProductMedia, ProductType
from ...shipping.models import ShippingZone
from ..storages import S3MediaStorage
@@ -290,7 +291,11 @@ def test_generate_unique_slug_with_slugable_field(
("わたし わ にっぽん です", "わたし-わ-にっぽん-です"),
]
for name, slug in product_names_and_slugs:
- ProductType.objects.create(name=name, slug=slug)
+ ProductType.objects.create(
+ name=name,
+ slug=slug,
+ type=ProductTypeKind.NORMAL,
+ )
instance, _ = ProductType.objects.get_or_create(name=product_name)
result = generate_unique_slug(instance, instance.name)
diff --git a/saleor/graphql/attribute/tests/deprecated/test_attributes.py b/saleor/graphql/attribute/tests/deprecated/test_attributes.py
index fb700c044a..5c982a8ae8 100644
--- a/saleor/graphql/attribute/tests/deprecated/test_attributes.py
+++ b/saleor/graphql/attribute/tests/deprecated/test_attributes.py
@@ -6,6 +6,7 @@ from django.db.models import Q
from .....attribute.models import Attribute
from .....channel.models import Channel
from .....channel.utils import DEPRECATION_WARNING_MESSAGE
+from .....product import ProductTypeKind
from .....product.models import Category, Product, ProductType
from ....tests.utils import get_graphql_content
@@ -39,7 +40,10 @@ def test_attributes_query_with_filter(
other_category = Category.objects.create(name="Other Category", slug="other-cat")
other_attribute = Attribute.objects.create(name="Other", slug="other")
other_product_type = ProductType.objects.create(
- name="Other type", has_variants=True, is_shipping_required=True
+ name="Other type",
+ has_variants=True,
+ is_shipping_required=True,
+ type=ProductTypeKind.NORMAL,
)
other_product_type.product_attributes.add(other_attribute)
diff --git a/saleor/graphql/attribute/tests/queries/test_attribute_filter.py b/saleor/graphql/attribute/tests/queries/test_attribute_filter.py
index c0feac1468..e35bbf3b61 100644
--- a/saleor/graphql/attribute/tests/queries/test_attribute_filter.py
+++ b/saleor/graphql/attribute/tests/queries/test_attribute_filter.py
@@ -5,6 +5,7 @@ import pytest
from .....attribute.models import Attribute
from .....attribute.utils import associate_attribute_values_to_instance
+from .....product import ProductTypeKind
from .....product.models import ProductType
from ....tests.utils import get_graphql_content, get_graphql_content_from_response
from ...filters import filter_attributes_by_product_types
@@ -134,6 +135,7 @@ def test_filter_attributes_in_category_invalid_category_id(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -175,6 +177,7 @@ def test_filter_attributes_in_category_object_with_given_id_does_not_exist(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -211,6 +214,7 @@ def test_filter_attributes_in_category_not_visible_in_listings_by_customer(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -260,6 +264,7 @@ def test_filter_attributes_in_category_not_visible_in_listings_by_staff_with_per
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -303,6 +308,7 @@ def test_filter_attributes_in_category_not_in_listings_by_staff_without_manage_p
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -349,6 +355,7 @@ def test_filter_attributes_in_category_not_visible_in_listings_by_app_with_perm(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -392,6 +399,7 @@ def test_filter_attributes_in_category_not_in_listings_by_app_without_manage_pro
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -432,6 +440,7 @@ def test_filter_attributes_in_category_not_published_by_customer(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -481,6 +490,7 @@ def test_filter_attributes_in_category_not_published_by_staff_with_perm(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -524,6 +534,7 @@ def test_filter_attributes_in_category_not_published_by_staff_without_manage_pro
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -570,6 +581,7 @@ def test_filter_attributes_in_category_not_published_by_app_with_perm(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -613,6 +625,7 @@ def test_filter_attributes_in_category_not_published_by_app_without_manage_produ
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -653,6 +666,7 @@ def test_filter_attributes_in_collection_invalid_category_id(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -697,6 +711,7 @@ def test_filter_attributes_in_collection_object_with_given_id_does_not_exist(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -736,6 +751,7 @@ def test_filter_attributes_in_collection_not_visible_in_listings_by_customer(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -778,6 +794,7 @@ def test_filter_in_collection_not_published_by_customer(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -830,6 +847,7 @@ def test_filter_in_collection_not_published_by_staff_with_perm(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -876,6 +894,7 @@ def test_filter_in_collection_not_published_by_staff_without_manage_products(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -925,6 +944,7 @@ def test_filter_in_collection_not_published_by_app_with_perm(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -971,6 +991,7 @@ def test_filter_in_collection_not_published_by_app_without_manage_products(
product_type = ProductType.objects.create(
name="Default Type 2",
slug="default-type-2",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
diff --git a/saleor/graphql/attribute/tests/queries/test_attribute_filtering_with_channels.py b/saleor/graphql/attribute/tests/queries/test_attribute_filtering_with_channels.py
index ecb35fa642..273d294bd6 100644
--- a/saleor/graphql/attribute/tests/queries/test_attribute_filtering_with_channels.py
+++ b/saleor/graphql/attribute/tests/queries/test_attribute_filtering_with_channels.py
@@ -4,6 +4,7 @@ import graphene
import pytest
from .....attribute.models import Attribute, AttributeProduct, AttributeVariant
+from .....product import ProductTypeKind
from .....product.models import (
Product,
ProductChannelListing,
@@ -53,7 +54,10 @@ def attributes_for_filtering_with_channels(
]
)
- product_type = ProductType.objects.create(name="My Product Type")
+ product_type = ProductType.objects.create(
+ name="My Product Type",
+ type=ProductTypeKind.NORMAL,
+ )
product = Product.objects.create(
name="Test product",
product_type=product_type,
diff --git a/saleor/graphql/attribute/tests/queries/test_attribute_pagination.py b/saleor/graphql/attribute/tests/queries/test_attribute_pagination.py
index 0671fc5b5f..0c568b20c5 100644
--- a/saleor/graphql/attribute/tests/queries/test_attribute_pagination.py
+++ b/saleor/graphql/attribute/tests/queries/test_attribute_pagination.py
@@ -5,6 +5,7 @@ import pytest
from .....attribute import AttributeType
from .....attribute.models import Attribute, AttributeProduct, AttributeVariant
+from .....product import ProductTypeKind
from .....product.models import (
Product,
ProductChannelListing,
@@ -57,7 +58,10 @@ def attributes_for_pagination(collection, category, channel_USD):
]
)
- product_type = ProductType.objects.create(name="My Product Type")
+ product_type = ProductType.objects.create(
+ name="My Product Type",
+ type=ProductTypeKind.NORMAL,
+ )
product = Product.objects.create(
name="Test product",
product_type=product_type,
diff --git a/saleor/graphql/attribute/tests/queries/test_attribute_query.py b/saleor/graphql/attribute/tests/queries/test_attribute_query.py
index 8204b65e16..67b6b93721 100644
--- a/saleor/graphql/attribute/tests/queries/test_attribute_query.py
+++ b/saleor/graphql/attribute/tests/queries/test_attribute_query.py
@@ -5,6 +5,7 @@ from graphene.utils.str_converters import to_camel_case
from .....attribute import AttributeInputType, AttributeType
from .....attribute.models import Attribute
+from .....product import ProductTypeKind
from .....product.models import Category, Collection, Product, ProductType
from .....tests.utils import dummy_editorjs
from ....tests.utils import (
@@ -532,7 +533,10 @@ def test_attributes_in_collection_query(
other_category = Category.objects.create(name="Other Category", slug="other-cat")
other_attribute = Attribute.objects.create(name="Other", slug="other")
other_product_type = ProductType.objects.create(
- name="Other type", has_variants=True, is_shipping_required=True
+ name="Other type",
+ has_variants=True,
+ is_shipping_required=True,
+ type=ProductTypeKind.NORMAL,
)
other_product_type.product_attributes.add(other_attribute)
other_product = Product.objects.create(
diff --git a/saleor/graphql/product/enums.py b/saleor/graphql/product/enums.py
index a55ea40502..c4ea9d57f9 100644
--- a/saleor/graphql/product/enums.py
+++ b/saleor/graphql/product/enums.py
@@ -1,5 +1,10 @@
import graphene
+from ...product import ProductTypeKind
+from ..core.enums import to_enum
+
+ProductTypeKindEnum = to_enum(ProductTypeKind)
+
class ProductAttributeType(graphene.Enum):
PRODUCT = "PRODUCT"
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 26f8e8402f..472148703e 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -50,6 +50,7 @@ from ...core.utils import (
)
from ...core.utils.reordering import perform_reordering
from ...warehouse.types import Warehouse
+from ..enums import ProductTypeKindEnum
from ..types import (
Category,
Collection,
@@ -1081,6 +1082,7 @@ class ProductVariantDelete(ModelDeleteMutation):
class ProductTypeInput(graphene.InputObjectType):
name = graphene.String(description="Name of the product type.")
slug = graphene.String(description="Product type slug.")
+ type = ProductTypeKindEnum(description="The product type type.")
has_variants = graphene.Boolean(
description=(
"Determines if product of this type has multiple variants. This option "
diff --git a/saleor/graphql/product/tests/test_attributes.py b/saleor/graphql/product/tests/test_attributes.py
index 9a72b3b0f1..cb08e4c61b 100644
--- a/saleor/graphql/product/tests/test_attributes.py
+++ b/saleor/graphql/product/tests/test_attributes.py
@@ -11,6 +11,7 @@ from ....attribute.models import (
AttributeVariant,
)
from ....attribute.utils import associate_attribute_values_to_instance
+from ....product import ProductTypeKind
from ....product.error_codes import ProductErrorCode
from ....product.models import ProductType
from ...attribute.enums import AttributeTypeEnum
@@ -294,7 +295,11 @@ def test_assign_attributes_to_product_type(
permission_manage_product_types_and_attributes,
product_type_attribute_list,
):
- product_type = ProductType.objects.create(name="Default Type", has_variants=True)
+ product_type = ProductType.objects.create(
+ name="Default Type",
+ has_variants=True,
+ type=ProductTypeKind.NORMAL,
+ )
product_type_global_id = graphene.Node.to_global_id("ProductType", product_type.pk)
query = PRODUCT_ASSIGN_ATTR_QUERY
@@ -349,7 +354,9 @@ def test_assign_non_existing_attributes_to_product_type(
permission_manage_product_types_and_attributes,
product_type_attribute_list,
):
- product_type = ProductType.objects.create(name="Default Type", has_variants=True)
+ product_type = ProductType.objects.create(
+ name="Default Type", has_variants=True, type=ProductTypeKind.NORMAL
+ )
product_type_global_id = graphene.Node.to_global_id("ProductType", product_type.pk)
query = PRODUCT_ASSIGN_ATTR_QUERY
@@ -458,7 +465,7 @@ def test_assign_attribute_to_product_type_having_already_that_attribute(
"""The assignAttribute mutation should raise an error when trying
to add an attribute already contained in the product type."""
- product_type = ProductType.objects.create(name="Type")
+ product_type = ProductType.objects.create(name="Type", type=ProductTypeKind.NORMAL)
attribute = color_attribute_without_values
staff_api_client.user.user_permissions.add(
permission_manage_product_types_and_attributes
@@ -538,7 +545,7 @@ def test_assign_attribute_to_product_type_multiple_errors_returned(
tag_page_attribute,
):
# given
- product_type = ProductType.objects.create(name="Type")
+ product_type = ProductType.objects.create(name="Type", type=ProductTypeKind.NORMAL)
staff_api_client.user.user_permissions.add(
permission_manage_product_types_and_attributes
)
@@ -625,7 +632,7 @@ def test_unassign_attributes_from_product_type(
permission_manage_product_types_and_attributes,
product_type_attribute_list,
):
- product_type = ProductType.objects.create(name="Type")
+ product_type = ProductType.objects.create(name="Type", type=ProductTypeKind.NORMAL)
product_type_global_id = graphene.Node.to_global_id("ProductType", product_type.pk)
variant_attribute, *product_attributes = product_type_attribute_list
@@ -675,7 +682,7 @@ def test_unassign_attributes_not_in_product_type(
permission_manage_product_types_and_attributes
)
- product_type = ProductType.objects.create(name="Type")
+ product_type = ProductType.objects.create(name="Type", type=ProductTypeKind.NORMAL)
product_type_global_id = graphene.Node.to_global_id("ProductType", product_type.pk)
query = PRODUCT_UNASSIGN_ATTR_QUERY
@@ -798,7 +805,9 @@ def test_sort_attributes_within_product_type_invalid_id(
):
"""Try to reorder an attribute not associated to the given product type."""
- product_type = ProductType.objects.create(name="Dummy Type")
+ product_type = ProductType.objects.create(
+ name="Dummy Type", type=ProductTypeKind.NORMAL
+ )
product_type_id = graphene.Node.to_global_id("ProductType", product_type.id)
attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.id)
@@ -849,7 +858,9 @@ def test_sort_attributes_within_product_type(
permission_manage_product_types_and_attributes
)
- product_type = ProductType.objects.create(name="Dummy Type")
+ product_type = ProductType.objects.create(
+ name="Dummy Type", type=ProductTypeKind.NORMAL
+ )
product_type_id = graphene.Node.to_global_id("ProductType", product_type.id)
m2m_attributes = getattr(product_type, relation_field)
m2m_attributes.set(attributes)
diff --git a/saleor/graphql/product/tests/test_bulk_delete.py b/saleor/graphql/product/tests/test_bulk_delete.py
index 58dba8a6aa..c6741d72c4 100644
--- a/saleor/graphql/product/tests/test_bulk_delete.py
+++ b/saleor/graphql/product/tests/test_bulk_delete.py
@@ -11,6 +11,7 @@ from ....checkout.utils import add_variant_to_checkout, calculate_checkout_quant
from ....order import OrderEvents, OrderStatus
from ....order.models import OrderEvent, OrderLine
from ....plugins.manager import get_plugins_manager
+from ....product import ProductTypeKind
from ....product.error_codes import ProductErrorCode
from ....product.models import (
Category,
@@ -37,9 +38,15 @@ def category_list():
@pytest.fixture
def product_type_list():
- product_type_1 = ProductType.objects.create(name="Type 1", slug="type-1")
- product_type_2 = ProductType.objects.create(name="Type 2", slug="type-2")
- product_type_3 = ProductType.objects.create(name="Type 3", slug="type-3")
+ product_type_1 = ProductType.objects.create(
+ name="Type 1", slug="type-1", type=ProductTypeKind.NORMAL
+ )
+ product_type_2 = ProductType.objects.create(
+ name="Type 2", slug="type-2", type=ProductTypeKind.NORMAL
+ )
+ product_type_3 = ProductType.objects.create(
+ name="Type 3", slug="type-3", type=ProductTypeKind.NORMAL
+ )
return product_type_1, product_type_2, product_type_3
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index 2a9a593994..0d7e4930e2 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -26,7 +26,7 @@ from ....core.units import WeightUnits
from ....order import OrderEvents, OrderStatus
from ....order.models import OrderEvent, OrderLine
from ....plugins.manager import PluginsManager, get_plugins_manager
-from ....product import ProductMediaTypes
+from ....product import ProductMediaTypes, ProductTypeKind
from ....product.error_codes import ProductErrorCode
from ....product.models import (
Category,
@@ -55,7 +55,7 @@ from ...tests.utils import (
get_multipart_request_body,
)
from ..bulk_mutations.products import ProductVariantStocksUpdate
-from ..enums import VariantAttributeScope
+from ..enums import ProductTypeKindEnum, VariantAttributeScope
from ..utils import create_stocks
@@ -1775,6 +1775,7 @@ def test_products_query_with_filter_attributes(
slug="custom-type",
has_variants=True,
is_shipping_required=True,
+ type=ProductTypeKind.NORMAL,
)
attribute = Attribute.objects.create(slug="new_attr", name="Attr")
attribute.product_types.add(product_type)
@@ -1837,6 +1838,7 @@ def test_products_query_with_filter_numeric_attributes(
product_type = ProductType.objects.create(
name="Custom Type",
slug="custom-type",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -1914,6 +1916,7 @@ def test_products_query_with_filter_boolean_attributes(
product_type = ProductType.objects.create(
name="Custom Type",
slug="custom-type",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -1973,6 +1976,7 @@ def test_products_query_with_filter_by_attributes_values_and_range(
product_type = ProductType.objects.create(
name="Custom Type",
slug="custom-type",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -7189,6 +7193,7 @@ PRODUCT_TYPE_CREATE_MUTATION = """
mutation createProductType(
$name: String,
$slug: String,
+ $type: ProductTypeKindEnum,
$taxCode: String,
$hasVariants: Boolean,
$isShippingRequired: Boolean,
@@ -7199,6 +7204,7 @@ PRODUCT_TYPE_CREATE_MUTATION = """
input: {
name: $name,
slug: $slug,
+ type: $type,
taxCode: $taxCode,
hasVariants: $hasVariants,
isShippingRequired: $isShippingRequired,
@@ -7208,6 +7214,7 @@ PRODUCT_TYPE_CREATE_MUTATION = """
productType {
name
slug
+ type
isShippingRequired
hasVariants
variantAttributes {
@@ -7260,6 +7267,7 @@ def test_product_type_create_mutation(
query = PRODUCT_TYPE_CREATE_MUTATION
product_type_name = "test type"
slug = "test-type"
+ type = ProductTypeKindEnum.NORMAL.name
has_variants = True
require_shipping = True
product_attributes = product_type.product_attributes.all()
@@ -7274,6 +7282,7 @@ def test_product_type_create_mutation(
variables = {
"name": product_type_name,
"slug": slug,
+ "type": type,
"hasVariants": has_variants,
"taxCode": "wine",
"isShippingRequired": require_shipping,
@@ -7289,6 +7298,73 @@ def test_product_type_create_mutation(
data = content["data"]["productTypeCreate"]["productType"]
assert data["name"] == product_type_name
assert data["slug"] == slug
+ assert data["type"] == type
+ assert data["hasVariants"] == has_variants
+ assert data["isShippingRequired"] == require_shipping
+
+ pa = product_attributes[0]
+ assert data["productAttributes"][0]["name"] == pa.name
+ pa_values = data["productAttributes"][0]["choices"]["edges"]
+ assert sorted([value["node"]["name"] for value in pa_values]) == sorted(
+ [value.name for value in pa.values.all()]
+ )
+
+ va = variant_attributes[0]
+ assert data["variantAttributes"][0]["name"] == va.name
+ va_values = data["variantAttributes"][0]["choices"]["edges"]
+ assert sorted([value["node"]["name"] for value in va_values]) == sorted(
+ [value.name for value in va.values.all()]
+ )
+
+ new_instance = ProductType.objects.latest("pk")
+ tax_code = manager.get_tax_code_from_object_meta(new_instance).code
+ assert tax_code == "wine"
+
+
+def test_create_gift_card_product_type(
+ staff_api_client,
+ product_type,
+ permission_manage_product_types_and_attributes,
+ monkeypatch,
+ setup_vatlayer,
+):
+ manager = PluginsManager(plugins=setup_vatlayer.PLUGINS)
+
+ query = PRODUCT_TYPE_CREATE_MUTATION
+ product_type_name = "test type"
+ slug = "test-type"
+ type = ProductTypeKindEnum.GIFT_CARD.name
+ has_variants = True
+ require_shipping = True
+ product_attributes = product_type.product_attributes.all()
+ product_attributes_ids = [
+ graphene.Node.to_global_id("Attribute", att.id) for att in product_attributes
+ ]
+ variant_attributes = product_type.variant_attributes.all()
+ variant_attributes_ids = [
+ graphene.Node.to_global_id("Attribute", att.id) for att in variant_attributes
+ ]
+
+ variables = {
+ "name": product_type_name,
+ "slug": slug,
+ "type": type,
+ "hasVariants": has_variants,
+ "taxCode": "wine",
+ "isShippingRequired": require_shipping,
+ "productAttributes": product_attributes_ids,
+ "variantAttributes": variant_attributes_ids,
+ }
+ initial_count = ProductType.objects.count()
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_product_types_and_attributes]
+ )
+ content = get_graphql_content(response)
+ assert ProductType.objects.count() == initial_count + 1
+ data = content["data"]["productTypeCreate"]["productType"]
+ assert data["name"] == product_type_name
+ assert data["slug"] == slug
+ assert data["type"] == type
assert data["hasVariants"] == has_variants
assert data["isShippingRequired"] == require_shipping
@@ -7330,6 +7406,7 @@ def test_create_product_type_with_rich_text_attribute(
variables = {
"name": product_type_name,
"slug": slug,
+ "type": ProductTypeKindEnum.NORMAL.name,
"productAttributes": product_attributes_ids,
}
@@ -7400,6 +7477,7 @@ def test_create_product_type_with_date_attribute(
variables = {
"name": product_type_name,
"slug": slug,
+ "type": ProductTypeKindEnum.NORMAL.name,
"productAttributes": product_attributes_ids,
}
@@ -7431,6 +7509,7 @@ def test_create_product_type_with_boolean_attribute(
query = PRODUCT_TYPE_CREATE_MUTATION
product_type_name = "test type"
slug = "test-type"
+ type = ProductTypeKindEnum.NORMAL.name
product_type.product_attributes.add(boolean_attribute)
product_attributes_ids = [
@@ -7441,6 +7520,7 @@ def test_create_product_type_with_boolean_attribute(
variables = {
"name": product_type_name,
"slug": slug,
+ "type": type,
"productAttributes": product_attributes_ids,
}
@@ -7454,6 +7534,7 @@ def test_create_product_type_with_boolean_attribute(
assert not errors
assert data["name"] == product_type_name
assert data["slug"] == slug
+ assert data["type"] == type
assert {"choices": {"edges": []}, "name": "Boolean"} in data["productAttributes"]
@@ -7474,7 +7555,11 @@ def test_create_product_type_with_given_slug(
):
query = PRODUCT_TYPE_CREATE_MUTATION
name = "Test product type"
- variables = {"name": name, "slug": input_slug}
+ variables = {
+ "name": name,
+ "slug": input_slug,
+ "type": ProductTypeKindEnum.NORMAL.name,
+ }
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_product_types_and_attributes]
)
@@ -7489,7 +7574,11 @@ def test_create_product_type_with_unicode_in_name(
):
query = PRODUCT_TYPE_CREATE_MUTATION
name = "わたし わ にっぽん です"
- variables = {"name": name}
+ type = ProductTypeKindEnum.NORMAL.name
+ variables = {
+ "name": name,
+ "type": type,
+ }
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_product_types_and_attributes]
)
@@ -7498,6 +7587,7 @@ def test_create_product_type_with_unicode_in_name(
assert not data["errors"]
assert data["productType"]["name"] == name
assert data["productType"]["slug"] == "わたし-わ-にっぽん-です"
+ assert data["productType"]["type"] == type
def test_create_product_type_create_with_negative_weight(
@@ -7505,7 +7595,11 @@ def test_create_product_type_create_with_negative_weight(
):
query = PRODUCT_TYPE_CREATE_MUTATION
name = "Test product type"
- variables = {"name": name, "weight": -1.1}
+ variables = {
+ "name": name,
+ "weight": -1.1,
+ "type": ProductTypeKindEnum.NORMAL.name,
+ }
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_product_types_and_attributes]
)
@@ -7550,6 +7644,7 @@ def test_product_type_create_mutation_not_valid_attributes(
variables = {
"name": product_type_name,
"slug": slug,
+ "type": ProductTypeKindEnum.NORMAL.name,
"hasVariants": has_variants,
"taxCode": "wine",
"isShippingRequired": require_shipping,
@@ -7907,6 +8002,47 @@ def test_update_product_type_with_negative_weight(
assert error["code"] == ProductErrorCode.INVALID.name
+def test_update_product_type_type(
+ staff_api_client,
+ product_type,
+ permission_manage_product_types_and_attributes,
+):
+ query = """
+ mutation($id: ID!, $type: ProductTypeKindEnum) {
+ productTypeUpdate(
+ id: $id
+ input: {
+ type: $type
+ }
+ ) {
+ productType{
+ name
+ type
+ }
+ errors {
+ field
+ message
+ code
+ }
+ }
+ }
+ """
+ type = ProductTypeKindEnum.GIFT_CARD.name
+ assert product_type.type != type
+
+ node_id = graphene.Node.to_global_id("ProductType", product_type.id)
+ variables = {"type": type, "id": node_id}
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_product_types_and_attributes]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productTypeUpdate"]
+ errors = data["errors"]
+ assert not errors
+ assert data["productType"]["type"] == type
+
+
PRODUCT_TYPE_DELETE_MUTATION = """
mutation deleteProductType($id: ID!) {
productTypeDelete(id: $id) {
@@ -9823,7 +9959,9 @@ def test_product_type_filter_unassigned_attributes(
):
expected_attribute = product_type_attribute_list[0]
query = QUERY_AVAILABLE_ATTRIBUTES
- product_type = ProductType.objects.create(name="Empty Type")
+ product_type = ProductType.objects.create(
+ name="Empty Type", type=ProductTypeKind.NORMAL
+ )
product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
filters = {"search": expected_attribute.name}
diff --git a/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.py b/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.py
index 6b34b37c60..ecb600ae88 100644
--- a/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.py
+++ b/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.py
@@ -4,6 +4,7 @@ from decimal import Decimal
import pytest
+from ....product import ProductTypeKind
from ....product.models import (
Product,
ProductChannelListing,
@@ -17,7 +18,7 @@ from ...tests.utils import assert_graphql_error_with_message, get_graphql_conten
@pytest.fixture
def products_for_sorting_with_channels(category, channel_USD, channel_PLN):
- product_type = ProductType.objects.create(name="Apple")
+ product_type = ProductType.objects.create(name="Apple", type=ProductTypeKind.NORMAL)
products = Product.objects.bulk_create(
[
Product(
diff --git a/saleor/graphql/product/tests/test_product_pagination.py b/saleor/graphql/product/tests/test_product_pagination.py
index 96cfde3aa1..24ab7b9a09 100644
--- a/saleor/graphql/product/tests/test_product_pagination.py
+++ b/saleor/graphql/product/tests/test_product_pagination.py
@@ -5,6 +5,7 @@ import graphene
import pytest
from ....attribute.utils import associate_attribute_values_to_instance
+from ....product import ProductTypeKind
from ....product.models import (
Category,
Collection,
@@ -305,7 +306,9 @@ def test_collections_pagination_with_filtering(
def products_for_pagination(
product_type, color_attribute, category, warehouse, channel_USD
):
- product_type2 = ProductType.objects.create(name="Apple")
+ product_type2 = ProductType.objects.create(
+ name="Apple", type=ProductTypeKind.NORMAL
+ )
products = Product.objects.bulk_create(
[
Product(
diff --git a/saleor/graphql/product/tests/test_product_sorting_attributes.py b/saleor/graphql/product/tests/test_product_sorting_attributes.py
index b7773dd64a..91a4ffa1bf 100644
--- a/saleor/graphql/product/tests/test_product_sorting_attributes.py
+++ b/saleor/graphql/product/tests/test_product_sorting_attributes.py
@@ -7,6 +7,7 @@ import pytest
from ....attribute import AttributeInputType, AttributeType
from ....attribute import models as attribute_models
from ....attribute.utils import associate_attribute_values_to_instance
+from ....product import ProductTypeKind
from ....product import models as product_models
from ...tests.utils import get_graphql_content
@@ -560,7 +561,7 @@ def test_sort_product_not_having_attribute_data(api_client, category, count_quer
name="Apples", slug="apples"
)
other_product_type = product_models.ProductType.objects.create(
- name="Chocolates", slug="chocolates"
+ name="Chocolates", slug="chocolates", type=ProductTypeKind.NORMAL
)
# Assign an attribute to the product type
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index eef0a29024..f4d97f5e95 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -970,6 +970,7 @@ class ProductType(CountableDjangoObjectType):
"is_shipping_required",
"name",
"slug",
+ "type",
"weight",
"tax_type",
]
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 5404a85cfd..4031e15a37 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -5376,6 +5376,7 @@ type ProductType implements Node & ObjectWithMetadata {
id: ID!
name: String!
slug: String!
+ type: ProductTypeType!
hasVariants: Boolean!
isShippingRequired: Boolean!
isDigital: Boolean!
@@ -5439,6 +5440,7 @@ input ProductTypeFilterInput {
input ProductTypeInput {
name: String
slug: String
+ type: ProductTypeKindEnum
hasVariants: Boolean
productAttributes: [ID]
variantAttributes: [ID]
@@ -5448,6 +5450,11 @@ input ProductTypeInput {
taxCode: String
}
+enum ProductTypeKindEnum {
+ NORMAL
+ GIFT_CARD
+}
+
type ProductTypeReorderAttributes {
productType: ProductType
productErrors: [ProductError!]! @deprecated(reason: "This field will be removed in Saleor 4.0. Use `errors` field instead.")
@@ -5465,6 +5472,11 @@ input ProductTypeSortingInput {
field: ProductTypeSortField!
}
+enum ProductTypeType {
+ NORMAL
+ GIFT_CARD
+}
+
type ProductTypeUpdate {
productErrors: [ProductError!]! @deprecated(reason: "This field will be removed in Saleor 4.0. Use `errors` field instead.")
errors: [ProductError!]!
diff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.py
index 1ecc2fe77e..de76b5f974 100644
--- a/saleor/plugins/avatax/tests/test_avatax.py
+++ b/saleor/plugins/avatax/tests/test_avatax.py
@@ -19,6 +19,7 @@ from ....checkout.fetch import (
from ....checkout.utils import add_variant_to_checkout
from ....core.prices import quantize_price
from ....core.taxes import TaxError, TaxType
+from ....product import ProductTypeKind
from ....product.models import Product, ProductType
from ...manager import get_plugins_manager
from ...models import PluginConfiguration
@@ -947,7 +948,9 @@ def test_get_checkout_line_tax_rate_for_product_type_with_non_taxable_product(
manager = get_plugins_manager()
- product_type = ProductType.objects.create(name="non-taxable")
+ product_type = ProductType.objects.create(
+ name="non-taxable", type=ProductTypeKind.NORMAL
+ )
product2 = product_with_two_variants
product2.product_type = product_type
manager.assign_tax_code_to_object_meta(product_type, "NT")
diff --git a/saleor/product/__init__.py b/saleor/product/__init__.py
index 602a59722b..00411a63a5 100644
--- a/saleor/product/__init__.py
+++ b/saleor/product/__init__.py
@@ -9,3 +9,13 @@ class ProductMediaTypes:
(IMAGE, "An uploaded image or an URL to an image"),
(VIDEO, "A URL to an external video"),
]
+
+
+class ProductTypeKind:
+ NORMAL = "normal"
+ GIFT_CARD = "gift_card"
+
+ CHOICES = [
+ (NORMAL, "A standard product type."),
+ (GIFT_CARD, "A gift card product type."),
+ ]
diff --git a/saleor/product/migrations/0148_producttype_type.py b/saleor/product/migrations/0148_producttype_type.py
new file mode 100644
index 0000000000..bf536734ce
--- /dev/null
+++ b/saleor/product/migrations/0148_producttype_type.py
@@ -0,0 +1,26 @@
+# Generated by Django 3.2.5 on 2021-07-28 07:52
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("product", "0147_auto_20210817_1015"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="producttype",
+ name="type",
+ field=models.CharField(
+ choices=[
+ ("normal", "A standard product type."),
+ ("gift_card", "A gift card product type."),
+ ],
+ default="normal",
+ max_length=32,
+ ),
+ preserve_default=False,
+ ),
+ ]
diff --git a/saleor/product/models.py b/saleor/product/models.py
index b8f196cc3d..5f75835838 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -49,7 +49,7 @@ from ..core.weight import zero_weight
from ..discount import DiscountInfo
from ..discount.utils import calculate_discounted_price
from ..seo.models import SeoModel, SeoModelTranslation
-from . import ProductMediaTypes
+from . import ProductMediaTypes, ProductTypeKind
if TYPE_CHECKING:
# flake8: noqa
@@ -119,6 +119,7 @@ class CategoryTranslation(SeoModelTranslation):
class ProductType(ModelWithMetadata):
name = models.CharField(max_length=250)
slug = models.SlugField(max_length=255, unique=True, allow_unicode=True)
+ type = models.CharField(max_length=32, choices=ProductTypeKind.CHOICES)
has_variants = models.BooleanField(default=True)
is_shipping_required = models.BooleanField(default=True)
is_digital = models.BooleanField(default=False)
diff --git a/saleor/product/tests/test_generate_and_set_variant_name.py b/saleor/product/tests/test_generate_and_set_variant_name.py
index 4f0b071175..065fe4339b 100644
--- a/saleor/product/tests/test_generate_and_set_variant_name.py
+++ b/saleor/product/tests/test_generate_and_set_variant_name.py
@@ -6,6 +6,7 @@ import pytest
from ...attribute import AttributeInputType
from ...attribute.models import AttributeValue
from ...attribute.utils import associate_attribute_values_to_instance
+from ...product import ProductTypeKind
from ...product.models import ProductVariantChannelListing
from ..models import Product, ProductType, ProductVariant
from ..tasks import _update_variants_names
@@ -16,7 +17,10 @@ from ..utils.variants import generate_and_set_variant_name
def variant_with_no_attributes(category, channel_USD):
"""Create a variant having no attributes, the same for the parent product."""
product_type = ProductType.objects.create(
- name="Test product type", has_variants=True, is_shipping_required=True
+ name="Test product type",
+ has_variants=True,
+ is_shipping_required=True,
+ type=ProductTypeKind.NORMAL,
)
product = Product.objects.create(
name="Test product",
diff --git a/saleor/product/tests/test_product.py b/saleor/product/tests/test_product.py
index bd9a2fefa8..1de59b2ad2 100644
--- a/saleor/product/tests/test_product.py
+++ b/saleor/product/tests/test_product.py
@@ -15,7 +15,7 @@ from ...graphql.product.filters import (
_clean_product_attributes_date_time_range_filter_input,
filter_products_by_attributes_values,
)
-from .. import models
+from .. import ProductTypeKind, models
from ..models import DigitalContentUrl
from ..thumbnails import create_product_thumbnails
from ..utils.costs import get_margin_for_variant_channel_listing
@@ -34,11 +34,17 @@ def test_filtering_by_attribute(
boolean_attribute,
):
product_type_a = models.ProductType.objects.create(
- name="New class", slug="new-class1", has_variants=True
+ name="New class",
+ slug="new-class1",
+ has_variants=True,
+ type=ProductTypeKind.NORMAL,
)
product_type_a.product_attributes.add(color_attribute)
product_type_b = models.ProductType.objects.create(
- name="New class", slug="new-class2", has_variants=True
+ name="New class",
+ slug="new-class2",
+ has_variants=True,
+ type=ProductTypeKind.NORMAL,
)
product_type_b.variant_attributes.add(color_attribute)
product_a = models.Product.objects.create(
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index c6f7550a85..dfdb14db72 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -80,7 +80,7 @@ from ..plugins.manager import get_plugins_manager
from ..plugins.models import PluginConfiguration
from ..plugins.vatlayer.plugin import VatlayerPlugin
from ..plugins.webhook.utils import to_payment_app_id
-from ..product import ProductMediaTypes
+from ..product import ProductMediaTypes, ProductTypeKind
from ..product.models import (
Category,
CategoryTranslation,
@@ -1443,6 +1443,7 @@ def product_type(color_attribute, size_attribute):
product_type = ProductType.objects.create(
name="Default Type",
slug="default-type",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -1458,6 +1459,7 @@ def product_type_with_rich_text_attribute(
product_type = ProductType.objects.create(
name="Default Type",
slug="default-type",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -1469,7 +1471,11 @@ def product_type_with_rich_text_attribute(
@pytest.fixture
def product_type_without_variant():
product_type = ProductType.objects.create(
- name="Type", slug="type", has_variants=False, is_shipping_required=True
+ name="Type",
+ slug="type",
+ has_variants=False,
+ is_shipping_required=True,
+ type=ProductTypeKind.NORMAL,
)
return product_type
@@ -1666,6 +1672,7 @@ def product_with_variant_with_two_attributes(
product_type = ProductType.objects.create(
name="Type with two variants",
slug="two-variants",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -1717,6 +1724,7 @@ def product_with_variant_with_external_media(
product_type = ProductType.objects.create(
name="Type with two variants",
slug="two-variants",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -1774,6 +1782,7 @@ def product_with_variant_with_file_attribute(
product_type = ProductType.objects.create(
name="Type with variant and file attribute",
slug="type-with-file-attribute",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=True,
)
@@ -1999,6 +2008,7 @@ def product_without_shipping(category, warehouse, channel_USD):
product_type = ProductType.objects.create(
name="Type with no shipping",
slug="no-shipping",
+ type=ProductTypeKind.NORMAL,
has_variants=False,
is_shipping_required=False,
)
@@ -3959,6 +3969,7 @@ def digital_content(category, media_root, warehouse, channel_USD) -> DigitalCont
product_type = ProductType.objects.create(
name="Digital Type",
slug="digital-type",
+ type=ProductTypeKind.NORMAL,
has_variants=True,
is_shipping_required=False,
is_digital=True,