Skip to content

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

Move page attribute related logic to page module (#18015)

Maciej Korycinskif4cf223ac3f6
diff --git a/saleor/graphql/attribute/shared_filters.py b/saleor/graphql/attribute/shared_filters.py
index 118f50d951..ca39977dbd 100644
--- a/saleor/graphql/attribute/shared_filters.py
+++ b/saleor/graphql/attribute/shared_filters.py
@@ -1,7 +1,7 @@
from typing import Literal, TypedDict
import graphene
-from django.db.models import Exists, OuterRef, Q, QuerySet
+from django.db.models import Exists, OuterRef, QuerySet
from graphql import GraphQLError
from ...attribute import AttributeInputType
@@ -98,482 +98,6 @@ class SharedContainsFilterParams(TypedDict):
identifier_field_name: Literal["slug", "id", "sku"]
-def filter_by_contains_referenced_object_ids(
- attr_id: int | None,
- attr_value: CONTAINS_TYPING,
- db_connection_name: str,
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
-):
- """Build a filter expression for objects referencing other entities by global IDs.
-
- Returns a Q expression to filter objects based on their references
- to other entities (like: variants, products, pages), identified by
- global IDs.
-
- - If `contains_all` is provided, only objects that reference all of the
- specified global IDs will match.
- - If `contains_any` is provided, objects that reference at least one of
- the specified global IDs will match.
- """
-
- contains_all = attr_value.get("contains_all")
- contains_any = attr_value.get("contains_any")
-
- variant_ids = set()
- product_ids = set()
- page_ids = set()
-
- for obj_id in contains_any or contains_all or []:
- type_, id_ = graphene.Node.from_global_id(obj_id)
- if type_ == "Page":
- page_ids.add(id_)
- elif type_ == "Product":
- product_ids.add(id_)
- elif type_ == "ProductVariant":
- variant_ids.add(id_)
-
- expression = Q()
- shared_filter_params: SharedContainsFilterParams = {
- "attr_id": attr_id,
- "db_connection_name": db_connection_name,
- "assigned_attr_model": assigned_attr_model,
- "assigned_id_field_name": assigned_id_field_name,
- "identifier_field_name": "id",
- }
- if contains_all:
- if page_ids:
- expression &= _filter_contains_all_condition(
- contains_all=list(page_ids),
- referenced_model=page_models.Page,
- attr_value_reference_field_name="reference_page_id",
- **shared_filter_params,
- )
- if product_ids:
- expression &= _filter_contains_all_condition(
- contains_all=list(product_ids),
- referenced_model=product_models.Product,
- attr_value_reference_field_name="reference_product_id",
- **shared_filter_params,
- )
- if variant_ids:
- expression &= _filter_contains_all_condition(
- contains_all=list(variant_ids),
- referenced_model=product_models.ProductVariant,
- attr_value_reference_field_name="reference_variant_id",
- **shared_filter_params,
- )
- return expression
-
- if contains_any:
- if page_ids:
- expression |= _filter_contains_any_condition(
- contains_any=list(page_ids),
- referenced_model=page_models.Page,
- attr_value_reference_field_name="reference_page_id",
- **shared_filter_params,
- )
-
- if product_ids:
- expression |= _filter_contains_any_condition(
- contains_any=list(product_ids),
- referenced_model=product_models.Product,
- attr_value_reference_field_name="reference_product_id",
- **shared_filter_params,
- )
-
- if variant_ids:
- expression |= _filter_contains_any_condition(
- contains_any=list(variant_ids),
- referenced_model=product_models.ProductVariant,
- attr_value_reference_field_name="reference_variant_id",
- **shared_filter_params,
- )
- return expression
-
-
-def _filter_contains_single_expression(
- attr_id: int | None,
- db_connection_name: str,
- reference_objs: QuerySet[
- page_models.Page | product_models.Product | product_models.ProductVariant
- ],
- attr_value_reference_field_name: Literal[
- "reference_page_id", "reference_product_id", "reference_variant_id"
- ],
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
-):
- single_reference_qs = AttributeValue.objects.using(db_connection_name).filter(
- Exists(reference_objs.filter(id=OuterRef(attr_value_reference_field_name))),
- )
- if attr_id:
- attr_query = Attribute.objects.using(db_connection_name).filter(id=attr_id)
- single_reference_qs = single_reference_qs.filter(
- Exists(attr_query.filter(id=OuterRef("attribute_id"))),
- )
- assigned_attr_value = assigned_attr_model.objects.using(db_connection_name).filter(
- Exists(single_reference_qs.filter(id=OuterRef("value_id"))),
- **{str(assigned_id_field_name): OuterRef("id")},
- )
- return Q(Exists(assigned_attr_value))
-
-
-def _filter_contains_all_condition(
- attr_id: int | None,
- db_connection_name: str,
- contains_all: list[str],
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
- identifier_field_name: Literal["slug", "id", "sku"],
- referenced_model: type[
- page_models.Page | product_models.Product | product_models.ProductVariant
- ],
- attr_value_reference_field_name: Literal[
- "reference_page_id", "reference_product_id", "reference_variant_id"
- ],
-):
- """Build a filter expression that ensures all specified references are present.
-
- Constructs a Q expression that checks for references to all entities from
- `referenced_model`, matched using the provided identifiers in `contains_all`.
-
- For each identifier, it resolves the corresponding object using
- `identifier_field_name` and adds a subquery to verify the presence
- of that reference. The subqueries are combined using logical AND.
- """
-
- identifiers = contains_all
- expression = Q()
-
- for identifier in identifiers:
- reference_obj = referenced_model.objects.using(db_connection_name).filter(
- **{str(identifier_field_name): identifier}
- )
- expression &= _filter_contains_single_expression(
- attr_id,
- db_connection_name,
- reference_obj,
- attr_value_reference_field_name,
- assigned_attr_model,
- assigned_id_field_name,
- )
- return expression
-
-
-def _filter_contains_any_condition(
- attr_id: int | None,
- db_connection_name: str,
- contains_any: list[str],
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
- identifier_field_name: Literal["slug", "id", "sku"],
- referenced_model: type[
- page_models.Page | product_models.Product | product_models.ProductVariant
- ],
- attr_value_reference_field_name: Literal[
- "reference_page_id", "reference_product_id", "reference_variant_id"
- ],
-):
- """Build a filter expression that ensures at least one specified reference is present.
-
- Constructs a Q expression that checks for a reference to any entity from
- `referenced_model`, matched using the provided identifiers in `contains_any`.
-
- All matching references are resolved using `identifier_field_name`,
- and passed as a single queryset to be checked in a single subquery.
-
- """
- identifiers = contains_any
- reference_objs = referenced_model.objects.using(db_connection_name).filter(
- **{f"{identifier_field_name}__in": identifiers}
- )
- return _filter_contains_single_expression(
- attr_id,
- db_connection_name,
- reference_objs,
- attr_value_reference_field_name,
- assigned_attr_model,
- assigned_id_field_name,
- )
-
-
-def filter_by_contains_referenced_pages(
- attr_id: int | None,
- attr_value: CONTAINS_TYPING,
- db_connection_name: str,
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
-):
- """Build a filter expression for referenced pages.
-
- Returns a Q expression to filter objects based on their references
- to pages.
-
- - If `contains_all` is provided, only objects that reference all of the
- specified pages will match.
- - If `contains_any` is provided, objects that reference at least one of
- the specified pages will match.
- """
- contains_all = attr_value.get("contains_all")
- contains_any = attr_value.get("contains_any")
-
- shared_filter_params: SharedContainsFilterParams = {
- "attr_id": attr_id,
- "db_connection_name": db_connection_name,
- "assigned_attr_model": assigned_attr_model,
- "assigned_id_field_name": assigned_id_field_name,
- "identifier_field_name": "slug",
- }
- if contains_all:
- return _filter_contains_all_condition(
- contains_all=contains_all,
- referenced_model=page_models.Page,
- attr_value_reference_field_name="reference_page_id",
- **shared_filter_params,
- )
-
- if contains_any:
- return _filter_contains_any_condition(
- contains_any=contains_any,
- referenced_model=page_models.Page,
- attr_value_reference_field_name="reference_page_id",
- **shared_filter_params,
- )
- return Q()
-
-
-def filter_by_contains_referenced_products(
- attr_id: int | None,
- attr_value: CONTAINS_TYPING,
- db_connection_name: str,
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
-):
- """Build a filter expression for referenced products.
-
- Returns a Q expression to filter objects based on their references
- to products.
-
- - If `contains_all` is provided, only objects that reference all of the
- specified products will match.
- - If `contains_any` is provided, objects that reference at least one of
- the specified products will match.
- """
- contains_all = attr_value.get("contains_all")
- contains_any = attr_value.get("contains_any")
-
- shared_filter_params: SharedContainsFilterParams = {
- "attr_id": attr_id,
- "db_connection_name": db_connection_name,
- "assigned_attr_model": assigned_attr_model,
- "assigned_id_field_name": assigned_id_field_name,
- "identifier_field_name": "slug",
- }
-
- if contains_all:
- return _filter_contains_all_condition(
- contains_all=contains_all,
- referenced_model=product_models.Product,
- attr_value_reference_field_name="reference_product_id",
- **shared_filter_params,
- )
-
- if contains_any:
- return _filter_contains_any_condition(
- contains_any=contains_any,
- referenced_model=product_models.Product,
- attr_value_reference_field_name="reference_product_id",
- **shared_filter_params,
- )
- return Q()
-
-
-def filter_by_contains_referenced_variants(
- attr_id: int | None,
- attr_value: CONTAINS_TYPING,
- db_connection_name: str,
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
-):
- """Build a filter expression for referenced product variants.
-
- Returns a Q expression to filter objects based on their references
- to product variants.
-
- - If `contains_all` is provided, only objects that reference all of the
- specified variants will match.
- - If `contains_any` is provided, objects that reference at least one of
- the specified variants will match.
- """
-
- contains_all = attr_value.get("contains_all")
- contains_any = attr_value.get("contains_any")
-
- shared_filter_params: SharedContainsFilterParams = {
- "attr_id": attr_id,
- "db_connection_name": db_connection_name,
- "assigned_attr_model": assigned_attr_model,
- "assigned_id_field_name": assigned_id_field_name,
- "identifier_field_name": "sku",
- }
-
- if contains_all:
- return _filter_contains_all_condition(
- contains_all=contains_all,
- referenced_model=product_models.ProductVariant,
- attr_value_reference_field_name="reference_variant_id",
- **shared_filter_params,
- )
-
- if contains_any:
- return _filter_contains_any_condition(
- contains_any=contains_any,
- referenced_model=product_models.ProductVariant,
- attr_value_reference_field_name="reference_variant_id",
- **shared_filter_params,
- )
- return Q()
-
-
-def filter_by_slug_or_name(
- attr_id: int | None,
- attr_value: dict,
- db_connection_name: str,
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
-):
- attribute_values = AttributeValue.objects.using(db_connection_name).filter(
- **{"attribute_id": attr_id} if attr_id else {}
- )
- if "slug" in attr_value:
- attribute_values = filter_where_by_value_field(
- attribute_values, "slug", attr_value["slug"]
- )
- if "name" in attr_value:
- attribute_values = filter_where_by_value_field(
- attribute_values, "name", attr_value["name"]
- )
- assigned_attr_value = assigned_attr_model.objects.using(db_connection_name).filter(
- Exists(attribute_values.filter(id=OuterRef("value_id"))),
- **{str(assigned_id_field_name): OuterRef("id")},
- )
- return Q(Exists(assigned_attr_value))
-
-
-def filter_by_numeric_attribute(
- attr_id: int | None,
- numeric_value,
- db_connection_name: str,
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
-):
- qs_by_numeric = AttributeValue.objects.using(db_connection_name).filter(
- attribute__input_type=AttributeInputType.NUMERIC,
- **{"attribute_id": attr_id} if attr_id else {},
- )
- qs_by_numeric = filter_where_by_numeric_field(
- qs_by_numeric,
- "numeric",
- numeric_value,
- )
-
- assigned_attr_value = assigned_attr_model.objects.using(db_connection_name).filter(
- value__in=qs_by_numeric,
- **{str(assigned_id_field_name): OuterRef("id")},
- )
- return Q(Exists(assigned_attr_value))
-
-
-def filter_by_boolean_attribute(
- attr_id: int | None,
- boolean_value,
- db_connection_name: str,
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
-):
- qs_by_boolean = AttributeValue.objects.using(db_connection_name).filter(
- attribute__input_type=AttributeInputType.BOOLEAN,
- **{"attribute_id": attr_id} if attr_id else {},
- )
- qs_by_boolean = qs_by_boolean.filter(boolean=boolean_value)
- assigned_attr_value = assigned_attr_model.objects.using(db_connection_name).filter(
- value__in=qs_by_boolean,
- **{str(assigned_id_field_name): OuterRef("id")},
- )
- return Q(Exists(assigned_attr_value))
-
-
-def filter_by_date_attribute(
- attr_id: int | None,
- date_value,
- db_connection_name: str,
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
-):
- qs_by_date = AttributeValue.objects.using(db_connection_name).filter(
- attribute__input_type=AttributeInputType.DATE,
- **{"attribute_id": attr_id} if attr_id else {},
- )
- qs_by_date = filter_range_field(
- qs_by_date,
- "date_time__date",
- date_value,
- )
- assigned_attr_value = assigned_attr_model.objects.using(db_connection_name).filter(
- value__in=qs_by_date,
- **{str(assigned_id_field_name): OuterRef("id")},
- )
- return Q(Exists(assigned_attr_value))
-
-
-def filter_by_date_time_attribute(
- attr_id: int | None,
- date_time_value,
- db_connection_name: str,
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
-):
- qs_by_date_time = AttributeValue.objects.using(db_connection_name).filter(
- attribute__input_type=AttributeInputType.DATE_TIME,
- **{"attribute_id": attr_id} if attr_id else {},
- )
- qs_by_date_time = filter_range_field(
- qs_by_date_time,
- "date_time",
- date_time_value,
- )
- assigned_attr_value = assigned_attr_model.objects.using(db_connection_name).filter(
- value__in=qs_by_date_time,
- **{str(assigned_id_field_name): OuterRef("id")},
- )
- return Exists(assigned_attr_value)
-
-
def get_attribute_values_by_slug_or_name_value(
attr_id: int | None,
attr_value: dict,
@@ -741,159 +265,6 @@ def get_attribute_values_by_referenced_variant_ids(
)
-def filter_objects_by_attributes[T: (page_models.Page, product_models.Product)](
- qs: QuerySet[T],
- value: list[dict],
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
-) -> QuerySet[T]:
- attribute_slugs = {
- attr_filter["slug"] for attr_filter in value if "slug" in attr_filter
- }
- attributes_map = {
- attr.slug: attr
- for attr in Attribute.objects.using(qs.db).filter(slug__in=attribute_slugs)
- }
- if len(attribute_slugs) != len(attributes_map.keys()):
- # Filter over non existing attribute
- return qs.none()
-
- attr_filter_expression = Q()
-
- attr_without_values_input = []
- for attr_filter in value:
- if "slug" in attr_filter and "value" not in attr_filter:
- attr_without_values_input.append(attributes_map[attr_filter["slug"]])
-
- if attr_without_values_input:
- atr_value_qs = AttributeValue.objects.using(qs.db).filter(
- attribute_id__in=[attr.id for attr in attr_without_values_input]
- )
- assigned_attr_value = assigned_attr_model.objects.using(qs.db).filter(
- Exists(atr_value_qs.filter(id=OuterRef("value_id"))),
- **{str(assigned_id_field_name): OuterRef("id")},
- )
- attr_filter_expression = Q(Exists(assigned_attr_value))
-
- for attr_filter in value:
- attr_value = attr_filter.get("value")
- if not attr_value:
- # attrs without value input are handled separately
- continue
-
- attr_id = None
- if attr_slug := attr_filter.get("slug"):
- attr = attributes_map[attr_slug]
- attr_id = attr.id
-
- attr_value = attr_filter["value"]
-
- if "slug" in attr_value or "name" in attr_value:
- attr_filter_expression &= filter_by_slug_or_name(
- attr_id,
- attr_value,
- qs.db,
- assigned_attr_model=assigned_attr_model,
- assigned_id_field_name=assigned_id_field_name,
- )
- elif "numeric" in attr_value:
- attr_filter_expression &= filter_by_numeric_attribute(
- attr_id,
- attr_value["numeric"],
- qs.db,
- assigned_attr_model=assigned_attr_model,
- assigned_id_field_name=assigned_id_field_name,
- )
- elif "boolean" in attr_value:
- attr_filter_expression &= filter_by_boolean_attribute(
- attr_id,
- attr_value["boolean"],
- qs.db,
- assigned_attr_model=assigned_attr_model,
- assigned_id_field_name=assigned_id_field_name,
- )
- elif "date" in attr_value:
- attr_filter_expression &= filter_by_date_attribute(
- attr_id,
- attr_value["date"],
- qs.db,
- assigned_attr_model=assigned_attr_model,
- assigned_id_field_name=assigned_id_field_name,
- )
- elif "date_time" in attr_value:
- attr_filter_expression &= filter_by_date_time_attribute(
- attr_id,
- attr_value["date_time"],
- qs.db,
- assigned_attr_model=assigned_attr_model,
- assigned_id_field_name=assigned_id_field_name,
- )
- elif "reference" in attr_value:
- attr_filter_expression &= filter_objects_by_reference_attributes(
- attr_id,
- attr_value["reference"],
- qs.db,
- assigned_attr_model=assigned_attr_model,
- assigned_id_field_name=assigned_id_field_name,
- )
- if attr_filter_expression != Q():
- return qs.filter(attr_filter_expression)
- return qs.none()
-
-
-def filter_objects_by_reference_attributes(
- attr_id: int | None,
- attr_value: dict[
- Literal[
- "referenced_ids", "page_slugs", "product_slugs", "product_variant_skus"
- ],
- CONTAINS_TYPING,
- ],
- db_connection_name: str,
- assigned_attr_model: type[
- AssignedPageAttributeValue | AssignedProductAttributeValue
- ],
- assigned_id_field_name: Literal["page_id", "product_id"],
-):
- filter_expression = Q()
-
- if "referenced_ids" in attr_value:
- filter_expression &= filter_by_contains_referenced_object_ids(
- attr_id,
- attr_value["referenced_ids"],
- db_connection_name,
- assigned_attr_model=assigned_attr_model,
- assigned_id_field_name=assigned_id_field_name,
- )
- if "page_slugs" in attr_value:
- filter_expression &= filter_by_contains_referenced_pages(
- attr_id,
- attr_value["page_slugs"],
- db_connection_name,
- assigned_attr_model=assigned_attr_model,
- assigned_id_field_name=assigned_id_field_name,
- )
- if "product_slugs" in attr_value:
- filter_expression &= filter_by_contains_referenced_products(
- attr_id,
- attr_value["product_slugs"],
- db_connection_name,
- assigned_attr_model=assigned_attr_model,
- assigned_id_field_name=assigned_id_field_name,
- )
- if "product_variant_skus" in attr_value:
- filter_expression &= filter_by_contains_referenced_variants(
- attr_id,
- attr_value["product_variant_skus"],
- db_connection_name,
- assigned_attr_model=assigned_attr_model,
- assigned_id_field_name=assigned_id_field_name,
- )
- return filter_expression
-
-
def validate_attribute_value_reference_input(
index_with_values: list[
tuple[
diff --git a/saleor/graphql/page/filters.py b/saleor/graphql/page/filters.py
index bf6e4288bb..0488bdbcfb 100644
--- a/saleor/graphql/page/filters.py
+++ b/saleor/graphql/page/filters.py
@@ -1,12 +1,25 @@
+from typing import Literal
+
import django_filters
import graphene
-from django.db.models import Q
+from django.db.models import Exists, OuterRef, Q, QuerySet
-from ...attribute.models import AssignedPageAttributeValue
+from ...attribute.models import AssignedPageAttributeValue, Attribute, AttributeValue
from ...page import models
from ..attribute.shared_filters import (
+ CONTAINS_TYPING,
AssignedAttributeWhereInput,
- filter_objects_by_attributes,
+ get_attribute_values_by_boolean_value,
+ get_attribute_values_by_date_time_value,
+ get_attribute_values_by_date_value,
+ get_attribute_values_by_numeric_value,
+ get_attribute_values_by_referenced_page_ids,
+ get_attribute_values_by_referenced_page_slugs,
+ get_attribute_values_by_referenced_product_ids,
+ get_attribute_values_by_referenced_product_slugs,
+ get_attribute_values_by_referenced_variant_ids,
+ get_attribute_values_by_referenced_variant_skus,
+ get_attribute_values_by_slug_or_name_value,
validate_attribute_value_input,
)
from ..core.context import ChannelQsContext
@@ -30,6 +43,7 @@ from ..core.filters.where_input import (
)
from ..utils import resolve_global_ids_to_primary_keys
from ..utils.filters import (
+ Number,
filter_by_id,
filter_by_ids,
filter_slug_list,
@@ -63,15 +77,476 @@ def filter_page_type_search(qs, _, value):
return qs.filter(Q(name__trigram_similar=value) | Q(slug__trigram_similar=value))
-def filter_pages_by_attributes(qs, value):
- return filter_objects_by_attributes(
- qs,
- value,
- AssignedPageAttributeValue,
- "page_id",
+def _get_assigned_page_attribute_for_attribute_value(
+ attribute_values: QuerySet[AttributeValue],
+ db_connection_name: str,
+):
+ return Q(
+ Exists(
+ AssignedPageAttributeValue.objects.using(db_connection_name).filter(
+ Exists(attribute_values.filter(id=OuterRef("value_id"))),
+ page_id=OuterRef("id"),
+ )
+ )
+ )
+
+
+def filter_by_slug_or_name(
+ attr_id: int | None,
+ attr_value: dict,
+ db_connection_name: str,
+):
+ attribute_values = get_attribute_values_by_slug_or_name_value(
+ attr_id=attr_id,
+ attr_value=attr_value,
+ db_connection_name=db_connection_name,
+ )
+ return _get_assigned_page_attribute_for_attribute_value(
+ attribute_values=attribute_values,
+ db_connection_name=db_connection_name,
+ )
+
+
+def filter_by_numeric_attribute(
+ attr_id: int | None,
+ numeric_value: dict[str, Number | list[Number] | dict[str, Number]],
+ db_connection_name: str,
+):
+ attribute_values = get_attribute_values_by_numeric_value(
+ attr_id=attr_id,
+ numeric_value=numeric_value,
+ db_connection_name=db_connection_name,
+ )
+ return _get_assigned_page_attribute_for_attribute_value(
+ attribute_values=attribute_values,
+ db_connection_name=db_connection_name,
+ )
+
+
+def filter_by_boolean_attribute(
+ attr_id: int | None,
+ boolean_value: bool,
+ db_connection_name: str,
+):
+ attribute_values = get_attribute_values_by_boolean_value(
+ attr_id=attr_id,
+ boolean_value=boolean_value,
+ db_connection_name=db_connection_name,
+ )
+ return _get_assigned_page_attribute_for_attribute_value(
+ attribute_values=attribute_values,
+ db_connection_name=db_connection_name,
)
+def filter_by_date_attribute(
+ attr_id: int | None,
+ date_value: dict[str, str],
+ db_connection_name: str,
+):
+ attribute_values = get_attribute_values_by_date_value(
+ attr_id=attr_id,
+ date_value=date_value,
+ db_connection_name=db_connection_name,
+ )
+ return _get_assigned_page_attribute_for_attribute_value(
+ attribute_values=attribute_values,
+ db_connection_name=db_connection_name,
+ )
+
+
+def filter_by_date_time_attribute(
+ attr_id: int | None,
+ date_value: dict[str, str],
+ db_connection_name: str,
+):
+ attribute_values = get_attribute_values_by_date_time_value(
+ attr_id=attr_id,
+ date_value=date_value,
+ db_connection_name=db_connection_name,
+ )
+ return _get_assigned_page_attribute_for_attribute_value(
+ attribute_values=attribute_values,
+ db_connection_name=db_connection_name,
+ )
+
+
+def _filter_contains_single_expression(
+ attr_id: int | None,
+ db_connection_name: str,
+ referenced_attr_values: QuerySet[AttributeValue],
+):
+ if attr_id:
+ referenced_attr_values = referenced_attr_values.filter(
+ attribute_id=attr_id,
+ )
+ return _get_assigned_page_attribute_for_attribute_value(
+ referenced_attr_values,
+ db_connection_name,
+ )
+
+
+def filter_by_contains_referenced_page_slugs(
+ attr_id: int | None,
+ attr_value: CONTAINS_TYPING,
+ db_connection_name: str,
+):
+ """Build an expression to filter pages based on their references to pages.
+
+ - If `contains_all` is provided, only pages that reference all of the
+ specified pages will match.
+ - If `contains_any` is provided, pages that reference at least one of
+ the specified pages will match.
+ """
+ contains_all = attr_value.get("contains_all")
+ contains_any = attr_value.get("contains_any")
+
+ if contains_all:
+ expression = Q()
+ for page_slug in contains_all:
+ referenced_attr_values = get_attribute_values_by_referenced_page_slugs(
+ slugs=[page_slug], db_connection_name=db_connection_name
+ )
+ expression &= _filter_contains_single_expression(
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ referenced_attr_values=referenced_attr_values,
+ )
+ return expression
+
+ if contains_any:
+ referenced_attr_values = get_attribute_values_by_referenced_page_slugs(
+ slugs=contains_any, db_connection_name=db_connection_name
+ )
+ return _filter_contains_single_expression(
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ referenced_attr_values=referenced_attr_values,
+ )
+ return Q()
+
+
+def filter_by_contains_referenced_product_slugs(
+ attr_id: int | None,
+ attr_value: CONTAINS_TYPING,
+ db_connection_name: str,
+):
+ """Build an expression to filter pages based on their references to products.
+
+ - If `contains_all` is provided, only pages that reference all of the
+ specified products will match.
+ - If `contains_any` is provided, pages that reference at least one of
+ the specified products will match.
+ """
+ contains_all = attr_value.get("contains_all")
+ contains_any = attr_value.get("contains_any")
+
+ if contains_all:
+ expression = Q()
+ for product_slug in contains_all:
+ referenced_attr_values = get_attribute_values_by_referenced_product_slugs(
+ slugs=[product_slug], db_connection_name=db_connection_name
+ )
+ expression &= _filter_contains_single_expression(
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ referenced_attr_values=referenced_attr_values,
+ )
+ return expression
+
+ if contains_any:
+ referenced_attr_values = get_attribute_values_by_referenced_product_slugs(
+ slugs=contains_any, db_connection_name=db_connection_name
+ )
+ return _filter_contains_single_expression(
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ referenced_attr_values=referenced_attr_values,
+ )
+ return Q()
+
+
+def filter_by_contains_referenced_variant_skus(
+ attr_id: int | None,
+ attr_value: CONTAINS_TYPING,
+ db_connection_name: str,
+):
+ """Build an expression to filter pages based on their references to variants.
+
+ - If `contains_all` is provided, only pages that reference all of the
+ specified variants will match.
+ - If `contains_any` is provided, pages that reference at least one of
+ the specified variants will match.
+ """
+ contains_all = attr_value.get("contains_all")
+ contains_any = attr_value.get("contains_any")
+
+ if contains_all:
+ expression = Q()
+ for variant_sku in contains_all:
+ referenced_attr_values = get_attribute_values_by_referenced_variant_skus(
+ slugs=[variant_sku], db_connection_name=db_connection_name
+ )
+ expression &= _filter_contains_single_expression(
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ referenced_attr_values=referenced_attr_values,
+ )
+ return expression
+
+ if contains_any:
+ referenced_attr_values = get_attribute_values_by_referenced_variant_skus(
+ slugs=contains_any, db_connection_name=db_connection_name
+ )
+ return _filter_contains_single_expression(
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ referenced_attr_values=referenced_attr_values,
+ )
+ return Q()
+
+
+def _filter_by_contains_all_referenced_object_ids(
+ variant_ids: set[int],
+ product_ids: set[int],
+ page_ids: set[int],
+ attr_id: int | None,
+ db_connection_name: str,
+) -> Q:
+ expression = Q()
+ if page_ids:
+ for page_id in page_ids:
+ referenced_attr_values = get_attribute_values_by_referenced_page_ids(
+ ids=[page_id], db_connection_name=db_connection_name
+ )
+ expression &= _filter_contains_single_expression(
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ referenced_attr_values=referenced_attr_values,
+ )
+ if product_ids:
+ for product_id in product_ids:
+ referenced_attr_values = get_attribute_values_by_referenced_product_ids(
+ ids=[product_id], db_connection_name=db_connection_name
+ )
+ expression &= _filter_contains_single_expression(
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ referenced_attr_values=referenced_attr_values,
+ )
+ if variant_ids:
+ for variant_id in variant_ids:
+ referenced_attr_values = get_attribute_values_by_referenced_variant_ids(
+ ids=[variant_id], db_connection_name=db_connection_name
+ )
+ expression &= _filter_contains_single_expression(
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ referenced_attr_values=referenced_attr_values,
+ )
+ return expression
+
+
+def _filter_by_contains_any_referenced_object_ids(
+ variant_ids: set[int],
+ product_ids: set[int],
+ page_ids: set[int],
+ attr_id: int | None,
+ db_connection_name: str,
+) -> Q:
+ expression = Q()
+ if page_ids:
+ referenced_attr_values = get_attribute_values_by_referenced_page_ids(
+ ids=list(page_ids), db_connection_name=db_connection_name
+ )
+ expression |= _filter_contains_single_expression(
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ referenced_attr_values=referenced_attr_values,
+ )
+ if product_ids:
+ referenced_attr_values = get_attribute_values_by_referenced_product_ids(
+ ids=list(product_ids), db_connection_name=db_connection_name
+ )
+ expression |= _filter_contains_single_expression(
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ referenced_attr_values=referenced_attr_values,
+ )
+ if variant_ids:
+ referenced_attr_values = get_attribute_values_by_referenced_variant_ids(
+ ids=list(variant_ids), db_connection_name=db_connection_name
+ )
+ expression |= _filter_contains_single_expression(
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ referenced_attr_values=referenced_attr_values,
+ )
+ return expression
+
+
+def filter_by_contains_referenced_object_ids(
+ attr_id: int | None,
+ attr_value: CONTAINS_TYPING,
+ db_connection_name: str,
+) -> Q:
+ contains_all = attr_value.get("contains_all")
+ contains_any = attr_value.get("contains_any")
+
+ variant_ids = set()
+ product_ids = set()
+ page_ids = set()
+
+ for obj_id in contains_any or contains_all or []:
+ type_, id_ = graphene.Node.from_global_id(obj_id)
+ if type_ == "Page":
+ page_ids.add(id_)
+ elif type_ == "Product":
+ product_ids.add(id_)
+ elif type_ == "ProductVariant":
+ variant_ids.add(id_)
+
+ if contains_all:
+ return _filter_by_contains_all_referenced_object_ids(
+ variant_ids=variant_ids,
+ product_ids=product_ids,
+ page_ids=page_ids,
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ )
+ if contains_any:
+ return _filter_by_contains_any_referenced_object_ids(
+ variant_ids=variant_ids,
+ product_ids=product_ids,
+ page_ids=page_ids,
+ attr_id=attr_id,
+ db_connection_name=db_connection_name,
+ )
+ return Q()
+
+
+def filter_objects_by_reference_attributes(
+ attr_id: int | None,
+ attr_value: dict[
+ Literal[
+ "referenced_ids", "page_slugs", "product_slugs", "product_variant_skus"
+ ],
+ CONTAINS_TYPING,
+ ],
+ db_connection_name: str,
+):
+ filter_expression = Q()
+
+ if "referenced_ids" in attr_value:
+ filter_expression &= filter_by_contains_referenced_object_ids(
+ attr_id,
+ attr_value["referenced_ids"],
+ db_connection_name,
+ )
+ if "page_slugs" in attr_value:
+ filter_expression &= filter_by_contains_referenced_page_slugs(
+ attr_id,
+ attr_value["page_slugs"],
+ db_connection_name,
+ )
+ if "product_slugs" in attr_value:
+ filter_expression &= filter_by_contains_referenced_product_slugs(
+ attr_id,
+ attr_value["product_slugs"],
+ db_connection_name,
+ )
+ if "product_variant_skus" in attr_value:
+ filter_expression &= filter_by_contains_referenced_variant_skus(
+ attr_id,
+ attr_value["product_variant_skus"],
+ db_connection_name,
+ )
+ return filter_expression
+
+
+def filter_pages_by_attributes(qs, value):
+ attribute_slugs = {
+ attr_filter["slug"] for attr_filter in value if "slug" in attr_filter
+ }
+ attributes_map = {
+ attr.slug: attr
+ for attr in Attribute.objects.using(qs.db).filter(slug__in=attribute_slugs)
+ }
+ if len(attribute_slugs) != len(attributes_map.keys()):
+ # Filter over non existing attribute
+ return qs.none()
+
+ attr_filter_expression = Q()
+
+ attr_without_values_input = []
+ for attr_filter in value:
+ if "slug" in attr_filter and "value" not in attr_filter:
+ attr_without_values_input.append(attributes_map[attr_filter["slug"]])
+
+ if attr_without_values_input:
+ atr_value_qs = AttributeValue.objects.using(qs.db).filter(
+ attribute_id__in=[attr.id for attr in attr_without_values_input]
+ )
+ assigned_attr_value = AssignedPageAttributeValue.objects.using(qs.db).filter(
+ Exists(atr_value_qs.filter(id=OuterRef("value_id"))),
+ page_id=OuterRef("id"),
+ )
+ attr_filter_expression = Q(Exists(assigned_attr_value))
+
+ for attr_filter in value:
+ attr_value = attr_filter.get("value")
+ if not attr_value:
+ # attrs without value input are handled separately
+ continue
+
+ attr_id = None
+ if attr_slug := attr_filter.get("slug"):
+ attr = attributes_map[attr_slug]
+ attr_id = attr.id
+
+ attr_value = attr_filter["value"]
+
+ if "slug" in attr_value or "name" in attr_value:
+ attr_filter_expression &= filter_by_slug_or_name(
+ attr_id,
+ attr_value,
+ qs.db,
+ )
+ elif "numeric" in attr_value:
+ attr_filter_expression &= filter_by_numeric_attribute(
+ attr_id,
+ attr_value["numeric"],
+ qs.db,
+ )
+ elif "boolean" in attr_value:
+ attr_filter_expression &= filter_by_boolean_attribute(
+ attr_id,
+ attr_value["boolean"],
+ qs.db,
+ )
+ elif "date" in attr_value:
+ attr_filter_expression &= filter_by_date_attribute(
+ attr_id,
+ attr_value["date"],
+ qs.db,
+ )
+ elif "date_time" in attr_value:
+ attr_filter_expression &= filter_by_date_time_attribute(
+ attr_id,
+ attr_value["date_time"],
+ qs.db,
+ )
+ elif "reference" in attr_value:
+ attr_filter_expression &= filter_objects_by_reference_attributes(
+ attr_id,
+ attr_value["reference"],
+ qs.db,
+ )
+ if attr_filter_expression != Q():
+ return qs.filter(attr_filter_expression)
+ return qs.none()
+
+
class PageWhere(MetadataWhereBase):
ids = GlobalIDMultipleChoiceWhereFilter(method=filter_by_ids("Page"))
slug = OperationObjectTypeWhereFilter(