Skip to content

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

Add text attribute input to API

Piotr Grundas16ef5ba8f814
diff --git a/saleor/attribute/__init__.py b/saleor/attribute/__init__.py
index 616582e2c6..8dbccf8eac 100644
--- a/saleor/attribute/__init__.py
+++ b/saleor/attribute/__init__.py
@@ -5,15 +5,17 @@ class AttributeInputType:
MULTISELECT = "multiselect"
FILE = "file"
REFERENCE = "reference"
+ TEXT = "text"
CHOICES = [
(DROPDOWN, "Dropdown"),
(MULTISELECT, "Multi Select"),
(FILE, "File"),
(REFERENCE, "Reference"),
+ (TEXT, "Text"),
]
# list of the input types that can be used in variant selection
- ALLOWED_IN_VARIANT_SELECTION = [DROPDOWN]
+ ALLOWED_IN_VARIANT_SELECTION = [DROPDOWN, TEXT]
class AttributeType:
diff --git a/saleor/attribute/migrations/0008_auto_20210325_1311.py b/saleor/attribute/migrations/0008_auto_20210325_1311.py
new file mode 100644
index 0000000000..58a81e0343
--- /dev/null
+++ b/saleor/attribute/migrations/0008_auto_20210325_1311.py
@@ -0,0 +1,18 @@
+# Generated by Django 3.1.7 on 2021-03-25 13:11
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('attribute', '0007_auto_20210308_1135'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='attribute',
+ name='input_type',
+ field=models.CharField(choices=[('dropdown', 'Dropdown'), ('multiselect', 'Multi Select'), ('file', 'File'), ('reference', 'Reference'), ('text', 'Text')], default='dropdown', max_length=50),
+ ),
+ ]
diff --git a/saleor/graphql/attribute/utils.py b/saleor/graphql/attribute/utils.py
index 955092ce97..caf067f1a9 100644
--- a/saleor/graphql/attribute/utils.py
+++ b/saleor/graphql/attribute/utils.py
@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Tuple, Union
import graphene
from django.core.exceptions import ValidationError
+from django.db import DataError
from django.db.models import Q
from django.utils.text import slugify
from graphql.error import GraphQLError
@@ -370,20 +371,36 @@ class AttributeAssignmentMixin:
:param instance: the product or variant to associate the attribute against.
:param cleaned_input: the cleaned user input (refer to clean_attributes)
"""
+ errors = []
+
for attribute, attr_values in cleaned_input:
- if attribute.input_type == AttributeInputType.FILE:
- attribute_values = cls._pre_save_file_value(
- instance, attribute, attr_values
+ try:
+ if attribute.input_type == AttributeInputType.FILE:
+ attribute_values = cls._pre_save_file_value(
+ instance, attribute, attr_values
+ )
+ elif attribute.input_type == AttributeInputType.REFERENCE:
+ attribute_values = cls._pre_save_reference_values(
+ instance, attribute, attr_values
+ )
+ else:
+ attribute_values = cls._pre_save_values(attribute, attr_values)
+
+ associate_attribute_values_to_instance(
+ instance, attribute, *attribute_values
)
- elif attribute.input_type == AttributeInputType.REFERENCE:
- attribute_values = cls._pre_save_reference_values(
- instance, attribute, attr_values
+
+ except DataError as e:
+ errors.append(
+ ValidationError(
+ str(e),
+ code="INVALID",
+ params={"attributes": [attr_values.global_id]},
+ )
)
- else:
- attribute_values = cls._pre_save_values(attribute, attr_values)
- associate_attribute_values_to_instance(
- instance, attribute, *attribute_values
- )
+
+ if errors:
+ raise ValidationError(errors)
def get_variant_selection_attributes(qs: "QuerySet"):
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 6f626e576d..b1e5b1e500 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -499,6 +499,7 @@ enum AttributeInputTypeEnum {
MULTISELECT
FILE
REFERENCE
+ TEXT
}
type AttributeReorderValues {