Skip to content

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

Merge branch 'master' into feature/page-reference-attributes

IKarbowiak9d6478018b8d
diff --cc saleor/attribute/migrations/0005_add_assignedattributevalues.py
index 7df71da317,0000000000..f6fbb82c2f
mode 100644,000000..100644
--- a/saleor/attribute/migrations/0005_add_assignedattributevalues.py
+++ b/saleor/attribute/migrations/0005_add_assignedattributevalues.py
@@@ -1,216 -1,0 +1,225 @@@
+# Generated by Django 3.1.3 on 2020-12-09 10:30
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+
+def create_through_product_relations(apps, schema_editor):
+ AssignedProductAttribute = apps.get_model("attribute", "AssignedProductAttribute")
+ AssignedProductAttributeValue = apps.get_model(
+ "attribute", "AssignedProductAttributeValue"
+ )
+ for assignment in AssignedProductAttribute.objects.iterator():
+ instances = []
+ for value in assignment.values.all():
+ instances.append(
+ AssignedProductAttributeValue(value=value, assignment=assignment)
+ )
+ AssignedProductAttributeValue.objects.bulk_create(instances)
+
+
+def create_through_variant_relations(apps, schema_editor):
+ AssignedVariantAttribute = apps.get_model("attribute", "AssignedVariantAttribute")
+ AssignedVariantAttributeValue = apps.get_model(
+ "attribute", "AssignedVariantAttributeValue"
+ )
+ for assignment in AssignedVariantAttribute.objects.iterator():
+ instances = []
+ for value in assignment.values.all():
+ instances.append(
+ AssignedVariantAttributeValue(value=value, assignment=assignment)
+ )
+ AssignedVariantAttributeValue.objects.bulk_create(instances)
+
+
+def create_through_page_relations(apps, schema_editor):
+ AssignedPageAttribute = apps.get_model("attribute", "AssignedPageAttribute")
+ AssignedPageAttributeValue = apps.get_model(
+ "attribute", "AssignedPageAttributeValue"
+ )
+ for assignment in AssignedPageAttribute.objects.iterator():
+ instances = []
+ for value in assignment.values.all():
+ instances.append(
+ AssignedPageAttributeValue(value=value, assignment=assignment)
+ )
+ AssignedPageAttributeValue.objects.bulk_create(instances)
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("attribute", "0004_auto_20201204_1325"),
+ ]
+
+ operations = [
+ # create new through models
+ migrations.CreateModel(
+ name="AssignedProductAttributeValue",
+ fields=[
+ (
+ "id",
+ models.AutoField(
+ auto_created=True,
+ primary_key=True,
+ serialize=False,
+ verbose_name="ID",
+ ),
+ ),
+ (
+ "sort_order",
+ models.IntegerField(db_index=True, editable=False, null=True),
+ ),
+ (
+ "assignment",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="productvalueassignment",
+ to="attribute.assignedproductattribute",
+ ),
+ ),
+ (
+ "value",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="productvalueassignment",
+ to="attribute.attributevalue",
+ ),
+ ),
+ ],
+ options={
+ "ordering": ("sort_order", "pk"),
+ "unique_together": {("value", "assignment")},
+ },
+ ),
+ migrations.CreateModel(
+ name="AssignedVariantAttributeValue",
+ fields=[
+ (
+ "id",
+ models.AutoField(
+ auto_created=True,
+ primary_key=True,
+ serialize=False,
+ verbose_name="ID",
+ ),
+ ),
+ (
+ "sort_order",
+ models.IntegerField(db_index=True, editable=False, null=True),
+ ),
+ (
+ "assignment",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="variantvalueassignment",
+ to="attribute.assignedvariantattribute",
+ ),
+ ),
+ (
+ "value",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="variantvalueassignment",
+ to="attribute.attributevalue",
+ ),
+ ),
+ ],
+ options={
+ "ordering": ("sort_order", "pk"),
+ "unique_together": {("value", "assignment")},
+ },
+ ),
+ migrations.CreateModel(
+ name="AssignedPageAttributeValue",
+ fields=[
+ (
+ "id",
+ models.AutoField(
+ auto_created=True,
+ primary_key=True,
+ serialize=False,
+ verbose_name="ID",
+ ),
+ ),
+ (
+ "sort_order",
+ models.IntegerField(db_index=True, editable=False, null=True),
+ ),
+ (
+ "assignment",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="pagevalueassignment",
+ to="attribute.assignedpageattribute",
+ ),
+ ),
+ (
+ "value",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="pagevalueassignment",
+ to="attribute.attributevalue",
+ ),
+ ),
+ ],
+ options={
+ "ordering": ("sort_order", "pk"),
+ "unique_together": {("value", "assignment")},
+ },
+ ),
+ # migrate existing data to through model
+ migrations.RunPython(
+ create_through_product_relations, reverse_code=migrations.RunPython.noop
+ ),
+ migrations.RunPython(
+ create_through_variant_relations, reverse_code=migrations.RunPython.noop
+ ),
+ migrations.RunPython(
+ create_through_page_relations, reverse_code=migrations.RunPython.noop
+ ),
+ # remove current `values` relations
- migrations.RemoveField(model_name="assignedproductattribute", name="values",),
- migrations.RemoveField(model_name="assignedvariantattribute", name="values",),
- migrations.RemoveField(model_name="assignedpageattribute", name="values",),
++ migrations.RemoveField(
++ model_name="assignedproductattribute",
++ name="values",
++ ),
++ migrations.RemoveField(
++ model_name="assignedvariantattribute",
++ name="values",
++ ),
++ migrations.RemoveField(
++ model_name="assignedpageattribute",
++ name="values",
++ ),
+ # create new values relations with use of through models
+ migrations.AddField(
+ model_name="assignedproductattribute",
+ name="values",
+ field=models.ManyToManyField(
+ blank=True,
+ related_name="productassignments",
+ through="attribute.AssignedProductAttributeValue",
+ to="attribute.AttributeValue",
+ ),
+ ),
+ migrations.AddField(
+ model_name="assignedvariantattribute",
+ name="values",
+ field=models.ManyToManyField(
+ blank=True,
+ related_name="variantassignments",
+ through="attribute.AssignedVariantAttributeValue",
+ to="attribute.AttributeValue",
+ ),
+ ),
+ migrations.AddField(
+ model_name="assignedpageattribute",
+ name="values",
+ field=models.ManyToManyField(
+ blank=True,
+ related_name="pageassignments",
+ through="attribute.AssignedPageAttributeValue",
+ to="attribute.AttributeValue",
+ ),
+ ),
+ ]
diff --cc saleor/attribute/models/base.py
index 016468f6b2,8c761b3729..b67a84b088
--- a/saleor/attribute/models/base.py
+++ b/saleor/attribute/models/base.py
@@@ -3,17 -3,32 +3,18 @@@ from typing import TYPE_CHECKING, Unio
from django.db import models
from django.db.models import F, Q
-from ..core.models import ModelWithMetadata, SortableModel
-from ..core.permissions import ProductPermissions
-from ..core.utils.translations import TranslationProxy
-from ..page.models import Page, PageType
-from ..product.models import Product, ProductType, ProductVariant
-from . import AttributeInputType, AttributeType
+from ...core.models import ModelWithMetadata, SortableModel
+from ...core.permissions import ProductPermissions
+from ...core.utils.translations import TranslationProxy
+from ...page.models import PageType
+from ...product.models import ProductType
+from .. import AttributeEntityType, AttributeInputType, AttributeType
if TYPE_CHECKING:
- from ...account.models import User
from django.db.models import OrderBy
- from ..account.models import User
-
-
-class BaseAttributeQuerySet(models.QuerySet):
- @staticmethod
- def user_has_access_to_all(user: "User") -> bool:
- return user.is_active and user.has_perm(ProductPermissions.MANAGE_PRODUCTS)
-
- def get_public_attributes(self):
- raise NotImplementedError
-
- def get_visible_to_user(self, user: "User"):
- if self.user_has_access_to_all(user):
- return self.all()
- return self.get_public_attributes()
++ from ...account.models import User
+
class BaseAssignedAttribute(models.Model):
assignment = None
diff --cc saleor/attribute/models/page.py
index 87bdedb6b9,0000000000..96f8d1c623
mode 100644,000000..100644
--- a/saleor/attribute/models/page.py
+++ b/saleor/attribute/models/page.py
@@@ -1,66 -1,0 +1,68 @@@
+from django.db import models
+
+from ...core.models import SortableModel
+from ...page.models import Page, PageType
+from .base import AssociatedAttributeQuerySet, BaseAssignedAttribute
+
+
+class AssignedPageAttributeValue(SortableModel):
+ value = models.ForeignKey(
- "AttributeValue", on_delete=models.CASCADE, related_name="pagevalueassignment",
++ "AttributeValue",
++ on_delete=models.CASCADE,
++ related_name="pagevalueassignment",
+ )
+ assignment = models.ForeignKey(
+ "AssignedPageAttribute",
+ on_delete=models.CASCADE,
+ related_name="pagevalueassignment",
+ )
+
+ class Meta:
+ unique_together = (("value", "assignment"),)
+ ordering = ("sort_order", "pk")
+
+ def get_ordering_queryset(self):
+ return self.assignment.pagevalueassignment.all()
+
+
+class AssignedPageAttribute(BaseAssignedAttribute):
+ """Associate a page type attribute and selected values to a given page."""
+
+ page = models.ForeignKey(Page, related_name="attributes", on_delete=models.CASCADE)
+ assignment = models.ForeignKey(
+ "AttributePage", on_delete=models.CASCADE, related_name="pageassignments"
+ )
+ values = models.ManyToManyField(
+ "AttributeValue",
+ blank=True,
+ related_name="pageassignments",
+ through=AssignedPageAttributeValue,
+ )
+
+ class Meta:
+ unique_together = (("page", "assignment"),)
+
+
+class AttributePage(SortableModel):
+ attribute = models.ForeignKey(
+ "Attribute", related_name="attributepage", on_delete=models.CASCADE
+ )
+ page_type = models.ForeignKey(
+ PageType, related_name="attributepage", on_delete=models.CASCADE
+ )
+ assigned_pages = models.ManyToManyField(
+ Page,
+ blank=True,
+ through=AssignedPageAttribute,
+ through_fields=("assignment", "page"),
+ related_name="attributesrelated",
+ )
+
+ objects = AssociatedAttributeQuerySet.as_manager()
+
+ class Meta:
+ unique_together = (("attribute", "page_type"),)
+ ordering = ("sort_order", "pk")
+
+ def get_ordering_queryset(self):
+ return self.page_type.attributepage.all()
diff --cc saleor/graphql/attribute/types.py
index 5095888495,4aadf57486..17fd1208c7
--- a/saleor/graphql/attribute/types.py
+++ b/saleor/graphql/attribute/types.py
@@@ -75,23 -69,6 +75,23 @@@ class AttributeValue(CountableDjangoObj
return
return File(url=root.file_url, content_type=root.content_type)
+ @staticmethod
+ def resolve_reference(root: models.AttributeValue, info, **_kwargs):
+ def prepare_reference(attribute):
- if not attribute.input_type == AttributeInputType.REFERENCE:
++ if attribute.input_type != AttributeInputType.REFERENCE:
+ return
+ reference_pk = root.slug.split("_")[1]
+ reference_id = graphene.Node.to_global_id(
+ attribute.entity_type, reference_pk
+ )
+ return reference_id
+
+ return (
+ AttributesByAttributeId(info.context)
+ .load(root.attribute_id)
+ .then(prepare_reference)
+ )
+
class Attribute(CountableDjangoObjectType):
input_type = AttributeInputTypeEnum(description=AttributeDescriptions.INPUT_TYPE)
diff --cc saleor/graphql/attribute/utils.py
index 1d95371af1,08de5ee374..90b4b1e024
--- a/saleor/graphql/attribute/utils.py
+++ b/saleor/graphql/attribute/utils.py
@@@ -6,15 -5,10 +6,11 @@@ import graphen
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.utils.text import slugify
+from graphql.error import GraphQLError
from graphql_relay import from_global_id
- from ...attribute import (
- AttributeEntityType,
- AttributeInputType,
- AttributeType,
- models as attribute_models,
- )
-from ...attribute import AttributeInputType, AttributeType
++from ...attribute import AttributeEntityType, AttributeInputType, AttributeType
+ from ...attribute import models as attribute_models
from ...attribute.utils import associate_attribute_values_to_instance
from ...core.utils import generate_unique_slug
from ...page import models as page_models
diff --cc saleor/graphql/page/tests/test_page_reorder_attribute_values.py
index f0f0985e9c,0000000000..1ff69d595e
mode 100644,000000..100644
--- a/saleor/graphql/page/tests/test_page_reorder_attribute_values.py
+++ b/saleor/graphql/page/tests/test_page_reorder_attribute_values.py
@@@ -1,220 -1,0 +1,223 @@@
+import graphene
+
+from ....attribute.models import AttributeValue
+from ....attribute.utils import associate_attribute_values_to_instance
+from ....page.error_codes import PageErrorCode
+from ...tests.utils import get_graphql_content
+
+PAGE_REORDER_ATTRIBUTE_VALUES_MUTATION = """
+ mutation PageReorderAttributeValues(
+ $pageId: ID!
+ $attributeId: ID!
+ $moves: [ReorderInput]!
+ ) {
+ pageReorderAttributeValues(
+ pageId: $pageId
+ attributeId: $attributeId
+ moves: $moves
+ ) {
+ page {
+ id
+ attributes {
+ attribute {
+ id
+ slug
+ }
+ values {
+ id
+ }
+ }
+ }
+
+ pageErrors {
+ field
+ message
+ code
+ attributes
+ values
+ }
+ }
+ }
+"""
+
+
+def test_sort_page_attribute_values(
- staff_api_client, permission_manage_pages, page, page_reference_attribute,
++ staff_api_client,
++ permission_manage_pages,
++ page,
++ page_reference_attribute,
+):
+ staff_api_client.user.user_permissions.add(permission_manage_pages)
+
+ page_type = page.page_type
+ page_type.page_attributes.clear()
+ page_type.page_attributes.add(page_reference_attribute)
+
+ page_id = graphene.Node.to_global_id("Page", page.id)
+ attribute_id = graphene.Node.to_global_id("Attribute", page_reference_attribute.id)
+ attr_values = AttributeValue.objects.bulk_create(
+ [
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{page.pk}_1",
+ name="test name 1",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{page.pk}_2",
+ name="test name 2",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{page.pk}_3",
+ name="test name 3",
+ ),
+ ]
+ )
+ associate_attribute_values_to_instance(page, page_reference_attribute, *attr_values)
+
+ variables = {
+ "pageId": page_id,
+ "attributeId": attribute_id,
+ "moves": [
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", attr_values[0].pk),
+ "sortOrder": +1,
+ },
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", attr_values[2].pk),
+ "sortOrder": -2,
+ },
+ ],
+ }
+
+ expected_order = [attr_values[2].pk, attr_values[1].pk, attr_values[0].pk]
+
+ content = get_graphql_content(
+ staff_api_client.post_graphql(PAGE_REORDER_ATTRIBUTE_VALUES_MUTATION, variables)
+ )["data"]["pageReorderAttributeValues"]
+ assert not content["pageErrors"]
+
+ assert content["page"]["id"] == page_id, "Did not return the correct page"
+
+ gql_attribute_values = content["page"]["attributes"][0]["values"]
+ assert len(gql_attribute_values) == 3
+
+ for attr, expected_pk in zip(gql_attribute_values, expected_order):
+ db_type, value_pk = graphene.Node.from_global_id(attr["id"])
+ assert db_type == "AttributeValue"
+ assert int(value_pk) == expected_pk
+
+
+def test_sort_page_attribute_values_invalid_attribute_id(
+ staff_api_client,
+ permission_manage_pages,
+ page,
+ page_reference_attribute,
+ color_attribute,
+):
+ staff_api_client.user.user_permissions.add(permission_manage_pages)
+
+ page_type = page.page_type
+ page_type.page_attributes.clear()
+ page_type.page_attributes.add(page_reference_attribute)
+
+ page_id = graphene.Node.to_global_id("Page", page.id)
+ attr_values = AttributeValue.objects.bulk_create(
+ [
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{page.pk}_1",
+ name="test name 1",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{page.pk}_2",
+ name="test name 2",
+ ),
+ ]
+ )
+ associate_attribute_values_to_instance(page, page_reference_attribute, *attr_values)
+
+ variables = {
+ "pageId": page_id,
+ "attributeId": graphene.Node.to_global_id("Attribute", color_attribute.pk),
+ "moves": [
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", attr_values[0].pk),
+ "sortOrder": +1,
+ },
+ ],
+ }
+
+ content = get_graphql_content(
+ staff_api_client.post_graphql(PAGE_REORDER_ATTRIBUTE_VALUES_MUTATION, variables)
+ )["data"]["pageReorderAttributeValues"]
+ errors = content["pageErrors"]
+ assert not content["page"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == PageErrorCode.NOT_FOUND.name
+ assert errors[0]["field"] == "attributeId"
+
+
+def test_sort_page_attribute_values_invalid_value_id(
+ staff_api_client,
+ permission_manage_pages,
+ page,
+ page_reference_attribute,
+ size_page_attribute,
+):
+ staff_api_client.user.user_permissions.add(permission_manage_pages)
+
+ page_type = page.page_type
+ page_type.page_attributes.clear()
+ page_type.page_attributes.add(page_reference_attribute)
+
+ page_id = graphene.Node.to_global_id("Page", page.id)
+ attribute_id = graphene.Node.to_global_id("Attribute", page_reference_attribute.id)
+ attr_values = AttributeValue.objects.bulk_create(
+ [
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{page.pk}_1",
+ name="test name 1",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{page.pk}_2",
+ name="test name 2",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{page.pk}_3",
+ name="test name 3",
+ ),
+ ]
+ )
+ associate_attribute_values_to_instance(page, page_reference_attribute, *attr_values)
+
+ invalid_value_id = graphene.Node.to_global_id(
+ "AttributeValue", size_page_attribute.values.first().pk
+ )
+
+ variables = {
+ "pageId": page_id,
+ "attributeId": attribute_id,
+ "moves": [
+ {"id": invalid_value_id, "sortOrder": +1},
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", attr_values[2].pk),
+ "sortOrder": -1,
+ },
+ ],
+ }
+
+ content = get_graphql_content(
+ staff_api_client.post_graphql(PAGE_REORDER_ATTRIBUTE_VALUES_MUTATION, variables)
+ )["data"]["pageReorderAttributeValues"]
+ errors = content["pageErrors"]
+ assert not content["page"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == PageErrorCode.NOT_FOUND.name
+ assert errors[0]["field"] == "moves"
+ assert errors[0]["values"] == [invalid_value_id]
diff --cc saleor/graphql/product/mutations/attributes.py
index 584214266f,063f754d89..4ffb0bce29
--- a/saleor/graphql/product/mutations/attributes.py
+++ b/saleor/graphql/product/mutations/attributes.py
@@@ -6,16 -6,13 +6,17 @@@ from django.core.exceptions import Obje
from django.db import transaction
from django.db.models import Q
- from ....attribute import AttributeType, models as attribute_models
+ from ....attribute import AttributeType
+ from ....attribute import models as attribute_models
-from ....core.permissions import ProductTypePermissions
+from ....core.permissions import ProductPermissions, ProductTypePermissions
from ....product import models
from ....product.error_codes import ProductErrorCode
-from ...attribute.mutations import BaseReorderAttributesMutation
+from ...attribute.mutations import (
+ BaseReorderAttributesMutation,
+ BaseReorderAttributeValuesMutation,
+)
from ...attribute.types import Attribute
+from ...channel import ChannelContext
from ...core.inputs import ReorderInput
from ...core.mutations import BaseMutation
from ...core.types.common import ProductError
diff --cc saleor/graphql/product/tests/test_attributes.py
index 5b817f3e63,ba839c68ea..3080009234
--- a/saleor/graphql/product/tests/test_attributes.py
+++ b/saleor/graphql/product/tests/test_attributes.py
@@@ -896,462 -895,3 +896,468 @@@ def test_sort_attributes_within_product
gql_type, gql_attr_id = graphene.Node.from_global_id(attr["id"])
assert gql_type == "Attribute"
assert int(gql_attr_id) == expected_pk
+
+
+PRODUCT_REORDER_ATTRIBUTE_VALUES_MUTATION = """
+ mutation ProductReorderAttributeValues(
+ $productId: ID!
+ $attributeId: ID!
+ $moves: [ReorderInput]!
+ ) {
+ productReorderAttributeValues(
+ productId: $productId
+ attributeId: $attributeId
+ moves: $moves
+ ) {
+ product {
+ id
+ attributes {
+ attribute {
+ id
+ slug
+ }
+ values {
+ id
+ }
+ }
+ }
+
+ productErrors {
+ field
+ message
+ code
+ attributes
+ values
+ }
+ }
+ }
+"""
+
+
+def test_sort_product_attribute_values(
- staff_api_client, permission_manage_products, product, page_reference_attribute,
++ staff_api_client,
++ permission_manage_products,
++ product,
++ page_reference_attribute,
+):
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ product_type = product.product_type
+ product_type.product_attributes.clear()
+ product_type.product_attributes.add(page_reference_attribute)
+
+ product_id = graphene.Node.to_global_id("Product", product.id)
+ attribute_id = graphene.Node.to_global_id("Attribute", page_reference_attribute.id)
+ attr_values = AttributeValue.objects.bulk_create(
+ [
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{product.pk}_1",
+ name="test name 1",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{product.pk}_2",
+ name="test name 2",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{product.pk}_3",
+ name="test name 3",
+ ),
+ ]
+ )
+ associate_attribute_values_to_instance(
+ product, page_reference_attribute, *attr_values
+ )
+
+ variables = {
+ "productId": product_id,
+ "attributeId": attribute_id,
+ "moves": [
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", attr_values[0].pk),
+ "sortOrder": +1,
+ },
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", attr_values[2].pk),
+ "sortOrder": -1,
+ },
+ ],
+ }
+
+ expected_order = [attr_values[1].pk, attr_values[2].pk, attr_values[0].pk]
+
+ content = get_graphql_content(
+ staff_api_client.post_graphql(
+ PRODUCT_REORDER_ATTRIBUTE_VALUES_MUTATION, variables
+ )
+ )["data"]["productReorderAttributeValues"]
+ assert not content["productErrors"]
+
+ assert content["product"]["id"] == product_id, "Did not return the correct product"
+
+ gql_attribute_values = content["product"]["attributes"][0]["values"]
+ assert len(gql_attribute_values) == 3
+
+ for attr, expected_pk in zip(gql_attribute_values, expected_order):
+ db_type, value_pk = graphene.Node.from_global_id(attr["id"])
+ assert db_type == "AttributeValue"
+ assert int(value_pk) == expected_pk
+
+
+def test_sort_product_attribute_values_invalid_attribute_id(
+ staff_api_client,
+ permission_manage_products,
+ product,
+ page_reference_attribute,
+ color_attribute,
+):
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ product_type = product.product_type
+ product_type.product_attributes.clear()
+ product_type.product_attributes.add(page_reference_attribute)
+
+ product_id = graphene.Node.to_global_id("Product", product.id)
+ attr_values = AttributeValue.objects.bulk_create(
+ [
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{product.pk}_1",
+ name="test name 1",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{product.pk}_2",
+ name="test name 2",
+ ),
+ ]
+ )
+ associate_attribute_values_to_instance(
+ product, page_reference_attribute, *attr_values
+ )
+
+ variables = {
+ "productId": product_id,
+ "attributeId": graphene.Node.to_global_id("Attribute", color_attribute.pk),
+ "moves": [
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", attr_values[0].pk),
+ "sortOrder": +1,
+ },
+ ],
+ }
+
+ content = get_graphql_content(
+ staff_api_client.post_graphql(
+ PRODUCT_REORDER_ATTRIBUTE_VALUES_MUTATION, variables
+ )
+ )["data"]["productReorderAttributeValues"]
+ errors = content["productErrors"]
+ assert not content["product"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == ProductErrorCode.NOT_FOUND.name
+ assert errors[0]["field"] == "attributeId"
+
+
+def test_sort_product_attribute_values_invalid_value_id(
+ staff_api_client,
+ permission_manage_products,
+ product,
+ page_reference_attribute,
+ size_page_attribute,
+):
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ product_type = product.product_type
+ product_type.product_attributes.clear()
+ product_type.product_attributes.add(page_reference_attribute)
+
+ product_id = graphene.Node.to_global_id("Product", product.id)
+ attribute_id = graphene.Node.to_global_id("Attribute", page_reference_attribute.id)
+ attr_values = AttributeValue.objects.bulk_create(
+ [
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{product.pk}_1",
+ name="test name 1",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{product.pk}_2",
+ name="test name 2",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{product.pk}_3",
+ name="test name 3",
+ ),
+ ]
+ )
+ associate_attribute_values_to_instance(
+ product, page_reference_attribute, *attr_values
+ )
+
+ invalid_value_id = graphene.Node.to_global_id(
+ "AttributeValue", size_page_attribute.values.first().pk
+ )
+
+ variables = {
+ "productId": product_id,
+ "attributeId": attribute_id,
+ "moves": [
+ {"id": invalid_value_id, "sortOrder": +1},
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", attr_values[2].pk),
+ "sortOrder": -1,
+ },
+ ],
+ }
+
+ content = get_graphql_content(
+ staff_api_client.post_graphql(
+ PRODUCT_REORDER_ATTRIBUTE_VALUES_MUTATION, variables
+ )
+ )["data"]["productReorderAttributeValues"]
+ errors = content["productErrors"]
+ assert not content["product"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == ProductErrorCode.NOT_FOUND.name
+ assert errors[0]["field"] == "moves"
+ assert errors[0]["values"] == [invalid_value_id]
+
+
+PRODUCT_VARIANT_REORDER_ATTRIBUTE_VALUES_MUTATION = """
+ mutation ProductVariantReorderAttributeValues(
+ $variantId: ID!
+ $attributeId: ID!
+ $moves: [ReorderInput]!
+ ) {
+ productVariantReorderAttributeValues(
+ variantId: $variantId
+ attributeId: $attributeId
+ moves: $moves
+ ) {
+ productVariant {
+ id
+ attributes {
+ attribute {
+ id
+ slug
+ }
+ values {
+ id
+ }
+ }
+ }
+
+ productErrors {
+ field
+ message
+ code
+ attributes
+ values
+ }
+ }
+ }
+"""
+
+
+def test_sort_product_variant_attribute_values(
- staff_api_client, permission_manage_products, product, page_reference_attribute,
++ staff_api_client,
++ permission_manage_products,
++ product,
++ page_reference_attribute,
+):
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ variant = product.variants.first()
+ product_type = product.product_type
+ product_type.variant_attributes.clear()
+ product_type.variant_attributes.add(page_reference_attribute)
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ attribute_id = graphene.Node.to_global_id("Attribute", page_reference_attribute.id)
+ attr_values = AttributeValue.objects.bulk_create(
+ [
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{variant.pk}_1",
+ name="test name 1",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{variant.pk}_2",
+ name="test name 2",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{variant.pk}_3",
+ name="test name 3",
+ ),
+ ]
+ )
+ associate_attribute_values_to_instance(
+ variant, page_reference_attribute, *attr_values
+ )
+
+ variables = {
+ "variantId": variant_id,
+ "attributeId": attribute_id,
+ "moves": [
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", attr_values[0].pk),
+ "sortOrder": +1,
+ },
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", attr_values[2].pk),
+ "sortOrder": -1,
+ },
+ ],
+ }
+
+ expected_order = [attr_values[1].pk, attr_values[2].pk, attr_values[0].pk]
+
+ content = get_graphql_content(
+ staff_api_client.post_graphql(
+ PRODUCT_VARIANT_REORDER_ATTRIBUTE_VALUES_MUTATION, variables
+ )
+ )["data"]["productVariantReorderAttributeValues"]
+ assert not content["productErrors"]
+
+ assert (
+ content["productVariant"]["id"] == variant_id
+ ), "Did not return the correct product variant"
+
+ gql_attribute_values = content["productVariant"]["attributes"][0]["values"]
+ assert len(gql_attribute_values) == 3
+
+ for attr, expected_pk in zip(gql_attribute_values, expected_order):
+ db_type, value_pk = graphene.Node.from_global_id(attr["id"])
+ assert db_type == "AttributeValue"
+ assert int(value_pk) == expected_pk
+
+
+def test_sort_product_variant_attribute_values_invalid_attribute_id(
+ staff_api_client,
+ permission_manage_products,
+ product,
+ page_reference_attribute,
+ color_attribute,
+):
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ variant = product.variants.first()
+ product_type = product.product_type
+ product_type.variant_attributes.clear()
+ product_type.variant_attributes.add(page_reference_attribute)
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ attr_values = AttributeValue.objects.bulk_create(
+ [
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{variant.pk}_1",
+ name="test name 1",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{variant.pk}_2",
+ name="test name 2",
+ ),
+ ]
+ )
+ associate_attribute_values_to_instance(
+ variant, page_reference_attribute, *attr_values
+ )
+
+ variables = {
+ "variantId": variant_id,
+ "attributeId": graphene.Node.to_global_id("Attribute", color_attribute.pk),
+ "moves": [
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", attr_values[0].pk),
+ "sortOrder": +1,
+ },
+ ],
+ }
+
+ content = get_graphql_content(
+ staff_api_client.post_graphql(
+ PRODUCT_VARIANT_REORDER_ATTRIBUTE_VALUES_MUTATION, variables
+ )
+ )["data"]["productVariantReorderAttributeValues"]
+ errors = content["productErrors"]
+ assert not content["productVariant"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == ProductErrorCode.NOT_FOUND.name
+ assert errors[0]["field"] == "attributeId"
+
+
+def test_sort_product_variant_attribute_values_invalid_value_id(
+ staff_api_client,
+ permission_manage_products,
+ product,
+ page_reference_attribute,
+ size_page_attribute,
+):
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ variant = product.variants.first()
+ product_type = product.product_type
+ product_type.variant_attributes.clear()
+ product_type.variant_attributes.add(page_reference_attribute)
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ attribute_id = graphene.Node.to_global_id("Attribute", page_reference_attribute.id)
+ attr_values = AttributeValue.objects.bulk_create(
+ [
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{variant.pk}_1",
+ name="test name 1",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{variant.pk}_2",
+ name="test name 2",
+ ),
+ AttributeValue(
+ attribute=page_reference_attribute,
+ slug=f"{variant.pk}_3",
+ name="test name 3",
+ ),
+ ]
+ )
+ associate_attribute_values_to_instance(
+ variant, page_reference_attribute, *attr_values
+ )
+
+ invalid_value_id = graphene.Node.to_global_id(
+ "AttributeValue", size_page_attribute.values.first().pk
+ )
+
+ variables = {
+ "variantId": variant_id,
+ "attributeId": attribute_id,
+ "moves": [
+ {"id": invalid_value_id, "sortOrder": +1},
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", attr_values[2].pk),
+ "sortOrder": -1,
+ },
+ ],
+ }
+
+ content = get_graphql_content(
+ staff_api_client.post_graphql(
+ PRODUCT_VARIANT_REORDER_ATTRIBUTE_VALUES_MUTATION, variables
+ )
+ )["data"]["productVariantReorderAttributeValues"]
+ errors = content["productErrors"]
+ assert not content["productVariant"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == ProductErrorCode.NOT_FOUND.name
+ assert errors[0]["field"] == "moves"
+ assert errors[0]["values"] == [invalid_value_id]
diff --cc saleor/graphql/product/tests/test_product.py
index c432a1220e,459e981c12..47adbe442e
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@@ -2559,57 -2518,11 +2585,60 @@@ def test_create_product_with_file_attri
]
+def test_create_product_with_page_reference_attribute_required_no_references(
+ staff_api_client,
+ product_type,
+ category,
+ page_reference_attribute,
+ permission_manage_products,
+):
+ query = CREATE_PRODUCT_MUTATION
+
+ page_reference_attribute.value_required = True
+ page_reference_attribute.save(update_fields=["value_required"])
+
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ product_name = "test name"
+ product_slug = "product-test-slug"
+
+ # Add second attribute
+ product_type.product_attributes.add(page_reference_attribute)
+ reference_attr_id = graphene.Node.to_global_id(
+ "Attribute", page_reference_attribute.id
+ )
+
+ # test creating root product
+ variables = {
+ "input": {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name,
+ "slug": product_slug,
+ "attributes": [{"id": reference_attr_id, "references": []}],
+ }
+ }
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productCreate"]
+ errors = data["productErrors"]
+ assert not data["product"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == ProductErrorCode.REQUIRED.name
+ assert errors[0]["field"] == "attributes"
+ assert errors[0]["attributes"] == [
+ graphene.Node.to_global_id("Attribute", page_reference_attribute.pk)
+ ]
+
+
def test_create_product_no_values_given(
- staff_api_client, product_type, category, permission_manage_products,
+ staff_api_client,
+ product_type,
+ category,
+ permission_manage_products,
):
query = CREATE_PRODUCT_MUTATION
@@@ -2873,7 -2792,13 +2907,10 @@@ def test_create_product_no_slug_in_inpu
def test_create_product_no_category_id(
- staff_api_client, product_type, permission_manage_products, monkeypatch,
+ staff_api_client,
+ product_type,
- category,
- size_attribute,
- description_json,
+ permission_manage_products,
+ monkeypatch,
):
query = CREATE_PRODUCT_MUTATION
diff --cc saleor/graphql/product/tests/test_product_sorting_attributes.py
index 373d3cb15f,926886ddeb..b7773dd64a
--- a/saleor/graphql/product/tests/test_product_sorting_attributes.py
+++ b/saleor/graphql/product/tests/test_product_sorting_attributes.py
@@@ -4,7 -4,8 +4,8 @@@ from decimal import Decima
import graphene
import pytest
- from ....attribute import AttributeInputType, AttributeType, models as attribute_models
-from ....attribute import AttributeType
++from ....attribute import AttributeInputType, AttributeType
+ from ....attribute import models as attribute_models
from ....attribute.utils import associate_attribute_values_to_instance
from ....product import models as product_models
from ...tests.utils import get_graphql_content