Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move managers out from models.py in product (#13495)
Ania Warzecha6f47b3e052f6
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex 3fca181746..574aa1fcf3 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -41,8 +41,7 @@ from ...permission.enums import (ProductPermissions,)from ...permission.utils import has_one_of_permissions-from ...product import ProductMediaTypes-from ...product.models import ALL_PRODUCTS_PERMISSIONS+from ...product.models import ALL_PRODUCTS_PERMISSIONS, ProductMediaTypesfrom ...shipping.interface import ShippingMethodDatafrom ...shipping.models import ShippingMethodChannelListingfrom ...shipping.utils import convert_to_shipping_method_datadiff --git a/saleor/product/managers.py b/saleor/product/managers.pynew file mode 100644index 0000000000..c2d81fb9a8--- /dev/null+++ b/saleor/product/managers.py@@ -0,0 +1,303 @@+import datetime+from typing import Union++import pytz+from django.contrib.postgres.aggregates import StringAgg+from django.db import models+from django.db.models import (+ BooleanField,+ Case,+ Count,+ DateTimeField,+ Exists,+ ExpressionWrapper,+ F,+ FilteredRelation,+ OuterRef,+ Q,+ Subquery,+ Sum,+ Value,+ When,+)+from django.db.models.functions import Coalesce++from ..account.models import User+from ..app.models import App+from ..channel.models import Channel+from ..permission.utils import has_one_of_permissions+++class ProductsQueryset(models.QuerySet):+ def published(self, channel_slug: str):+ from .models import ProductChannelListing++ today = datetime.datetime.now(pytz.UTC)+ channels = Channel.objects.filter(+ slug=str(channel_slug), is_active=True+ ).values("id")+ channel_listings = ProductChannelListing.objects.filter(+ Q(published_at__lte=today) | Q(published_at__isnull=True),+ Exists(channels.filter(pk=OuterRef("channel_id"))),+ is_published=True,+ ).values("id")+ return self.filter(Exists(channel_listings.filter(product_id=OuterRef("pk"))))++ def not_published(self, channel_slug: str):+ today = datetime.datetime.now(pytz.UTC)+ return self.annotate_publication_info(channel_slug).filter(+ Q(published_at__gt=today) & Q(is_published=True)+ | Q(is_published=False)+ | Q(is_published__isnull=True)+ )++ def published_with_variants(self, channel_slug: str):+ from .models import ProductVariant, ProductVariantChannelListing++ published = self.published(channel_slug)+ channels = Channel.objects.filter(+ slug=str(channel_slug), is_active=True+ ).values("id")+ variant_channel_listings = ProductVariantChannelListing.objects.filter(+ Exists(channels.filter(pk=OuterRef("channel_id"))),+ price_amount__isnull=False,+ ).values("id")+ variants = ProductVariant.objects.filter(+ Exists(variant_channel_listings.filter(variant_id=OuterRef("pk")))+ )+ return published.filter(Exists(variants.filter(product_id=OuterRef("pk"))))++ def visible_to_user(self, requestor: Union["User", "App", None], channel_slug: str):+ from .models import ALL_PRODUCTS_PERMISSIONS, ProductChannelListing++ if has_one_of_permissions(requestor, ALL_PRODUCTS_PERMISSIONS):+ if channel_slug:+ channels = Channel.objects.filter(slug=str(channel_slug)).values("id")+ channel_listings = ProductChannelListing.objects.filter(+ Exists(channels.filter(pk=OuterRef("channel_id")))+ ).values("id")+ return self.filter(+ Exists(channel_listings.filter(product_id=OuterRef("pk")))+ )+ return self.all()+ return self.published_with_variants(channel_slug)++ def annotate_publication_info(self, channel_slug: str):+ return self.annotate_is_published(channel_slug).annotate_published_at(+ channel_slug+ )++ def annotate_is_published(self, channel_slug: str):+ from .models import ProductChannelListing++ query = Subquery(+ ProductChannelListing.objects.filter(+ product_id=OuterRef("pk"), channel__slug=str(channel_slug)+ ).values_list("is_published")[:1]+ )+ return self.annotate(+ is_published=ExpressionWrapper(query, output_field=BooleanField())+ )++ def annotate_published_at(self, channel_slug: str):+ from .models import ProductChannelListing++ query = Subquery(+ ProductChannelListing.objects.filter(+ product_id=OuterRef("pk"), channel__slug=str(channel_slug)+ ).values_list("published_at")[:1]+ )+ return self.annotate(+ published_at=ExpressionWrapper(query, output_field=DateTimeField())+ )++ def annotate_visible_in_listings(self, channel_slug):+ from .models import ProductChannelListing++ query = Subquery(+ ProductChannelListing.objects.filter(+ product_id=OuterRef("pk"), channel__slug=str(channel_slug)+ ).values_list("visible_in_listings")[:1]+ )+ return self.annotate(+ visible_in_listings=ExpressionWrapper(query, output_field=BooleanField())+ )++ def sort_by_attribute(+ self, attribute_pk: Union[int, str], descending: bool = False+ ):+ """Sort a query set by the values of the given product attribute.++ :param attribute_pk: The database ID (must be a numeric) of the attribute+ to sort by.+ :param descending: The sorting direction.+ """+ from ..attribute.models import AttributeProduct, AttributeValue++ qs: models.QuerySet = self+ # If the passed attribute ID is valid, execute the sorting+ if not (isinstance(attribute_pk, int) or attribute_pk.isnumeric()):+ return qs.annotate(+ concatenated_values_order=Value(+ None, output_field=models.IntegerField()+ ),+ concatenated_values=Value(None, output_field=models.CharField()),+ )++ # Retrieve all the products' attribute data IDs (assignments) and+ # product types that have the given attribute associated to them+ associated_values = tuple(+ AttributeProduct.objects.filter(attribute_id=attribute_pk).values_list(+ "pk", "product_type_id"+ )+ )++ if not associated_values:+ qs = qs.annotate(+ concatenated_values_order=Value(+ None, output_field=models.IntegerField()+ ),+ concatenated_values=Value(None, output_field=models.CharField()),+ )++ else:+ attribute_associations, product_types_associated_to_attribute = zip(+ *associated_values+ )++ qs = qs.annotate(+ # Contains to retrieve the attribute data (singular) of each product+ # Refer to `AttributeProduct`.+ filtered_attribute=FilteredRelation(+ relation_name="attributes",+ condition=Q(attributes__assignment_id__in=attribute_associations),+ ),+ # Implicit `GROUP BY` required for the `StringAgg` aggregation+ grouped_ids=Count("id"),+ # String aggregation of the attribute's values to efficiently sort them+ concatenated_values=Case(+ # If the product has no association data but has+ # the given attribute associated to its product type,+ # then consider the concatenated values as empty (non-null).+ When(+ Q(product_type_id__in=product_types_associated_to_attribute)+ & Q(filtered_attribute=None),+ then=models.Value(""),+ ),+ default=StringAgg(+ F("filtered_attribute__values__name"),+ delimiter=",",+ ordering=(+ [+ f"filtered_attribute__values__{field_name}"+ for field_name in AttributeValue._meta.ordering or []+ ]+ ),+ ),+ output_field=models.CharField(),+ ),+ concatenated_values_order=Case(+ # Make the products having no such attribute be last in the sorting+ When(concatenated_values=None, then=2),+ # Put the products having an empty attribute value at the bottom of+ # the other products.+ When(concatenated_values="", then=1),+ # Put the products having an attribute value to be always at the top+ default=0,+ output_field=models.IntegerField(),+ ),+ )++ # Sort by concatenated_values_order then+ # Sort each group of products (0, 1, 2, ...) per attribute values+ # Sort each group of products by name,+ # if they have the same values or not values+ ordering = "-" if descending else ""+ return qs.order_by(+ f"{ordering}concatenated_values_order",+ f"{ordering}concatenated_values",+ f"{ordering}name",+ )++ def prefetched_for_webhook(self, single_object=True):+ common_fields = (+ "attributes__values",+ "attributes__assignment__attribute",+ "media",+ "variants__attributes__values",+ "variants__attributes__assignment__attribute",+ "variants__variant_media__media",+ "variants__stocks__allocations",+ "variants__channel_listings__channel",+ "channel_listings__channel",+ )+ if single_object:+ return self.prefetch_related(*common_fields)+ return self.prefetch_related("collections", "category", *common_fields)+++ProductManager = models.Manager.from_queryset(ProductsQueryset)+++class ProductVariantQueryset(models.QuerySet):+ def annotate_quantities(self):+ return self.annotate(+ quantity=Coalesce(Sum("stocks__quantity"), 0),+ quantity_allocated=Coalesce(+ Sum("stocks__allocations__quantity_allocated"), 0+ ),+ )++ def available_in_channel(self, channel_slug):+ return self.filter(+ channel_listings__price_amount__isnull=False,+ channel_listings__channel__slug=str(channel_slug),+ )++ def prefetched_for_webhook(self):+ return self.prefetch_related(+ "attributes__values",+ "attributes__assignment__attribute",+ "variant_media__media",+ )+++ProductVariantManager = models.Manager.from_queryset(ProductVariantQueryset)+++class ProductVariantChannelListingQuerySet(models.QuerySet):+ def annotate_preorder_quantity_allocated(self):+ return self.annotate(+ preorder_quantity_allocated=Coalesce(+ Sum("preorder_allocations__quantity"), 0+ ),+ )+++ProductVariantChannelListingManager = models.Manager.from_queryset(+ ProductVariantChannelListingQuerySet+)+++class CollectionsQueryset(models.QuerySet):+ def published(self, channel_slug: str):+ today = datetime.datetime.now(pytz.UTC)+ return self.filter(+ Q(channel_listings__published_at__lte=today)+ | Q(channel_listings__published_at__isnull=True),+ channel_listings__channel__slug=str(channel_slug),+ channel_listings__channel__is_active=True,+ channel_listings__is_published=True,+ )++ def visible_to_user(self, requestor: Union["User", "App", None], channel_slug: str):+ from .models import ALL_PRODUCTS_PERMISSIONS++ if has_one_of_permissions(requestor, ALL_PRODUCTS_PERMISSIONS):+ if channel_slug:+ return self.filter(channel_listings__channel__slug=str(channel_slug))+ return self.all()+ return self.published(channel_slug)+++CollectionManager = models.Manager.from_queryset(CollectionsQueryset)diff --git a/saleor/product/models.py b/saleor/product/models.pyindex 3ec0dd2f36..8bb389fa35 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -1,34 +1,15 @@import datetime-from typing import TYPE_CHECKING, Iterable, Optional, Union+from typing import TYPE_CHECKING, Iterable, Optionalfrom uuid import uuid4import grapheneimport pytzfrom django.conf import settings-from django.contrib.postgres.aggregates import StringAggfrom django.contrib.postgres.indexes import BTreeIndex, GinIndexfrom django.contrib.postgres.search import SearchVectorFieldfrom django.core.validators import MinValueValidatorfrom django.db import models, transaction-from django.db.models import (- BooleanField,- Case,- Count,- DateTimeField,- Exists,- ExpressionWrapper,- F,- FilteredRelation,- JSONField,- OuterRef,- Q,- Subquery,- Sum,- TextField,- Value,- When,-)-from django.db.models.functions import Coalesce+from django.db.models import JSONField, TextFieldfrom django.urls import reversefrom django.utils import timezonefrom django_measurement.models import MeasurementField@@ -59,16 +40,13 @@ from ..permission.enums import (ProductPermissions,ProductTypePermissions,)-from ..permission.utils import has_one_of_permissionsfrom ..seo.models import SeoModel, SeoModelTranslationfrom ..tax.models import TaxClass-from . import ProductMediaTypes, ProductTypeKind+from . import ProductMediaTypes, ProductTypeKind, managersif TYPE_CHECKING:from decimal import Decimal- from ..account.models import User- from ..app.models import AppALL_PRODUCTS_PERMISSIONS = [# List of permissions, where each of them allows viewing all products@@ -198,205 +176,6 @@ class ProductType(ModelWithMetadata):)-class ProductsQueryset(models.QuerySet["Product"]):- def published(self, channel_slug: str):- today = datetime.datetime.now(pytz.UTC)- channels = Channel.objects.filter(- slug=str(channel_slug), is_active=True- ).values("id")- channel_listings = ProductChannelListing.objects.filter(- Q(published_at__lte=today) | Q(published_at__isnull=True),- Exists(channels.filter(pk=OuterRef("channel_id"))),- is_published=True,- ).values("id")- return self.filter(Exists(channel_listings.filter(product_id=OuterRef("pk"))))-- def not_published(self, channel_slug: str):- today = datetime.datetime.now(pytz.UTC)- return self.annotate_publication_info(channel_slug).filter(- Q(published_at__gt=today) & Q(is_published=True)- | Q(is_published=False)- | Q(is_published__isnull=True)- )-- def published_with_variants(self, channel_slug: str):- published = self.published(channel_slug)- channels = Channel.objects.filter(- slug=str(channel_slug), is_active=True- ).values("id")- variant_channel_listings = ProductVariantChannelListing.objects.filter(- Exists(channels.filter(pk=OuterRef("channel_id"))),- price_amount__isnull=False,- ).values("id")- variants = ProductVariant.objects.filter(- Exists(variant_channel_listings.filter(variant_id=OuterRef("pk")))- )- return published.filter(Exists(variants.filter(product_id=OuterRef("pk"))))-- def visible_to_user(self, requestor: Union["User", "App", None], channel_slug: str):- if has_one_of_permissions(requestor, ALL_PRODUCTS_PERMISSIONS):- if channel_slug:- channels = Channel.objects.filter(slug=str(channel_slug)).values("id")- channel_listings = ProductChannelListing.objects.filter(- Exists(channels.filter(pk=OuterRef("channel_id")))- ).values("id")- return self.filter(- Exists(channel_listings.filter(product_id=OuterRef("pk")))- )- return self.all()- return self.published_with_variants(channel_slug)-- def annotate_publication_info(self, channel_slug: str):- return self.annotate_is_published(channel_slug).annotate_published_at(- channel_slug- )-- def annotate_is_published(self, channel_slug: str):- query = Subquery(- ProductChannelListing.objects.filter(- product_id=OuterRef("pk"), channel__slug=str(channel_slug)- ).values_list("is_published")[:1]- )- return self.annotate(- is_published=ExpressionWrapper(query, output_field=BooleanField())- )-- def annotate_published_at(self, channel_slug: str):- query = Subquery(- ProductChannelListing.objects.filter(- product_id=OuterRef("pk"), channel__slug=str(channel_slug)- ).values_list("published_at")[:1]- )- return self.annotate(- published_at=ExpressionWrapper(query, output_field=DateTimeField())- )-- def annotate_visible_in_listings(self, channel_slug):- query = Subquery(- ProductChannelListing.objects.filter(- product_id=OuterRef("pk"), channel__slug=str(channel_slug)- ).values_list("visible_in_listings")[:1]- )- return self.annotate(- visible_in_listings=ExpressionWrapper(query, output_field=BooleanField())- )-- def sort_by_attribute(- self, attribute_pk: Union[int, str], descending: bool = False- ):- """Sort a query set by the values of the given product attribute.-- :param attribute_pk: The database ID (must be a numeric) of the attribute- to sort by.- :param descending: The sorting direction.- """- from ..attribute.models import AttributeProduct, AttributeValue-- qs: models.QuerySet = self- # If the passed attribute ID is valid, execute the sorting- if not (isinstance(attribute_pk, int) or attribute_pk.isnumeric()):- return qs.annotate(- concatenated_values_order=Value(- None, output_field=models.IntegerField()- ),- concatenated_values=Value(None, output_field=models.CharField()),- )-- # Retrieve all the products' attribute data IDs (assignments) and- # product types that have the given attribute associated to them- associated_values = tuple(- AttributeProduct.objects.filter(attribute_id=attribute_pk).values_list(- "pk", "product_type_id"- )- )-- if not associated_values:- qs = qs.annotate(- concatenated_values_order=Value(- None, output_field=models.IntegerField()- ),- concatenated_values=Value(None, output_field=models.CharField()),- )-- else:- attribute_associations, product_types_associated_to_attribute = zip(- *associated_values- )-- qs = qs.annotate(- # Contains to retrieve the attribute data (singular) of each product- # Refer to `AttributeProduct`.- filtered_attribute=FilteredRelation(- relation_name="attributes",- condition=Q(attributes__assignment_id__in=attribute_associations),- ),- # Implicit `GROUP BY` required for the `StringAgg` aggregation- grouped_ids=Count("id"),- # String aggregation of the attribute's values to efficiently sort them- concatenated_values=Case(- # If the product has no association data but has- # the given attribute associated to its product type,- # then consider the concatenated values as empty (non-null).- When(- Q(product_type_id__in=product_types_associated_to_attribute)- & Q(filtered_attribute=None),- then=models.Value(""),- ),- default=StringAgg(- F("filtered_attribute__values__name"),- delimiter=",",- ordering=(- [- f"filtered_attribute__values__{field_name}"- for field_name in AttributeValue._meta.ordering or []- ]- ),- ),- output_field=models.CharField(),- ),- concatenated_values_order=Case(- # Make the products having no such attribute be last in the sorting- When(concatenated_values=None, then=2),- # Put the products having an empty attribute value at the bottom of- # the other products.- When(concatenated_values="", then=1),- # Put the products having an attribute value to be always at the top- default=0,- output_field=models.IntegerField(),- ),- )-- # Sort by concatenated_values_order then- # Sort each group of products (0, 1, 2, ...) per attribute values- # Sort each group of products by name,- # if they have the same values or not values- ordering = "-" if descending else ""- return qs.order_by(- f"{ordering}concatenated_values_order",- f"{ordering}concatenated_values",- f"{ordering}name",- )-- def prefetched_for_webhook(self, single_object=True):- common_fields = (- "attributes__values",- "attributes__assignment__attribute",- "media",- "variants__attributes__values",- "variants__attributes__assignment__attribute",- "variants__variant_media__media",- "variants__stocks__allocations",- "variants__channel_listings__channel",- "channel_listings__channel",- )- if single_object:- return self.prefetch_related(*common_fields)- return self.prefetch_related("collections", "category", *common_fields)---ProductManager = models.Manager.from_queryset(ProductsQueryset)--class Product(SeoModel, ModelWithMetadata, ModelWithExternalReference):product_type = models.ForeignKey(ProductType, related_name="products", on_delete=models.CASCADE@@ -440,7 +219,7 @@ class Product(SeoModel, ModelWithMetadata, ModelWithExternalReference):on_delete=models.SET_NULL,)- objects = ProductManager()+ objects = managers.ProductManager()class Meta:app_label = "product"@@ -529,29 +308,6 @@ class ProductTranslation(SeoModelTranslation):return translated_keys-class ProductVariantQueryset(models.QuerySet["ProductVariant"]):- def annotate_quantities(self):- return self.annotate(- quantity=Coalesce(Sum("stocks__quantity"), 0),- quantity_allocated=Coalesce(- Sum("stocks__allocations__quantity_allocated"), 0- ),- )-- def available_in_channel(self, channel_slug):- return self.filter(- channel_listings__price_amount__isnull=False,- channel_listings__channel__slug=str(channel_slug),- )-- def prefetched_for_webhook(self):- return self.prefetch_related(- "attributes__values",- "attributes__assignment__attribute",- "variant_media__media",- )--class ProductChannelListing(PublishableModel):product = models.ForeignKey(Product,@@ -595,9 +351,6 @@ class ProductChannelListing(PublishableModel):)-ProductVariantManager = models.Manager.from_queryset(ProductVariantQueryset)--class ProductVariant(SortableModel, ModelWithMetadata, ModelWithExternalReference):sku = models.CharField(max_length=255, unique=True, null=True, blank=True)name = models.CharField(max_length=255, blank=True)@@ -622,7 +375,7 @@ class ProductVariant(SortableModel, ModelWithMetadata, ModelWithExternalReferencnull=True,)- objects = ProductVariantManager()+ objects = managers.ProductVariantManager()class Meta(ModelWithMetadata.Meta):ordering = ("sort_order", "sku")@@ -727,22 +480,6 @@ class ProductVariantTranslation(Translation):return {"name": self.name}-class ProductVariantChannelListingQuerySet(- models.QuerySet["ProductVariantChannelListing"]-):- def annotate_preorder_quantity_allocated(self):- return self.annotate(- preorder_quantity_allocated=Coalesce(- Sum("preorder_allocations__quantity"), 0- ),- )---ProductVariantChannelListingManager = models.Manager.from_queryset(- ProductVariantChannelListingQuerySet-)--class ProductVariantChannelListing(models.Model):variant = models.ForeignKey(ProductVariant,@@ -787,7 +524,7 @@ class ProductVariantChannelListing(models.Model):preorder_quantity_threshold = models.IntegerField(blank=True, null=True)- objects = ProductVariantChannelListingManager()+ objects = managers.ProductVariantChannelListingManager()class Meta:unique_together = [["variant", "channel"]]@@ -900,28 +637,6 @@ class CollectionProduct(SortableModel):return self.product.collectionproduct.all()-class CollectionsQueryset(models.QuerySet["Collection"]):- def published(self, channel_slug: str):- today = datetime.datetime.now(pytz.UTC)- return self.filter(- Q(channel_listings__published_at__lte=today)- | Q(channel_listings__published_at__isnull=True),- channel_listings__channel__slug=str(channel_slug),- channel_listings__channel__is_active=True,- channel_listings__is_published=True,- )-- def visible_to_user(self, requestor: Union["User", "App", None], channel_slug: str):- if has_one_of_permissions(requestor, ALL_PRODUCTS_PERMISSIONS):- if channel_slug:- return self.filter(channel_listings__channel__slug=str(channel_slug))- return self.all()- return self.published(channel_slug)---CollectionManager = models.Manager.from_queryset(CollectionsQueryset)--class Collection(SeoModel, ModelWithMetadata):name = models.CharField(max_length=250)slug = models.SlugField(max_length=255, unique=True, allow_unicode=True)@@ -939,7 +654,7 @@ class Collection(SeoModel, ModelWithMetadata):description = SanitizedJSONField(blank=True, null=True, sanitizer=clean_editor_js)- objects = CollectionManager()+ objects = managers.CollectionManager()class Meta(ModelWithMetadata.Meta):ordering = ("slug",)