Skip to content

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

Add more usages of replica (#15540)

Marcin Gębala3ab99b8dc923
diff --git a/saleor/graphql/account/schema.py b/saleor/graphql/account/schema.py
index df57f725e9..e9f05a5b10 100644
--- a/saleor/graphql/account/schema.py
+++ b/saleor/graphql/account/schema.py
@@ -213,13 +213,17 @@ class AccountQueries(graphene.ObjectType):
@staticmethod
def resolve_customers(_root, info: ResolveInfo, **kwargs):
qs = resolve_customers(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, UserCountableConnection)
@staticmethod
def resolve_permission_groups(_root, info: ResolveInfo, **kwargs):
qs = resolve_permission_groups(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, GroupCountableConnection)
@staticmethod
@@ -235,7 +239,9 @@ class AccountQueries(graphene.ObjectType):
@staticmethod
def resolve_staff_users(_root, info: ResolveInfo, **kwargs):
qs = resolve_staff_users(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, UserCountableConnection)
@staticmethod
diff --git a/saleor/graphql/app/schema.py b/saleor/graphql/app/schema.py
index 816792b6bc..845ed81c52 100644
--- a/saleor/graphql/app/schema.py
+++ b/saleor/graphql/app/schema.py
@@ -125,7 +125,9 @@ class AppQueries(graphene.ObjectType):
@staticmethod
def resolve_apps(_root, info: ResolveInfo, **kwargs):
qs = resolve_apps(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, AppCountableConnection)
@staticmethod
@@ -144,7 +146,9 @@ class AppQueries(graphene.ObjectType):
@staticmethod
def resolve_app_extensions(_root, info: ResolveInfo, **kwargs):
qs = resolve_app_extensions(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(
qs, info, kwargs, AppExtensionCountableConnection
)
diff --git a/saleor/graphql/attribute/schema.py b/saleor/graphql/attribute/schema.py
index 3c7a3ff050..2682f036c5 100644
--- a/saleor/graphql/attribute/schema.py
+++ b/saleor/graphql/attribute/schema.py
@@ -61,7 +61,9 @@ class AttributeQueries(graphene.ObjectType):
def resolve_attributes(self, info: ResolveInfo, *, search=None, **kwargs):
qs = resolve_attributes(info)
- qs = filter_connection_queryset(qs, kwargs, info.context)
+ qs = filter_connection_queryset(
+ qs, kwargs, info.context, allow_replica=info.context.allow_replica
+ )
if search:
qs = filter_attribute_search(qs, None, search)
return create_connection_slice(qs, info, kwargs, AttributeCountableConnection)
diff --git a/saleor/graphql/attribute/types.py b/saleor/graphql/attribute/types.py
index 1e154ce805..e7cbdc06d1 100644
--- a/saleor/graphql/attribute/types.py
+++ b/saleor/graphql/attribute/types.py
@@ -282,7 +282,9 @@ class Attribute(ModelObjectType[models.Attribute]):
QuerySet[models.AttributeValue], models.AttributeValue.objects.none()
)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(
qs, info, kwargs, AttributeValueCountableConnection
)
diff --git a/saleor/graphql/checkout/schema.py b/saleor/graphql/checkout/schema.py
index 57ceae5546..c612927dc2 100644
--- a/saleor/graphql/checkout/schema.py
+++ b/saleor/graphql/checkout/schema.py
@@ -95,7 +95,9 @@ class CheckoutQueries(graphene.ObjectType):
@staticmethod
def resolve_checkouts(_root, info: ResolveInfo, *, channel=None, **kwargs):
qs = resolve_checkouts(info, channel)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, CheckoutCountableConnection)
@staticmethod
diff --git a/saleor/graphql/core/connection.py b/saleor/graphql/core/connection.py
index 0bdf4e2ff0..904f29b120 100644
--- a/saleor/graphql/core/connection.py
+++ b/saleor/graphql/core/connection.py
@@ -444,7 +444,9 @@ def slice_connection_iterable(
return slice
-def filter_connection_queryset(iterable, args, request=None, root=None):
+def filter_connection_queryset(
+ iterable, args, request=None, root=None, allow_replica=False
+):
update_args_with_channel(args, root)
if args.get(args[FILTERS_NAME]) and args.get(args[WHERE_NAME]):
raise GraphQLError(
@@ -460,7 +462,9 @@ def filter_connection_queryset(iterable, args, request=None, root=None):
filter_func = where_filter_qs
if filter_input:
- return filter_func(iterable, args, filterset_class, filter_input, request)
+ return filter_func(
+ iterable, args, filterset_class, filter_input, request, allow_replica
+ )
return iterable
@@ -471,7 +475,9 @@ def update_args_with_channel(args, root):
args["channel"] = root.channel_slug
-def filter_qs(iterable, args, filterset_class, filter_input, request):
+def filter_qs(
+ iterable, args, filterset_class, filter_input, request, allow_replica=False
+):
try:
filter_channel = str(filter_input["channel"])
except (NoDefaultChannel, ChannelNotDefined, GraphQLError, KeyError):
@@ -497,7 +503,9 @@ def filter_qs(iterable, args, filterset_class, filter_input, request):
return filterset.qs
-def where_filter_qs(iterable, args, filterset_class, filter_input, request):
+def where_filter_qs(
+ iterable, args, filterset_class, filter_input, request, allow_replica=False
+):
"""Filter queryset by complex statement provided in where argument.
Handle `AND`, `OR`, `NOT` operators, as well as flat filter input.
@@ -544,12 +552,12 @@ def where_filter_qs(iterable, args, filterset_class, filter_input, request):
if and_filter_input:
queryset = _handle_and_filter_input(
- and_filter_input, queryset, args, filterset_class, request
+ and_filter_input, queryset, args, filterset_class, request, allow_replica
)
if or_filter_input:
queryset = _handle_or_filter_input(
- or_filter_input, queryset, args, filterset_class, request
+ or_filter_input, queryset, args, filterset_class, request, allow_replica
)
# TODO: needs optimization
@@ -560,7 +568,7 @@ def where_filter_qs(iterable, args, filterset_class, filter_input, request):
if filter_input:
qs_to_combine = filter_qs(
- iterable, args, filterset_class, filter_input, request
+ iterable, args, filterset_class, filter_input, request, allow_replica
)
if isinstance(qs_to_combine, ChannelQsContext):
queryset &= qs_to_combine.qs
@@ -578,18 +586,26 @@ def contains_filter_operator(input: dict[str, Union[dict, str]]):
return any([operator in input for operator in ["AND", "OR", "NOT"]])
-def _handle_and_filter_input(filter_input, queryset, args, filterset_class, request):
+def _handle_and_filter_input(
+ filter_input, queryset, args, filterset_class, request, allow_replica
+):
for input in filter_input:
if contains_filter_operator(input):
# when the input contains the operator run the where_filter_qs method again
# to properly handle the nested input
- queryset &= where_filter_qs(queryset, args, filterset_class, input, request)
+ queryset &= where_filter_qs(
+ queryset, args, filterset_class, input, request, allow_replica
+ )
else:
- queryset &= filter_qs(queryset, args, filterset_class, input, request)
+ queryset &= filter_qs(
+ queryset, args, filterset_class, input, request, allow_replica
+ )
return queryset
-def _handle_or_filter_input(filter_input, queryset, args, filterset_class, request):
+def _handle_or_filter_input(
+ filter_input, queryset, args, filterset_class, request, allow_replica
+):
# for the OR operator the instanced that passed one of specified condition are
# found, then the return queryset is joined with the use of AND operator with
# main qs
@@ -598,9 +614,13 @@ def _handle_or_filter_input(filter_input, queryset, args, filterset_class, reque
if contains_filter_operator(input):
# when the input contains the operator run the where_filter_qs method again
# to properly handle the nested input
- qs |= where_filter_qs(queryset, args, filterset_class, input, request)
+ qs |= where_filter_qs(
+ queryset, args, filterset_class, input, request, allow_replica
+ )
else:
- qs |= filter_qs(queryset, args, filterset_class, input, request)
+ qs |= filter_qs(
+ queryset, args, filterset_class, input, request, allow_replica
+ )
queryset &= qs
return queryset
diff --git a/saleor/graphql/csv/schema.py b/saleor/graphql/csv/schema.py
index 409212de9d..bfe6f33bd1 100644
--- a/saleor/graphql/csv/schema.py
+++ b/saleor/graphql/csv/schema.py
@@ -35,7 +35,9 @@ class CsvQueries(graphene.ObjectType):
def resolve_export_files(self, info: ResolveInfo, **kwargs):
qs = resolve_export_files(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, ExportFileCountableConnection)
diff --git a/saleor/graphql/discount/schema.py b/saleor/graphql/discount/schema.py
index b6c1218f0f..ee3ae1cd1f 100644
--- a/saleor/graphql/discount/schema.py
+++ b/saleor/graphql/discount/schema.py
@@ -174,7 +174,9 @@ class DiscountQueries(graphene.ObjectType):
def resolve_sales(_root, info: ResolveInfo, *, channel=None, **kwargs):
qs = resolve_sales(info, channel_slug=channel, **kwargs)
kwargs["channel"] = channel
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, SaleCountableConnection)
@staticmethod
@@ -186,7 +188,9 @@ class DiscountQueries(graphene.ObjectType):
def resolve_vouchers(_root, info: ResolveInfo, *, channel=None, **kwargs):
qs = resolve_vouchers(info, channel_slug=channel, **kwargs)
kwargs["channel"] = channel
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, VoucherCountableConnection)
@staticmethod
@@ -197,7 +201,9 @@ class DiscountQueries(graphene.ObjectType):
@staticmethod
def resolve_promotions(_root, info: ResolveInfo, **kwargs):
qs = resolve_promotions(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, PromotionCountableConnection)
diff --git a/saleor/graphql/giftcard/schema.py b/saleor/graphql/giftcard/schema.py
index 3dc7823a68..afe38ad3a9 100644
--- a/saleor/graphql/giftcard/schema.py
+++ b/saleor/graphql/giftcard/schema.py
@@ -103,7 +103,9 @@ class GiftCardQueries(graphene.ObjectType):
if search:
qs = search_gift_cards(qs, search)
qs = filter_connection_queryset(
- qs, {"sort_by": sort_by, "filter": filter, **kwargs}
+ qs,
+ {"sort_by": sort_by, "filter": filter, **kwargs},
+ allow_replica=info.context.allow_replica,
)
return create_connection_slice(
qs,
@@ -123,7 +125,9 @@ class GiftCardQueries(graphene.ObjectType):
@staticmethod
def resolve_gift_card_tags(_root, info: ResolveInfo, **data):
qs = resolve_gift_card_tags(info)
- qs = filter_connection_queryset(qs, data)
+ qs = filter_connection_queryset(
+ qs, data, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, data, GiftCardTagCountableConnection)
diff --git a/saleor/graphql/menu/schema.py b/saleor/graphql/menu/schema.py
index 5a23ea1520..e2e6ca3d18 100644
--- a/saleor/graphql/menu/schema.py
+++ b/saleor/graphql/menu/schema.py
@@ -88,7 +88,9 @@ class MenuQueries(graphene.ObjectType):
if channel is None:
channel = get_default_channel_slug_or_graphql_error()
qs = resolve_menus(info, channel)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, MenuCountableConnection)
@staticmethod
@@ -104,7 +106,9 @@ class MenuQueries(graphene.ObjectType):
channel = get_default_channel_slug_or_graphql_error()
menu_items = resolve_menu_items(info)
qs = ChannelQsContext(qs=menu_items, channel_slug=channel)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, MenuItemCountableConnection)
diff --git a/saleor/graphql/order/schema.py b/saleor/graphql/order/schema.py
index 0f5c52ce88..5509289b8f 100644
--- a/saleor/graphql/order/schema.py
+++ b/saleor/graphql/order/schema.py
@@ -188,7 +188,9 @@ class OrderQueries(graphene.ObjectType):
{"direction": "-", "field": ["search_rank", "id"]}
)
qs = resolve_orders(info, channel)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, OrderCountableConnection)
@staticmethod
@@ -207,7 +209,9 @@ class OrderQueries(graphene.ObjectType):
{"direction": "-", "field": ["search_rank", "id"]}
)
qs = resolve_draft_orders(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, OrderCountableConnection)
@staticmethod
diff --git a/saleor/graphql/page/resolvers.py b/saleor/graphql/page/resolvers.py
index a6015370bd..0f01f3a891 100644
--- a/saleor/graphql/page/resolvers.py
+++ b/saleor/graphql/page/resolvers.py
@@ -12,16 +12,16 @@ def resolve_page(info, global_page_id=None, slug=None):
if slug is not None:
page = (
- models.Page.objects.visible_to_user(requestor)
- .using(get_database_connection_name(info.context))
+ models.Page.objects.using(get_database_connection_name(info.context))
+ .visible_to_user(requestor)
.filter(slug=slug)
.first()
)
else:
_type, page_pk = from_global_id_or_error(global_page_id, Page)
page = (
- models.Page.objects.visible_to_user(requestor)
- .using(get_database_connection_name(info.context))
+ models.Page.objects.using(get_database_connection_name(info.context))
+ .visible_to_user(requestor)
.filter(pk=page_pk)
.first()
)
@@ -30,9 +30,9 @@ def resolve_page(info, global_page_id=None, slug=None):
def resolve_pages(info):
requestor = get_user_or_app_from_context(info.context)
- return models.Page.objects.visible_to_user(requestor).using(
+ return models.Page.objects.using(
get_database_connection_name(info.context)
- )
+ ).visible_to_user(requestor)
def resolve_page_type(info, id):
diff --git a/saleor/graphql/page/schema.py b/saleor/graphql/page/schema.py
index 6bf8b6f593..18972bd720 100644
--- a/saleor/graphql/page/schema.py
+++ b/saleor/graphql/page/schema.py
@@ -68,7 +68,9 @@ class PageQueries(graphene.ObjectType):
@staticmethod
def resolve_pages(_root, info: ResolveInfo, **kwargs):
qs = resolve_pages(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, PageCountableConnection)
@staticmethod
@@ -79,7 +81,9 @@ class PageQueries(graphene.ObjectType):
@staticmethod
def resolve_page_types(_root, info: ResolveInfo, **kwargs):
qs = resolve_page_types(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, PageTypeCountableConnection)
diff --git a/saleor/graphql/page/types.py b/saleor/graphql/page/types.py
index 69830747a7..20101b8cfd 100644
--- a/saleor/graphql/page/types.py
+++ b/saleor/graphql/page/types.py
@@ -92,7 +92,9 @@ class PageType(ModelObjectType[models.PageType]):
qs = attribute_models.Attribute.objects.get_unassigned_page_type_attributes(
root.pk
).using(get_database_connection_name(info.context))
- qs = filter_connection_queryset(qs, kwargs, info.context)
+ qs = filter_connection_queryset(
+ qs, kwargs, info.context, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, AttributeCountableConnection)
@staticmethod
diff --git a/saleor/graphql/payment/schema.py b/saleor/graphql/payment/schema.py
index fe43ac8d69..36479f749a 100644
--- a/saleor/graphql/payment/schema.py
+++ b/saleor/graphql/payment/schema.py
@@ -86,7 +86,9 @@ class PaymentQueries(graphene.ObjectType):
@staticmethod
def resolve_payments(_root, info: ResolveInfo, **kwargs):
qs = resolve_payments(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, PaymentCountableConnection)
@staticmethod
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index dda871ab58..8e535e1351 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -91,7 +91,9 @@ from .enums import (
T_PRODUCT_FILTER_QUERIES = dict[int, list[int]]
-def _clean_product_attributes_filter_input(filter_value, queries):
+def _clean_product_attributes_filter_input(
+ filter_value, queries, database_connection_name
+):
attribute_slugs = []
value_slugs = []
for attr_slug, val_slugs in filter_value:
@@ -100,9 +102,11 @@ def _clean_product_attributes_filter_input(filter_value, queries):
attributes_slug_pk_map: dict[str, int] = {}
attributes_pk_slug_map: dict[int, str] = {}
values_map: dict[str, dict[str, int]] = defaultdict(dict)
- for attr_slug, attr_pk in Attribute.objects.filter(
- slug__in=attribute_slugs
- ).values_list("slug", "id"):
+ for attr_slug, attr_pk in (
+ Attribute.objects.using(database_connection_name)
+ .filter(slug__in=attribute_slugs)
+ .values_list("slug", "id")
+ ):
attributes_slug_pk_map[attr_slug] = attr_pk
attributes_pk_slug_map[attr_pk] = attr_slug
@@ -110,9 +114,11 @@ def _clean_product_attributes_filter_input(filter_value, queries):
attr_pk,
value_pk,
value_slug,
- ) in AttributeValue.objects.filter(
- slug__in=value_slugs, attribute_id__in=attributes_pk_slug_map.keys()
- ).values_list("attribute_id", "pk", "slug"):
+ ) in (
+ AttributeValue.objects.using(database_connection_name)
+ .filter(slug__in=value_slugs, attribute_id__in=attributes_pk_slug_map.keys())
+ .values_list("attribute_id", "pk", "slug")
+ ):
attr_slug = attributes_pk_slug_map[attr_pk]
values_map[attr_slug][value_slug] = value_pk
@@ -130,12 +136,15 @@ def _clean_product_attributes_filter_input(filter_value, queries):
queries[attr_pk] += attr_val_pk
-def _clean_product_attributes_range_filter_input(filter_value, queries):
- attributes = Attribute.objects.filter(input_type=AttributeInputType.NUMERIC)
+def _clean_product_attributes_range_filter_input(
+ filter_value, queries, database_connection_name
+):
+ attributes = Attribute.objects.using(database_connection_name).filter(
+ input_type=AttributeInputType.NUMERIC
+ )
values = (
- AttributeValue.objects.filter(
- Exists(attributes.filter(pk=OuterRef("attribute_id")))
- )
+ AttributeValue.objects.using(database_connection_name)
+ .filter(Exists(attributes.filter(pk=OuterRef("attribute_id"))))
.annotate(numeric_value=Cast("name", FloatField()))
.select_related("attribute")
)
@@ -165,9 +174,11 @@ def _clean_product_attributes_range_filter_input(filter_value, queries):
queries[attr_pk] += attr_values[value]
-def _clean_product_attributes_date_time_range_filter_input(filter_value):
+def _clean_product_attributes_date_time_range_filter_input(
+ filter_value, database_connection_name
+):
attribute_slugs = [slug for slug, _ in filter_value]
- matching_attributes = AttributeValue.objects.filter(
+ matching_attributes = AttributeValue.objects.using(database_connection_name).filter(
attribute__slug__in=attribute_slugs
)
filters = {}
@@ -192,11 +203,15 @@ class KeyValueDict(TypedDict):
values: dict[Optional[bool], int]
-def _clean_product_attributes_boolean_filter_input(filter_value, queries):
+def _clean_product_attributes_boolean_filter_input(
+ filter_value, queries, database_connection_name
+):
attribute_slugs = [slug for slug, _ in filter_value]
- attributes = Attribute.objects.filter(
- input_type=AttributeInputType.BOOLEAN, slug__in=attribute_slugs
- ).prefetch_related("values")
+ attributes = (
+ Attribute.objects.using(database_connection_name)
+ .filter(input_type=AttributeInputType.BOOLEAN, slug__in=attribute_slugs)
+ .prefetch_related("values")
+ )
values_map: dict[str, KeyValueDict] = {
attr.slug: {
"pk": attr.pk,
@@ -215,22 +230,24 @@ def _clean_product_attributes_boolean_filter_input(filter_value, queries):
def filter_products_by_attributes_values(qs, queries: T_PRODUCT_FILTER_QUERIES):
filters = []
for values in queries.values():
- assigned_product_attribute_values = (
- AssignedProductAttributeValue.objects.filter(value_id__in=values)
- )
+ assigned_product_attribute_values = AssignedProductAttributeValue.objects.using(
+ qs.db
+ ).filter(value_id__in=values)
product_attribute_filter = Q(
Exists(assigned_product_attribute_values.filter(product_id=OuterRef("pk")))
)
- assigned_variant_attribute_values = (
- AssignedVariantAttributeValue.objects.filter(value_id__in=values)
- )
- assigned_variant_attributes = AssignedVariantAttribute.objects.filter(
+ assigned_variant_attribute_values = AssignedVariantAttributeValue.objects.using(
+ qs.db
+ ).filter(value_id__in=values)
+ assigned_variant_attributes = AssignedVariantAttribute.objects.using(
+ qs.db
+ ).filter(
Exists(
assigned_variant_attribute_values.filter(assignment_id=OuterRef("pk"))
)
)
- product_variants = ProductVariant.objects.filter(
+ product_variants = ProductVariant.objects.using(qs.db).filter(
Exists(assigned_variant_attributes.filter(variant_id=OuterRef("pk")))
)
variant_attribute_filter = Q(
@@ -243,20 +260,20 @@ def filter_products_by_attributes_values(qs, queries: T_PRODUCT_FILTER_QUERIES):
def filter_products_by_attributes_values_qs(qs, values_qs):
- assigned_product_attribute_values = AssignedProductAttributeValue.objects.filter(
- value__in=values_qs
- )
+ assigned_product_attribute_values = AssignedProductAttributeValue.objects.using(
+ qs.db
+ ).filter(value__in=values_qs)
product_attribute_filter = Q(
Exists(assigned_product_attribute_values.filter(product_id=OuterRef("pk")))
)
- assigned_variant_attribute_values = AssignedVariantAttributeValue.objects.filter(
- value__in=values_qs
- )
- assigned_variant_attributes = AssignedVariantAttribute.objects.filter(
+ assigned_variant_attribute_values = AssignedVariantAttributeValue.objects.using(
+ qs.db
+ ).filter(value__in=values_qs)
+ assigned_variant_attributes = AssignedVariantAttribute.objects.using(qs.db).filter(
Exists(assigned_variant_attribute_values.filter(assignment_id=OuterRef("pk")))
)
- product_variants = ProductVariant.objects.filter(
+ product_variants = ProductVariant.objects.using(qs.db).filter(
Exists(assigned_variant_attributes.filter(variant_id=OuterRef("pk")))
)
variant_attribute_filter = Q(
@@ -277,22 +294,24 @@ def filter_products_by_attributes(
queries: dict[int, list[int]] = defaultdict(list)
try:
if filter_values:
- _clean_product_attributes_filter_input(filter_values, queries)
+ _clean_product_attributes_filter_input(filter_values, queries, qs.db)
if filter_range_values:
- _clean_product_attributes_range_filter_input(filter_range_values, queries)
+ _clean_product_attributes_range_filter_input(
+ filter_range_values, queries, qs.db
+ )
if date_range_list:
values_qs = _clean_product_attributes_date_time_range_filter_input(
- date_range_list
+ date_range_list, qs.db
)
return filter_products_by_attributes_values_qs(qs, values_qs)
if date_time_range_list:
values_qs = _clean_product_attributes_date_time_range_filter_input(
- date_time_range_list
+ date_time_range_list, qs.db
)
return filter_products_by_attributes_values_qs(qs, values_qs)
if filter_boolean_values:
_clean_product_attributes_boolean_filter_input(
- filter_boolean_values, queries
+ filter_boolean_values, queries, qs.db
)
except ValueError:
return Product.objects.none()
@@ -300,10 +319,10 @@ def filter_products_by_attributes(
def filter_products_by_variant_price(qs, channel_slug, price_lte=None, price_gte=None):
- channels = Channel.objects.filter(slug=channel_slug).values("pk")
- product_variant_channel_listings = ProductVariantChannelListing.objects.filter(
- Exists(channels.filter(pk=OuterRef("channel_id")))
- )
+ channels = Channel.objects.using(qs.db).filter(slug=channel_slug).values("pk")
+ product_variant_channel_listings = ProductVariantChannelListing.objects.using(
+ qs.db
+ ).filter(Exists(channels.filter(pk=OuterRef("channel_id"))))
if price_lte:
product_variant_channel_listings = product_variant_channel_listings.filter(
Q(price_amount__lte=price_lte) | Q(price_amount__isnull=True)
@@ -315,19 +334,23 @@ def filter_products_by_variant_price(qs, channel_slug, price_lte=None, price_gte
product_variant_channel_listings = product_variant_channel_listings.values(
"variant_id"
)
- variants = ProductVariant.objects.filter(
- Exists(product_variant_channel_listings.filter(variant_id=OuterRef("pk")))
- ).values("product_id")
+ variants = (
+ ProductVariant.objects.using(qs.db)
+ .filter(
+ Exists(product_variant_channel_listings.filter(variant_id=OuterRef("pk")))
+ )
+ .values("product_id")
+ )
return qs.filter(Exists(variants.filter(product_id=OuterRef("pk"))))
def filter_products_by_minimal_price(
qs, channel_slug, minimal_price_lte=None, minimal_price_gte=None
):
- channel = Channel.objects.filter(slug=channel_slug).first()
+ channel = Channel.objects.using(qs.db).filter(slug=channel_slug).first()
if not channel:
return qs
- product_channel_listings = ProductChannelListing.objects.filter(
+ product_channel_listings = ProductChannelListing.objects.using(qs.db).filter(
channel_id=channel.id
)
if minimal_price_lte:
@@ -343,30 +366,36 @@ def filter_products_by_minimal_price(
def filter_products_by_categories(qs, category_ids):
- categories = Category.objects.filter(pk__in=category_ids)
- categories = Category.tree.get_queryset_descendants(
- categories, include_self=True
- ).values("pk")
+ categories = Category.objects.using(qs.db).filter(pk__in=category_ids)
+ categories = (
+ Category.tree.get_queryset_descendants(categories, include_self=True)
+ .using(qs.db)
+ .values("pk")
+ )
return qs.filter(Exists(categories.filter(pk=OuterRef("category_id"))))
def filter_products_by_collections(qs, collection_pks):
- collection_products = CollectionProduct.objects.filter(
- collection_id__in=collection_pks
- ).values("product_id")
+ collection_products = (
+ CollectionProduct.objects.using(qs.db)
+ .filter(collection_id__in=collection_pks)
+ .values("product_id")
+ )
return qs.filter(Exists(collection_products.filter(product_id=OuterRef("pk"))))
def filter_products_by_stock_availability(qs, stock_availability, channel_slug):
allocations = (
- Allocation.objects.values("stock_id")
+ Allocation.objects.using(qs.db)
+ .values("stock_id")
.filter(quantity_allocated__gt=0, stock_id=OuterRef("pk"))
.values_list(Sum("quantity_allocated"))
)
allocated_subquery = Subquery(queryset=allocations, output_field=IntegerField())
reservations = (
- Reservation.objects.values("stock_id")
+ Reservation.objects.using(qs.db)
+ .values("stock_id")
.filter(
quantity_reserved__gt=0,
stock_id=OuterRef("pk"),
@@ -377,16 +406,19 @@ def filter_products_by_stock_availability(qs, stock_availability, channel_slug):
reservation_subquery = Subquery(queryset=reservations, output_field=IntegerField())
stocks = (
- Stock.objects.for_channel_and_country(channel_slug)
+ Stock.objects.using(qs.db)
+ .for_channel_and_country(channel_slug)
.filter(
quantity__gt=Coalesce(allocated_subquery, 0)
+ Coalesce(reservation_subquery, 0)
)
.values("product_variant_id")
)
- variants = ProductVariant.objects.filter(
- Exists(stocks.filter(product_variant_id=OuterRef("pk")))
- ).values("product_id")
+ variants = (
+ ProductVariant.objects.using(qs.db)
+ .filter(Exists(stocks.filter(product_variant_id=OuterRef("pk"))))
+ .values("product_id")
+ )
if stock_availability == StockAvailability.IN_STOCK:
qs = qs.filter(Exists(variants.filter(product_id=OuterRef("pk"))))
@@ -451,7 +483,8 @@ def filter_has_category(qs, _, value):
def filter_has_preordered_variants(qs, _, value):
variants = (
- ProductVariant.objects.filter(is_preorder=True)
+ ProductVariant.objects.using(qs.db)
+ .filter(is_preorder=True)
.filter(
Q(preorder_end_date__isnull=True) | Q(preorder_end_date__gt=timezone.now())
)
@@ -473,19 +506,27 @@ def filter_collections(qs, _, value):
def _filter_products_is_published(qs, _, value, channel_slug):
- channel = Channel.objects.filter(slug=channel_slug).values("pk")
- product_channel_listings = ProductChannelListing.objects.filter(
- Exists(channel.filter(pk=OuterRef("channel_id"))), is_published=value
- ).values("product_id")
+ channel = Channel.objects.using(qs.db).filter(slug=channel_slug).values("pk")
+ product_channel_listings = (
+ ProductChannelListing.objects.using(qs.db)
+ .filter(Exists(channel.filter(pk=OuterRef("channel_id"))), is_published=value)
+ .values("product_id")
+ )
# Filter out product for which there is no variant with price
- variant_channel_listings = ProductVariantChannelListing.objects.filter(
- Exists(channel.filter(pk=OuterRef("channel_id"))),
- price_amount__isnull=False,
- ).values("id")
- variants = ProductVariant.objects.filter(
- Exists(variant_channel_listings.filter(variant_id=OuterRef("pk")))
- ).values("product_id")
+ variant_channel_listings = (
+ ProductVariantChannelListing.objects.using(qs.db)
+ .filter(
+ Exists(channel.filter(pk=OuterRef("channel_id"))),
+ price_amount__isnull=False,
+ )
+ .values("id")
+ )
+ variants = (
+ ProductVariant.objects.using(qs.db)
+ .filter(Exists(variant_channel_listings.filter(variant_id=OuterRef("pk"))))
+ .values("product_id")
+ )
return qs.filter(
Exists(product_channel_listings.filter(product_id=OuterRef("pk"))),
@@ -494,41 +535,57 @@ def _filter_products_is_published(qs, _, value, channel_slug):
def _filter_products_is_available(qs, _, value, channel_slug):
- channel = Channel.objects.filter(slug=channel_slug).values("pk")
+ channel = Channel.objects.using(qs.db).filter(slug=channel_slug).values("pk")
now = datetime.datetime.now(pytz.UTC)
if value:
- product_channel_listings = ProductChannelListing.objects.filter(
- Exists(channel.filter(pk=OuterRef("channel_id"))),
- available_for_purchase_at__lte=now,
- ).values("product_id")
+ product_channel_listings = (
+ ProductChannelListing.objects.using(qs.db)
+ .filter(
+ Exists(channel.filter(pk=OuterRef("channel_id"))),
+ available_for_purchase_at__lte=now,
+ )
+ .values("product_id")
+ )
else:
- product_channel_listings = ProductChannelListing.objects.filter(
- Exists(channel.filter(pk=OuterRef("channel_id"))),
- Q(available_for_purchase_at__gt=now)
- | Q(available_for_purchase_at__isnull=True),
- ).values("product_id")
+ product_channel_listings = (
+ ProductChannelListing.objects.using(qs.db)
+ .filter(
+ Exists(channel.filter(pk=OuterRef("channel_id"))),
+ Q(available_for_purchase_at__gt=now)
+ | Q(available_for_purchase_at__isnull=True),
+ )
+ .values("product_id")
+ )
return qs.filter(Exists(product_channel_listings.filter(product_id=OuterRef("pk"))))
def _filter_products_channel_field_from_date(qs, _, value, channel_slug, field):
- channel = Channel.objects.filter(slug=channel_slug).values("pk")
+ channel = Channel.objects.using(qs.db).filter(slug=channel_slug).values("pk")
lookup = {
f"{field}__lte": value,
}
- product_channel_listings = ProductChannelListing.objects.filter(
- Exists(channel.filter(pk=OuterRef("channel_id"))),
- **lookup,
- ).values("product_id")
+ product_channel_listings = (
+ ProductChannelListing.objects.using(qs.db)
+ .filter(
+ Exists(channel.filter(pk=OuterRef("channel_id"))),
+ **lookup,
+ )
+ .values("product_id")
+ )
return qs.filter(Exists(product_channel_listings.filter(product_id=OuterRef("pk"))))
def _filter_products_visible_in_listing(qs, _, value, channel_slug):
- channel = Channel.objects.filter(slug=channel_slug).values("pk")
- product_channel_listings = ProductChannelListing.objects.filter(
- Exists(channel.filter(pk=OuterRef("channel_id"))), visible_in_listings=value
- ).values("product_id")
+ channel = Channel.objects.using(qs.db).filter(slug=channel_slug).values("pk")
+ product_channel_listings = (
+ ProductChannelListing.objects.using(qs.db)
+ .filter(
+ Exists(channel.filter(pk=OuterRef("channel_id"))), visible_in_listings=value
+ )
+ .values("product_id")
+ )
return qs.filter(Exists(product_channel_listings.filter(product_id=OuterRef("pk"))))
@@ -568,7 +625,9 @@ def _filter_collections_is_published(qs, _, value, channel_slug):
def filter_gift_card(qs, _, value):
- product_types = ProductType.objects.filter(kind=ProductTypeKind.GIFT_CARD)
+ product_types = ProductType.objects.using(qs.db).filter(
+ kind=ProductTypeKind.GIFT_CARD
+ )
lookup = Exists(product_types.filter(id=OuterRef("product_type_id")))
return qs.filter(lookup) if value is True else qs.exclude(lookup)
@@ -612,11 +671,17 @@ def filter_warehouses(qs, _, value):
_, warehouse_pks = resolve_global_ids_to_primary_keys(
value, warehouse_types.Warehouse
)
- warehouses = Warehouse.objects.filter(pk__in=warehouse_pks).values("pk")
- variant_ids = Stock.objects.filter(
- Exists(warehouses.filter(pk=OuterRef("warehouse")))
- ).values("product_variant_id")
- variants = ProductVariant.objects.filter(id__in=variant_ids).values("pk")
+ warehouses = (
+ Warehouse.objects.using(qs.db).filter(pk__in=warehouse_pks).values("pk")
+ )
+ variant_ids = (
+ Stock.objects.using(qs.db)
+ .filter(Exists(warehouses.filter(pk=OuterRef("warehouse"))))
+ .values("product_variant_id")
+ )
+ variants = (
+ ProductVariant.objects.using(qs.db).filter(id__in=variant_ids).values("pk")
+ )
return qs.filter(Exists(variants.filter(product=OuterRef("pk"))))
return qs
@@ -643,7 +708,7 @@ def filter_quantity(qs, quantity_value, warehouse_ids=None):
between given range. If warehouses is given, it aggregates quantity only
from stocks which are in given warehouses.
"""
- stocks = Stock.objects.all()
+ stocks = Stock.objects.using(qs.db).all()
if warehouse_ids:
_, warehouse_pks = resolve_global_ids_to_primary_keys(
warehouse_ids, warehouse_types.Warehouse
@@ -654,7 +719,7 @@ def filter_quantity(qs, quantity_value, warehouse_ids=None):
)
stocks = Subquery(stocks.values_list(Sum("quantity")))
- variants = ProductVariant.objects.annotate(
+ variants = ProductVariant.objects.using(qs.db).annotate(
total_quantity=ExpressionWrapper(stocks, output_field=IntegerField())
)
variants = list(
@@ -813,19 +878,27 @@ class ProductFilter(MetadataFilterBase):
def where_filter_products_is_available(qs, _, value, channel_slug):
if value is None:
return qs.none()
- channel = Channel.objects.filter(slug=channel_slug).values("pk")
+ channel = Channel.objects.using(qs.db).filter(slug=channel_slug).values("pk")
now = datetime.datetime.now(pytz.UTC)
if value:
- product_channel_listings = ProductChannelListing.objects.filter(
- Exists(channel.filter(pk=OuterRef("channel_id"))),
- available_for_purchase_at__lte=now,
- ).values("product_id")
+ product_channel_listings = (
+ ProductChannelListing.objects.using(qs.db)
+ .filter(
+ Exists(channel.filter(pk=OuterRef("channel_id"))),
+ available_for_purchase_at__lte=now,
+ )
+ .values("product_id")
+ )
else:
- product_channel_listings = ProductChannelListing.objects.filter(
- Exists(channel.filter(pk=OuterRef("channel_id"))),
- Q(available_for_purchase_at__gt=now)
- | Q(available_for_purchase_at__isnull=True),
- ).values("product_id")
+ product_channel_listings = (
+ ProductChannelListing.objects.using(qs.db)
+ .filter(
+ Exists(channel.filter(pk=OuterRef("channel_id"))),
+ Q(available_for_purchase_at__gt=now)
+ | Q(available_for_purchase_at__isnull=True),
+ )
+ .values("product_id")
+ )
return qs.filter(Exists(product_channel_listings.filter(product_id=OuterRef("pk"))))
@@ -833,14 +906,18 @@ def where_filter_products_is_available(qs, _, value, channel_slug):
def where_filter_products_channel_field_from_date(qs, _, value, channel_slug, field):
if value is None:
return qs.none()
- channel = Channel.objects.filter(slug=channel_slug).values("pk")
+ channel = Channel.objects.using(qs.db).filter(slug=channel_slug).values("pk")
lookup = {
f"{field}__lte": value,
}
- product_channel_listings = ProductChannelListing.objects.filter(
- Exists(channel.filter(pk=OuterRef("channel_id"))),
- **lookup,
- ).values("product_id")
+ product_channel_listings = (
+ ProductChannelListing.objects.using(qs.db)
+ .filter(
+ Exists(channel.filter(pk=OuterRef("channel_id"))),
+ **lookup,
+ )
+ .values("product_id")
+ )
return qs.filter(Exists(product_channel_listings.filter(product_id=OuterRef("pk"))))
@@ -905,11 +982,17 @@ def where_filter_warehouses(qs, _, value):
_, warehouse_pks = resolve_global_ids_to_primary_keys(
value, warehouse_types.Warehouse
)
- warehouses = Warehouse.objects.filter(pk__in=warehouse_pks).values("pk")
- variant_ids = Stock.objects.filter(
- Exists(warehouses.filter(pk=OuterRef("warehouse")))
- ).values("product_variant_id")
- variants = ProductVariant.objects.filter(id__in=variant_ids).values("pk")
+ warehouses = (
+ Warehouse.objects.using(qs.db).filter(pk__in=warehouse_pks).values("pk")
+ )
+ variant_ids = (
+ Stock.objects.using(qs.db)
+ .filter(Exists(warehouses.filter(pk=OuterRef("warehouse"))))
+ .values("product_variant_id")
+ )
+ variants = (
+ ProductVariant.objects.using(qs.db).filter(id__in=variant_ids).values("pk")
+ )
return qs.filter(Exists(variants.filter(product=OuterRef("pk"))))
@@ -920,7 +1003,7 @@ def where_filter_quantity(qs, quantity_value, warehouse_ids=None):
between given range. If warehouses is given, it aggregates quantity only
from stocks which are in given warehouses.
"""
- stocks = Stock.objects.all()
+ stocks = Stock.objects.using(qs.db).all()
if warehouse_ids:
_, warehouse_pks = resolve_global_ids_to_primary_keys(
warehouse_ids, warehouse_types.Warehouse
@@ -931,7 +1014,7 @@ def where_filter_quantity(qs, quantity_value, warehouse_ids=None):
)
stocks = Subquery(stocks.values_list(Sum("quantity")))
- variants = ProductVariant.objects.annotate(
+ variants = ProductVariant.objects.using(qs.db).annotate(
total_quantity=ExpressionWrapper(stocks, output_field=IntegerField())
)
variants = list(
@@ -959,7 +1042,9 @@ def where_filter_gift_card(qs, _, value):
if value is None:
return qs.none()
- product_types = ProductType.objects.filter(kind=ProductTypeKind.GIFT_CARD)
+ product_types = ProductType.objects.using(qs.db).filter(
+ kind=ProductTypeKind.GIFT_CARD
+ )
lookup = Exists(product_types.filter(id=OuterRef("product_type_id")))
return qs.filter(lookup) if value is True else qs.exclude(lookup)
@@ -969,7 +1054,8 @@ def where_filter_has_preordered_variants(qs, _, value):
return qs.none()
variants = (
- ProductVariant.objects.filter(is_preorder=True)
+ ProductVariant.objects.using(qs.db)
+ .filter(is_preorder=True)
.filter(
Q(preorder_end_date__isnull=True) | Q(preorder_end_date__gt=timezone.now())
)
@@ -1099,7 +1185,7 @@ class ProductWhere(MetadataWhereFilterBase):
@staticmethod
def filter_collection(qs, _, value):
- collection_products_qs = CollectionProduct.objects.filter()
+ collection_products_qs = CollectionProduct.objects.using(qs.db).filter()
collection_products_qs = filter_where_by_id_field(
collection_products_qs, "collection_id", value, "Collection"
)
@@ -1155,25 +1241,29 @@ class ProductWhere(MetadataWhereFilterBase):
def filter_variant_price(self, qs, _, value):
channel_slug = get_channel_slug_from_filter_data(self.data)
- channel_id = Channel.objects.filter(slug=channel_slug).values("pk")
- variant_listing = ProductVariantChannelListing.objects.filter(
+ channel_id = Channel.objects.using(qs.db).filter(slug=channel_slug).values("pk")
+ variant_listing = ProductVariantChannelListing.objects.using(qs.db).filter(
Exists(channel_id.filter(pk=OuterRef("channel_id")))
)
variant_listing = filter_where_by_numeric_field(
variant_listing, "price_amount", value
)
variant_listing = variant_listing.values("variant_id")
- variants = ProductVariant.objects.filter(
- Exists(variant_listing.filter(variant_id=OuterRef("pk")))
- ).values("product_id")
+ variants = (
+ ProductVariant.objects.using(qs.db)
+ .filter(Exists(variant_listing.filter(variant_id=OuterRef("pk"))))
+ .values("product_id")
+ )
return qs.filter(Exists(variants.filter(product_id=OuterRef("pk"))))
def filter_minimal_price(self, qs, _, value):
channel_slug = get_channel_slug_from_filter_data(self.data)
- channel = Channel.objects.filter(slug=channel_slug).first()
+ channel = Channel.objects.using(qs.db).filter(slug=channel_slug).first()
if not channel:
return qs
- product_listing = ProductChannelListing.objects.filter(channel_id=channel.id)
+ product_listing = ProductChannelListing.objects.using(qs.db).filter(
+ channel_id=channel.id
+ )
product_listing = filter_where_by_numeric_field(
product_listing, "discounted_price_amount", value
)
@@ -1205,7 +1295,9 @@ class ProductVariantFilter(MetadataFilterBase):
if not value:
return queryset
qs = Q(name__ilike=value) | Q(sku__ilike=value)
- products = Product.objects.filter(name__ilike=value).values("pk")
+ products = (
+ Product.objects.using(queryset.db).filter(name__ilike=value).values("pk")
+ )
qs |= Q(Exists(products.filter(variants=OuterRef("pk"))))
return queryset.filter(qs)
diff --git a/saleor/graphql/product/resolvers.py b/saleor/graphql/product/resolvers.py
index a4d496ba25..841be576d9 100644
--- a/saleor/graphql/product/resolvers.py
+++ b/saleor/graphql/product/resolvers.py
@@ -26,8 +26,8 @@ def resolve_categories(info: ResolveInfo, level=None):
def resolve_collection_by_id(info: ResolveInfo, id, channel_slug, requestor):
return (
- models.Collection.objects.visible_to_user(requestor, channel_slug=channel_slug)
- .using(get_database_connection_name(info.context))
+ models.Collection.objects.using(get_database_connection_name(info.context))
+ .visible_to_user(requestor, channel_slug=channel_slug)
.filter(id=id)
.first()
)
@@ -35,8 +35,8 @@ def resolve_collection_by_id(info: ResolveInfo, id, channel_slug, requestor):
def resolve_collection_by_slug(info: ResolveInfo, slug, channel_slug, requestor):
return (
- models.Collection.objects.visible_to_user(requestor, channel_slug)
- .using(get_database_connection_name(info.context))
+ models.Collection.objects.using(get_database_connection_name(info.context))
+ .visible_to_user(requestor, channel_slug)
.filter(slug=slug)
.first()
)
@@ -44,9 +44,9 @@ def resolve_collection_by_slug(info: ResolveInfo, slug, channel_slug, requestor)
def resolve_collections(info: ResolveInfo, channel_slug):
requestor = get_user_or_app_from_context(info.context)
- qs = models.Collection.objects.visible_to_user(requestor, channel_slug).using(
+ qs = models.Collection.objects.using(
get_database_connection_name(info.context)
- )
+ ).visible_to_user(requestor, channel_slug)
return ChannelQsContext(qs=qs, channel_slug=channel_slug)
@@ -135,8 +135,8 @@ def resolve_variant(
):
connection_name = get_database_connection_name(info.context)
visible_products = (
- models.Product.objects.visible_to_user(requestor, channel_slug)
- .using(connection_name)
+ models.Product.objects.using(connection_name)
+ .visible_to_user(requestor, channel_slug)
.values_list("pk", flat=True)
)
qs = models.ProductVariant.objects.using(connection_name).filter(
@@ -162,9 +162,9 @@ def resolve_product_variants(
channel_slug=None,
) -> ChannelQsContext:
connection_name = get_database_connection_name(info.context)
- visible_products = models.Product.objects.visible_to_user(
+ visible_products = models.Product.objects.using(connection_name).visible_to_user(
requestor, channel_slug
- ).using(connection_name)
+ )
qs = models.ProductVariant.objects.using(connection_name).filter(
product__id__in=visible_products
)
@@ -174,9 +174,9 @@ def resolve_product_variants(
channel_slug
).exclude(visible_in_listings=False)
qs = (
- qs.filter(product__in=visible_products)
+ qs.using(connection_name)
+ .filter(product__in=visible_products)
.available_in_channel(channel_slug)
- .using(connection_name)
)
if ids:
db_ids = [
diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.py
index c00a91813c..21adae5174 100644
--- a/saleor/graphql/product/schema.py
+++ b/saleor/graphql/product/schema.py
@@ -342,7 +342,9 @@ class ProductQueries(graphene.ObjectType):
@staticmethod
def resolve_categories(_root, info: ResolveInfo, *, level=None, **kwargs):
qs = resolve_categories(info, level=level)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, CategoryCountableConnection)
@staticmethod
@@ -393,7 +395,9 @@ class ProductQueries(graphene.ObjectType):
channel = get_default_channel_slug_or_graphql_error()
qs = resolve_collections(info, channel)
kwargs["channel"] = channel
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, CollectionCountableConnection)
@staticmethod
@@ -460,7 +464,9 @@ class ProductQueries(graphene.ObjectType):
qs=search_products(qs.qs, search), channel_slug=channel
)
kwargs["channel"] = channel
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, ProductCountableConnection)
@staticmethod
@@ -471,7 +477,9 @@ class ProductQueries(graphene.ObjectType):
@staticmethod
def resolve_product_types(_root, info: ResolveInfo, **kwargs):
qs = resolve_product_types(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, ProductTypeCountableConnection)
@staticmethod
@@ -526,7 +534,9 @@ class ProductQueries(graphene.ObjectType):
requestor=requestor,
)
kwargs["channel"] = qs.channel_slug
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(
qs, info, kwargs, ProductVariantCountableConnection
)
diff --git a/saleor/graphql/product/types/categories.py b/saleor/graphql/product/types/categories.py
index f81fbecb4d..4ae8420f38 100644
--- a/saleor/graphql/product/types/categories.py
+++ b/saleor/graphql/product/types/categories.py
@@ -189,12 +189,17 @@ class Category(ModelObjectType[models.Category]):
qs = ChannelQsContext(qs=qs, channel_slug=channel)
kwargs["channel"] = channel
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, ProductCountableConnection)
@staticmethod
- def __resolve_references(roots: list["Category"], _info):
- return resolve_federation_references(Category, roots, models.Category.objects)
+ def __resolve_references(roots: list["Category"], info):
+ database_connection_name = get_database_connection_name(info.context)
+ return resolve_federation_references(
+ Category, roots, models.Category.objects.using(database_connection_name)
+ )
class CategoryCountableConnection(CountableConnection):
diff --git a/saleor/graphql/product/types/collections.py b/saleor/graphql/product/types/collections.py
index 4dbaa9421a..952e5bcce0 100644
--- a/saleor/graphql/product/types/collections.py
+++ b/saleor/graphql/product/types/collections.py
@@ -142,9 +142,9 @@ class Collection(ChannelContextTypeWithMetadata[models.Collection]):
search = kwargs.get("search")
requestor = get_user_or_app_from_context(info.context)
- qs = root.node.products.visible_to_user(requestor, root.channel_slug).using(
+ qs = root.node.products.using(
get_database_connection_name(info.context)
- )
+ ).visible_to_user(requestor, root.channel_slug)
if search:
qs = ChannelQsContext(
@@ -154,7 +154,9 @@ class Collection(ChannelContextTypeWithMetadata[models.Collection]):
qs = ChannelQsContext(qs=qs, channel_slug=root.channel_slug)
kwargs["channel"] = root.channel_slug
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, ProductCountableConnection)
@staticmethod
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index a77e206f9b..a1a4f38f5e 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -1824,18 +1824,22 @@ class ProductType(ModelObjectType[models.ProductType]):
requestor = get_user_or_app_from_context(info.context)
if channel is None:
channel = get_default_channel_slug_or_graphql_error()
- qs = root.products.visible_to_user(requestor, channel)
+ qs = root.products.using(
+ get_database_connection_name(info.context)
+ ).visible_to_user(requestor, channel)
qs = ChannelQsContext(qs=qs, channel_slug=channel)
kwargs["channel"] = channel
return create_connection_slice(qs, info, kwargs, ProductCountableConnection)
@staticmethod
def resolve_available_attributes(root: models.ProductType, info, **kwargs):
- qs = attribute_models.Attribute.objects.get_unassigned_product_type_attributes(
- root.pk
- ).using(get_database_connection_name(info.context))
+ qs = attribute_models.Attribute.objects.using(
+ get_database_connection_name(info.context)
+ ).get_unassigned_product_type_attributes(root.pk)
qs = resolve_attributes(info, qs=qs)
- qs = filter_connection_queryset(qs, kwargs, info.context)
+ qs = filter_connection_queryset(
+ qs, kwargs, info.context, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, AttributeCountableConnection)
@staticmethod
@@ -1851,9 +1855,12 @@ class ProductType(ModelObjectType[models.ProductType]):
)
@staticmethod
- def __resolve_references(roots: list["ProductType"], _info):
+ def __resolve_references(roots: list["ProductType"], info):
+ database_connection_name = get_database_connection_name(info.context)
return resolve_federation_references(
- ProductType, roots, models.ProductType.objects
+ ProductType,
+ roots,
+ models.ProductType.objects.using(database_connection_name),
)
@@ -1918,9 +1925,12 @@ class ProductMedia(ModelObjectType[models.ProductMedia]):
)
@staticmethod
- def __resolve_references(roots: list["ProductMedia"], _info):
+ def __resolve_references(roots: list["ProductMedia"], info):
+ database_connection_name = get_database_connection_name(info.context)
return resolve_federation_references(
- ProductMedia, roots, models.ProductMedia.objects
+ ProductMedia,
+ roots,
+ models.ProductMedia.objects.using(database_connection_name),
)
@staticmethod
diff --git a/saleor/graphql/shipping/schema.py b/saleor/graphql/shipping/schema.py
index 672112eb33..c2095cd7c7 100644
--- a/saleor/graphql/shipping/schema.py
+++ b/saleor/graphql/shipping/schema.py
@@ -70,7 +70,9 @@ class ShippingQueries(graphene.ObjectType):
@staticmethod
def resolve_shipping_zones(_root, info: ResolveInfo, *, channel=None, **kwargs):
qs = resolve_shipping_zones(info, channel)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(
qs, info, kwargs, ShippingZoneCountableConnection
)
diff --git a/saleor/graphql/tax/schema.py b/saleor/graphql/tax/schema.py
index 73dfb375c3..fe758f8d9f 100644
--- a/saleor/graphql/tax/schema.py
+++ b/saleor/graphql/tax/schema.py
@@ -125,7 +125,8 @@ class TaxQueries(graphene.ObjectType):
qs = models.TaxConfiguration.objects.using(
get_database_connection_name(info.context)
).all()
- qs = filter_connection_queryset(qs, kwargs)
+ allow_replica = getattr(info.context, "allow_replica", True)
+ qs = filter_connection_queryset(qs, kwargs, allow_replica=allow_replica)
return create_connection_slice(
qs, info, kwargs, TaxConfigurationCountableConnection
)
@@ -144,7 +145,8 @@ class TaxQueries(graphene.ObjectType):
qs = models.TaxClass.objects.using(
get_database_connection_name(info.context)
).all()
- qs = filter_connection_queryset(qs, kwargs)
+ allow_replica = getattr(info.context, "allow_replica", True)
+ qs = filter_connection_queryset(qs, kwargs, allow_replica=allow_replica)
return create_connection_slice(qs, info, kwargs, TaxClassCountableConnection)
@staticmethod
diff --git a/saleor/graphql/translations/types.py b/saleor/graphql/translations/types.py
index 7bad3b2d4b..df4281852a 100644
--- a/saleor/graphql/translations/types.py
+++ b/saleor/graphql/translations/types.py
@@ -563,8 +563,8 @@ class PageTranslatableContent(ModelObjectType[page_models.Page]):
@staticmethod
def resolve_page(root: page_models.Page, info):
return (
- page_models.Page.objects.visible_to_user(info.context.user)
- .using(get_database_connection_name(info.context))
+ page_models.Page.objects.using(get_database_connection_name(info.context))
+ .visible_to_user(info.context.user)
.filter(pk=root.id)
.first()
)
diff --git a/saleor/graphql/warehouse/schema.py b/saleor/graphql/warehouse/schema.py
index c1ad234927..36107bd20e 100644
--- a/saleor/graphql/warehouse/schema.py
+++ b/saleor/graphql/warehouse/schema.py
@@ -71,7 +71,9 @@ class WarehouseQueries(graphene.ObjectType):
@staticmethod
def resolve_warehouses(_root, info: ResolveInfo, **kwargs):
qs = resolve_warehouses(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, WarehouseCountableConnection)
@@ -107,7 +109,9 @@ class StockQueries(graphene.ObjectType):
@staticmethod
def resolve_stocks(_root, info: ResolveInfo, **kwargs):
qs = resolve_stocks(info)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(qs, info, kwargs, StockCountableConnection)
diff --git a/saleor/graphql/webhook/resolvers.py b/saleor/graphql/webhook/resolvers.py
index ff5ede36eb..6f93c62369 100644
--- a/saleor/graphql/webhook/resolvers.py
+++ b/saleor/graphql/webhook/resolvers.py
@@ -23,15 +23,18 @@ def resolve_webhook(info: ResolveInfo, id, app):
if app:
return app.webhooks.filter(id=id).first()
user = info.context.user
+ database_connection_name = get_database_connection_name(info.context)
if user and user.has_perm(AppPermission.MANAGE_APPS):
apps = (
- App.objects.using(get_database_connection_name(info.context))
+ App.objects.using(database_connection_name)
.filter(removed_at__isnull=True)
.values("pk")
)
- return models.Webhook.objects.filter(
- Q(pk=id), Exists(apps.filter(id=OuterRef("app_id")))
- ).first()
+ return (
+ models.Webhook.objects.using(database_connection_name)
+ .filter(Q(pk=id), Exists(apps.filter(id=OuterRef("app_id"))))
+ .first()
+ )
raise PermissionDenied(permissions=[AppPermission.MANAGE_APPS])
diff --git a/saleor/graphql/webhook/types.py b/saleor/graphql/webhook/types.py
index ec9cf1326a..7dc81344bf 100644
--- a/saleor/graphql/webhook/types.py
+++ b/saleor/graphql/webhook/types.py
@@ -133,7 +133,9 @@ class EventDelivery(ModelObjectType[core_models.EventDelivery]):
qs = core_models.EventDeliveryAttempt.objects.using(
get_database_connection_name(info.context)
).filter(delivery=root)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(
qs, info, kwargs, EventDeliveryAttemptCountableConnection
)
@@ -246,7 +248,9 @@ class Webhook(ModelObjectType[models.Webhook]):
qs = core_models.EventDelivery.objects.using(
get_database_connection_name(info.context)
).filter(webhook_id=root.pk)
- qs = filter_connection_queryset(qs, kwargs)
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
return create_connection_slice(
qs, info, kwargs, EventDeliveryCountableConnection
)
diff --git a/saleor/product/tests/test_product.py b/saleor/product/tests/test_product.py
index 66f2f2ac37..7c4e372a39 100644
--- a/saleor/product/tests/test_product.py
+++ b/saleor/product/tests/test_product.py
@@ -160,7 +160,7 @@ def test_filtering_by_attribute(
def test_clean_product_attributes_date_time_range_filter_input(
- date_attribute, date_time_attribute
+ date_attribute, date_time_attribute, settings
):
# filter date attribute
filter_value = [
@@ -169,7 +169,9 @@ def test_clean_product_attributes_date_time_range_filter_input(
{"gte": datetime(2020, 10, 5, tzinfo=pytz.utc)},
)
]
- values_qs = _clean_product_attributes_date_time_range_filter_input(filter_value)
+ values_qs = _clean_product_attributes_date_time_range_filter_input(
+ filter_value, settings.DATABASE_CONNECTION_DEFAULT_NAME
+ )
assert set(date_attribute.values.all()) == set(values_qs.all())
@@ -179,7 +181,9 @@ def test_clean_product_attributes_date_time_range_filter_input(
{"gte": datetime(2020, 10, 5).date(), "lte": datetime(2020, 11, 4).date()},
)
]
- values_qs = _clean_product_attributes_date_time_range_filter_input(filter_value)
+ values_qs = _clean_product_attributes_date_time_range_filter_input(
+ filter_value, settings.DATABASE_CONNECTION_DEFAULT_NAME
+ )
assert {date_attribute.values.first().pk} == set(
values_qs.values_list("pk", flat=True)
@@ -192,7 +196,9 @@ def test_clean_product_attributes_date_time_range_filter_input(
{"lte": datetime(2020, 11, 4, tzinfo=timezone.utc)},
)
]
- values_qs = _clean_product_attributes_date_time_range_filter_input(filter_value)
+ values_qs = _clean_product_attributes_date_time_range_filter_input(
+ filter_value, settings.DATABASE_CONNECTION_DEFAULT_NAME
+ )
assert {date_attribute.values.first().pk} == set(
values_qs.values_list("pk", flat=True)
@@ -204,15 +210,19 @@ def test_clean_product_attributes_date_time_range_filter_input(
{"lte": datetime(2020, 10, 4, tzinfo=timezone.utc)},
)
]
- values_qs = _clean_product_attributes_date_time_range_filter_input(filter_value)
+ values_qs = _clean_product_attributes_date_time_range_filter_input(
+ filter_value, settings.DATABASE_CONNECTION_DEFAULT_NAME
+ )
assert values_qs.exists() is False
-def test_clean_product_attributes_boolean_filter_input(boolean_attribute):
+def test_clean_product_attributes_boolean_filter_input(boolean_attribute, settings):
filter_value = [(boolean_attribute.slug, True)]
queries = defaultdict(list)
- _clean_product_attributes_boolean_filter_input(filter_value, queries)
+ _clean_product_attributes_boolean_filter_input(
+ filter_value, queries, settings.DATABASE_CONNECTION_DEFAULT_NAME
+ )
assert dict(queries) == {
boolean_attribute.pk: [boolean_attribute.values.first().pk]
@@ -220,13 +230,17 @@ def test_clean_product_attributes_boolean_filter_input(boolean_attribute):
filter_value = [(boolean_attribute.slug, False)]
queries = defaultdict(list)
- _clean_product_attributes_boolean_filter_input(filter_value, queries)
+ _clean_product_attributes_boolean_filter_input(
+ filter_value, queries, settings.DATABASE_CONNECTION_DEFAULT_NAME
+ )
assert dict(queries) == {boolean_attribute.pk: [boolean_attribute.values.last().pk]}
filter_value = [(boolean_attribute.slug, True), (boolean_attribute.slug, False)]
queries = defaultdict(list)
- _clean_product_attributes_boolean_filter_input(filter_value, queries)
+ _clean_product_attributes_boolean_filter_input(
+ filter_value, queries, settings.DATABASE_CONNECTION_DEFAULT_NAME
+ )
assert dict(queries) == {
boolean_attribute.pk: list(