Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Split file product.types into smaller files
Maciej Korycinski3e6da5cb786d
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex 08e507352c..dc474bdcfb 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -1,8 +1,8 @@from textwrap import dedent-from django.core.exceptions import ValidationErrorimport grapheneimport graphene_django_optimizer as gql_optimizer+from django.core.exceptions import ValidationErrorfrom graphene import relayfrom ...order import models@@ -10,9 +10,10 @@ from ...order.models import FulfillmentStatusfrom ...product.templatetags.product_images import get_product_image_thumbnailfrom ..account.types import Userfrom ..core.connection import CountableDjangoObjectType+from ..core.types.common import Imagefrom ..core.types.money import Money, TaxedMoneyfrom ..payment.types import OrderAction, Payment, PaymentChargeStatusEnum-from ..product.types import Image, ProductVariant+from ..product.types import ProductVariantfrom ..shipping.types import ShippingMethodfrom .enums import OrderEventsEmailsEnum, OrderEventsEnumfrom .utils import applicable_shipping_methods, validate_draft_orderdiff --git a/saleor/graphql/product/types/__init__.py b/saleor/graphql/product/types/__init__.pynew file mode 100644index 0000000000..0d7d8bc184--- /dev/null+++ b/saleor/graphql/product/types/__init__.py@@ -0,0 +1,5 @@+from .attributes import Attribute, AttributeValue, SelectedAttribute+from .digital_contents import DigitalContent, DigitalContentUrl+from .input import ProductFilterInput, ProductOrder+from .products import (+ Category, Collection, Product, ProductImage, ProductType, ProductVariant)diff --git a/saleor/graphql/product/types/attributes.py b/saleor/graphql/product/types/attributes.pynew file mode 100644index 0000000000..cc0ae8b4e5--- /dev/null+++ b/saleor/graphql/product/types/attributes.py@@ -0,0 +1,95 @@+import re+from textwrap import dedent++import graphene+import graphene_django_optimizer as gql_optimizer+from graphene import relay++from ....product import models+from ...core.connection import CountableDjangoObjectType+from ...translations.enums import LanguageCodeEnum+from ...translations.resolvers import resolve_translation+from ...translations.types import (+ AttributeTranslation, AttributeValueTranslation)+from ..descriptions import AttributeDescriptions, AttributeValueDescriptions+from ..enums import AttributeValueType++COLOR_PATTERN = r'^(#[0-9a-fA-F]{3}|#(?:[0-9a-fA-F]{2}){2,4}|(rgb|hsl)a?\((-?\d+%?[,\s]+){2,3}\s*[\d\.]+%?\))$' # noqa+color_pattern = re.compile(COLOR_PATTERN)+++def resolve_attribute_value_type(attribute_value):+ if color_pattern.match(attribute_value):+ return AttributeValueType.COLOR+ if 'gradient(' in attribute_value:+ return AttributeValueType.GRADIENT+ if '://' in attribute_value:+ return AttributeValueType.URL+ return AttributeValueType.STRING+++class AttributeValue(CountableDjangoObjectType):+ name = graphene.String(description=AttributeValueDescriptions.NAME)+ slug = graphene.String(description=AttributeValueDescriptions.SLUG)+ type = AttributeValueType(description=AttributeValueDescriptions.TYPE)+ value = graphene.String(description=AttributeValueDescriptions.VALUE)+ translation = graphene.Field(+ AttributeValueTranslation,+ language_code=graphene.Argument(+ LanguageCodeEnum,+ description='A language code to return the translation for.',+ required=True),+ description=(+ 'Returns translated Attribute Value fields '+ 'for the given language code.'),+ resolver=resolve_translation)++ class Meta:+ description = 'Represents a value of an attribute.'+ only_fields = ['id', 'sort_order']+ interfaces = [relay.Node]+ model = models.AttributeValue++ def resolve_type(self, info):+ return resolve_attribute_value_type(self.value)+++class Attribute(CountableDjangoObjectType):+ name = graphene.String(description=AttributeDescriptions.NAME)+ slug = graphene.String(description=AttributeDescriptions.SLUG)+ values = gql_optimizer.field(+ graphene.List(+ AttributeValue, description=AttributeDescriptions.VALUES),+ model_field='values')+ translation = graphene.Field(+ AttributeTranslation,+ language_code=graphene.Argument(+ LanguageCodeEnum,+ description='A language code to return the translation for.',+ required=True),+ description=(+ 'Returns translated Attribute fields '+ 'for the given language code.'),+ resolver=resolve_translation)++ class Meta:+ description = dedent("""Custom attribute of a product. Attributes can be+ assigned to products and variants at the product type level.""")+ only_fields = ['id', 'product_type', 'product_variant_type']+ interfaces = [relay.Node]+ model = models.Attribute++ def resolve_values(self, info):+ return self.values.all()+++class SelectedAttribute(graphene.ObjectType):+ attribute = graphene.Field(+ Attribute, default_value=None, description=AttributeDescriptions.NAME,+ required=True)+ value = graphene.Field(+ AttributeValue, default_value=None,+ description='Value of an attribute.', required=True)++ class Meta:+ description = 'Represents a custom attribute.'diff --git a/saleor/graphql/product/types/digital_contents.py b/saleor/graphql/product/types/digital_contents.pynew file mode 100644index 0000000000..e46937d99a--- /dev/null+++ b/saleor/graphql/product/types/digital_contents.py@@ -0,0 +1,37 @@+import graphene+import graphene_django_optimizer as gql_optimizer+from graphene import relay+from ....product import models+from ...core.connection import CountableDjangoObjectType+++class DigitalContentUrl(CountableDjangoObjectType):+ url = graphene.String(description='Url for digital content')++ class Meta:+ model = models.DigitalContentUrl+ only_fields = ['content', 'created', 'download_num', 'token', 'url']+ interfaces = (relay.Node,)++ def resolve_url(self, info):+ return self.get_absolute_url()+++class DigitalContent(CountableDjangoObjectType):+ urls = gql_optimizer.field(+ graphene.List(+ lambda: DigitalContentUrl,+ description='List of urls for the digital variant'),+ model_field='urls')++ class Meta:+ model = models.DigitalContent+ only_fields = [+ 'automatic_fulfillment', 'content_file', 'max_downloads',+ 'product_variant', 'url_valid_days', 'urls',+ 'use_default_settings']+ interfaces = (relay.Node,)++ def resolve_urls(self, info, **kwargs):+ qs = self.urls.all()+ return gql_optimizer.query(qs, info)diff --git a/saleor/graphql/product/types/input.py b/saleor/graphql/product/types/input.pynew file mode 100644index 0000000000..71fe2ad602--- /dev/null+++ b/saleor/graphql/product/types/input.py@@ -0,0 +1,18 @@+import graphene+from ...core.types import FilterInputObjectType+from ..enums import OrderDirection, ProductOrderField+from ..filters import ProductFilter+++class ProductOrder(graphene.InputObjectType):+ field = graphene.Argument(+ ProductOrderField, required=True,+ description='Sort products by the selected field.')+ direction = graphene.Argument(+ OrderDirection, required=True,+ description='Specifies the direction in which to sort products')+++class ProductFilterInput(FilterInputObjectType):+ class Meta:+ filterset_class = ProductFilterdiff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types/products.pysimilarity index 78%rename from saleor/graphql/product/types.pyrename to saleor/graphql/product/types/products.pyindex cf50130dff..c152bd6be4 100644--- a/saleor/graphql/product/types.py+++ b/saleor/graphql/product/types/products.py@@ -1,4 +1,3 @@-import refrom textwrap import dedentimport graphene@@ -8,31 +7,39 @@ from graphene import relayfrom graphql.error import GraphQLErrorfrom graphql_jwt.decorators import permission_required-from ...product import models-from ...product.templatetags.product_images import (+from .digital_contents import DigitalContent+from ....product import models+from ....product.templatetags.product_images import (get_product_image_thumbnail, get_thumbnail)-from ...product.utils import calculate_revenue_for_variant-from ...product.utils.availability import get_availability-from ...product.utils.costs import (+from ....product.utils import calculate_revenue_for_variant+from ....product.utils.availability import get_availability+from ....product.utils.costs import (get_margin_for_variant, get_product_costs_data)-from ..core.connection import CountableDjangoObjectType-from ..core.enums import ReportingPeriod, TaxRateType-from ..core.fields import PrefetchingConnectionField-from ..core.types import (- FilterInputObjectType, Image, Money, MoneyRange, TaxedMoney,- TaxedMoneyRange)-from ..translations.enums import LanguageCodeEnum-from ..translations.resolvers import resolve_translation-from ..translations.types import (- AttributeTranslation, AttributeValueTranslation, CategoryTranslation,- CollectionTranslation, ProductTranslation, ProductVariantTranslation)-from ..utils import get_database_id, reporting_period_to_date-from .descriptions import AttributeDescriptions, AttributeValueDescriptions-from .enums import AttributeValueType, OrderDirection, ProductOrderField-from .filters import ProductFilter--COLOR_PATTERN = r'^(#[0-9a-fA-F]{3}|#(?:[0-9a-fA-F]{2}){2,4}|(rgb|hsl)a?\((-?\d+%?[,\s]+){2,3}\s*[\d\.]+%?\))$' # noqa-color_pattern = re.compile(COLOR_PATTERN)+from ...core.connection import CountableDjangoObjectType+from ...core.enums import ReportingPeriod, TaxRateType+from ...core.fields import PrefetchingConnectionField+from ...core.types import Image, Money, MoneyRange, TaxedMoney, TaxedMoneyRange+from ...translations.enums import LanguageCodeEnum+from ...translations.resolvers import resolve_translation+from ...translations.types import (+ CategoryTranslation, CollectionTranslation, ProductTranslation,+ ProductVariantTranslation)+from ...utils import get_database_id, reporting_period_to_date+from .attributes import SelectedAttribute, Attribute+++def prefetch_products(info, *args, **kwargs):+ """Prefetch products visible to the current user.++ Can be used with models that have the `products` relationship. Queryset of+ products being prefetched is filtered based on permissions of the viewing+ user, to restrict access to unpublished products to non-staff users.+ """+ user = info.context.user+ qs = models.Product.objects.visible_to_user(user)+ return Prefetch(+ 'products', queryset=gql_optimizer.query(qs, info),+ to_attr='prefetched_products')def resolve_attribute_list(attributes_hstore, attributes_qs):@@ -60,129 +67,11 @@ def resolve_attribute_list(attributes_hstore, attributes_qs):return attributes_list-def resolve_attribute_value_type(attribute_value):- if color_pattern.match(attribute_value):- return AttributeValueType.COLOR- if 'gradient(' in attribute_value:- return AttributeValueType.GRADIENT- if '://' in attribute_value:- return AttributeValueType.URL- return AttributeValueType.STRING---class AttributeValue(CountableDjangoObjectType):- name = graphene.String(description=AttributeValueDescriptions.NAME)- slug = graphene.String(description=AttributeValueDescriptions.SLUG)- type = AttributeValueType(description=AttributeValueDescriptions.TYPE)- value = graphene.String(description=AttributeValueDescriptions.VALUE)- translation = graphene.Field(- AttributeValueTranslation,- language_code=graphene.Argument(- LanguageCodeEnum,- description='A language code to return the translation for.',- required=True),- description=(- 'Returns translated Attribute Value fields '- 'for the given language code.'),- resolver=resolve_translation)-- class Meta:- description = 'Represents a value of an attribute.'- only_fields = ['id', 'sort_order']- interfaces = [relay.Node]- model = models.AttributeValue-- def resolve_type(self, info):- return resolve_attribute_value_type(self.value)---class Attribute(CountableDjangoObjectType):- name = graphene.String(description=AttributeDescriptions.NAME)- slug = graphene.String(description=AttributeDescriptions.SLUG)- values = gql_optimizer.field(- graphene.List(- AttributeValue, description=AttributeDescriptions.VALUES),- model_field='values')- translation = graphene.Field(- AttributeTranslation,- language_code=graphene.Argument(- LanguageCodeEnum,- description='A language code to return the translation for.',- required=True),- description=(- 'Returns translated Attribute fields '- 'for the given language code.'),- resolver=resolve_translation)-- class Meta:- description = dedent("""Custom attribute of a product. Attributes can be- assigned to products and variants at the product type level.""")- only_fields = ['id', 'product_type', 'product_variant_type']- interfaces = [relay.Node]- model = models.Attribute-- def resolve_values(self, info):- return self.values.all()--class Margin(graphene.ObjectType):start = graphene.Int()stop = graphene.Int()-class SelectedAttribute(graphene.ObjectType):- attribute = graphene.Field(- Attribute, default_value=None, description=AttributeDescriptions.NAME,- required=True)- value = graphene.Field(- AttributeValue, default_value=None,- description='Value of an attribute.', required=True)-- class Meta:- description = 'Represents a custom attribute.'---class DigitalContentUrl(CountableDjangoObjectType):- url = graphene.String(description='Url for digital content')-- class Meta:- model = models.DigitalContentUrl- only_fields = ['content', 'created', 'download_num', 'token', 'url']- interfaces = (relay.Node,)-- def resolve_url(self, info):- return self.get_absolute_url()---class DigitalContent(CountableDjangoObjectType):- urls = gql_optimizer.field(- graphene.List(- lambda: DigitalContentUrl,- description='List of urls for the digital variant'),- model_field='urls')-- class Meta:- model = models.DigitalContent- only_fields = [- 'automatic_fulfillment', 'content_file', 'max_downloads',- 'product_variant', 'url_valid_days', 'urls',- 'use_default_settings']- interfaces = (relay.Node,)-- def resolve_urls(self, info, **kwargs):- qs = self.urls.all()- return gql_optimizer.query(qs, info)---class ProductOrder(graphene.InputObjectType):- field = graphene.Argument(- ProductOrderField, required=True,- description='Sort products by the selected field.')- direction = graphene.Argument(- OrderDirection, required=True,- description='Specifies the direction in which to sort products')--class ProductVariant(CountableDjangoObjectType):stock_quantity = graphene.Int(required=True, description='Quantity of a product available for sale.')@@ -306,11 +195,6 @@ class ProductAvailability(graphene.ObjectType):description = 'Represents availability of a product in the storefront.'-class ProductFilterInput(FilterInputObjectType):- class Meta:- filterset_class = ProductFilter--class Product(CountableDjangoObjectType):url = graphene.String(description='The storefront URL for the product.', required=True)@@ -455,20 +339,6 @@ class Product(CountableDjangoObjectType):return None-def prefetch_products(info, *args, **kwargs):- """Prefetch products visible to the current user.-- Can be used with models that have the `products` relationship. Queryset of- products being prefetched is filtered based on permissions of the viewing- user, to restrict access to unpublished products to non-staff users.- """- user = info.context.user- qs = models.Product.objects.visible_to_user(user)- return Prefetch(- 'products', queryset=gql_optimizer.query(qs, info),- to_attr='prefetched_products')--class ProductType(CountableDjangoObjectType):products = gql_optimizer.field(PrefetchingConnectionField(diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.pyindex cceec3f5f8..aadeb679b4 100644--- a/tests/api/test_attributes.py+++ b/tests/api/test_attributes.py@@ -4,7 +4,8 @@ from django.db.models import Qfrom django.template.defaultfilters import slugifyfrom saleor.graphql.product.enums import AttributeTypeEnum, AttributeValueType-from saleor.graphql.product.types import resolve_attribute_value_type+from saleor.graphql.product.types.attributes import (+ resolve_attribute_value_type)from saleor.graphql.product.utils import attributes_to_hstorefrom saleor.product.models import Attribute, AttributeValue, Categoryfrom tests.api.utils import get_graphql_contentdiff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex a8ddb8c208..107a93f1a1 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -11,7 +11,7 @@ from prices import Moneyfrom saleor.graphql.core.enums import ReportingPeriodfrom saleor.graphql.product.enums import StockAvailability-from saleor.graphql.product.types import resolve_attribute_list+from saleor.graphql.product.types.products import resolve_attribute_listfrom saleor.product.models import (Attribute, AttributeValue, Category, Product, ProductImage, ProductType,ProductVariant)