Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
fix requirements and conntection resolver
Krzysztof Świąteka73f1906713d
diff --git a/saleor/graphql/account/sorters.py b/saleor/graphql/account/sorters.pyindex ae411b6c5c..e7dcbf9798 100644--- a/saleor/graphql/account/sorters.py+++ b/saleor/graphql/account/sorters.py@@ -46,7 +46,7 @@ class UserSortField(graphene.Enum):raise ValueError("Unsupported enum value: %s" % self.value)@staticmethod- def prepare_qs_for_sort_order_count(queryset: QuerySet) -> QuerySet:+ def qs_with_order_count(queryset: QuerySet) -> QuerySet:return queryset.annotate(order_count=Count("orders__id"))diff --git a/saleor/graphql/core/connection.py b/saleor/graphql/core/connection.pyindex 270417c323..f54409dda3 100644--- a/saleor/graphql/core/connection.py+++ b/saleor/graphql/core/connection.py@@ -48,7 +48,8 @@ def get_field(instance, field):def prepare_filter(cursor, sorting_fields, sorting_direction):- """+ """Create filter arguments based on sorting fields.+:param cursor: list of values that are passed from page_info, used for filtering.:param sorting_fields: list of fields that were used for sorting.:param sorting_direction: keyword direction ('lt', gt').@@ -56,19 +57,17 @@ def prepare_filter(cursor, sorting_fields, sorting_direction):(OR: ('first_field__gt', 'first_value_form_cursor'),(AND: ('second_field__gt', 'second_value_form_cursor'),('first_field', 'first_value_form_cursor')),- (AND: ('thrid_field__gt', 'third_value_form_cursor'),- ('second_field__gt', 'second_value_form_cursor'),+ (AND: ('third_field__gt', 'third_value_form_cursor'),+ ('second_field', 'second_value_form_cursor'),('first_field', 'first_value_form_cursor')))"""filter_kwargs = Q()- for field_id in range(len(sorting_fields)):+ for index, field_name in enumerate(sorting_fields):field_expression = {}- for val_id in range(len(cursor[:field_id])):- field_expression[sorting_fields[val_id]] = cursor[val_id]- field_expression[f"{sorting_fields[field_id]}__{sorting_direction}"] = cursor[- field_id- ]+ for cursor_id, cursor_value in enumerate(cursor[:index]):+ field_expression[sorting_fields[cursor_id]] = cursor_value+ field_expression[f"{field_name}__{sorting_direction}"] = cursor[index]filter_kwargs |= Q(**field_expression)return filter_kwargs@@ -77,20 +76,20 @@ def get_field_value(instance, field_name):return get_repr(get_field(instance, field_name))-def connection_args_validation(args):+def validate_connection_args(args):first = args.get("first")last = args.get("last")- if isinstance(first, int) and first < 0 or first and not isinstance(first, int):- raise ValueError("Argument 'first' must be a non-negative integer.")- if isinstance(last, int) and last < 0 or last and not isinstance(last, int):- raise ValueError("Argument 'last' must be a non-negative integer.")+ if first and not (isinstance(first, int) and first > 0):+ raise ValueError('Argument "first" must be a non-negative integer.')+ if last and not (isinstance(last, int) and last > 0):+ raise ValueError('Argument "last" must be a non-negative integer.')if first and last:- raise ValueError("Argument 'last' cannot be combined with 'first'.")+ raise ValueError('Argument "last" cannot be combined with "first".')if first and args.get("before"):- raise ValueError("Argument 'first' cannot be combined with 'before'.")+ raise ValueError('Argument "first" cannot be combined with "before".')if last and args.get("after"):- raise ValueError("Argument 'last' cannot be combined with 'after'.")+ raise ValueError('Argument "last" cannot be combined with "after".')def connection_from_queryset_slice(@@ -106,7 +105,7 @@ def connection_from_queryset_slice(after = args.get("after")first = args.get("first")last = args.get("last")- connection_args_validation(args)+ validate_connection_args(args)cursor_after = from_global_cursor(after) if after else Nonecursor_before = from_global_cursor(before) if before else None@@ -160,7 +159,7 @@ def connection_from_queryset_slice([get_field_value(record, field) for field in sorting_fields]),)- for index, record in enumerate(matching_records)+ for record in matching_records]first_edge_cursor = edges[0].cursor if edges else None@@ -215,6 +214,8 @@ class CountableConnection(NonNullConnection):@staticmethoddef resolve_total_count(root, *_args, **_kwargs):+ if isinstance(root.iterable, list):+ return len(root.iterable)return root.iterable.count()diff --git a/saleor/graphql/core/fields.py b/saleor/graphql/core/fields.pyindex 94c47f785a..47f0d7e10a 100644--- a/saleor/graphql/core/fields.py+++ b/saleor/graphql/core/fields.py@@ -9,7 +9,7 @@ from graphene_django.fields import DjangoConnectionFieldfrom graphql_relay.connection.arrayconnection import connection_from_list_slicefrom promise import Promise-from ..utils import sort_queryset+from ..utils import sort_queryset, sort_queryset_by_defaultfrom .connection import connection_from_queryset_slicefrom .types.common import Weightfrom .types.money import Money, TaxedMoney@@ -191,6 +191,8 @@ class FilterInputConnectionField(PrefetchingConnectionField):sort_by = args.get("sort_by")if sort_by:iterable = sort_queryset(queryset=iterable, sort_by=sort_by)+ else:+ iterable = sort_queryset_by_default(queryset=iterable)filter_input = args.get(filters_name)diff --git a/saleor/graphql/menu/sorters.py b/saleor/graphql/menu/sorters.pyindex 8bd4bec9f0..9b2b69e0f3 100644--- a/saleor/graphql/menu/sorters.py+++ b/saleor/graphql/menu/sorters.py@@ -17,7 +17,7 @@ class MenuSortField(graphene.Enum):raise ValueError("Unsupported enum value: %s" % self.value)@staticmethod- def prepare_qs_for_sort_items_count(queryset: QuerySet) -> QuerySet:+ def qs_with_items_count(queryset: QuerySet) -> QuerySet:return queryset.annotate(items_count=Count("items__id"))diff --git a/saleor/graphql/order/sorters.py b/saleor/graphql/order/sorters.pyindex 3376d03f6c..bbda786531 100644--- a/saleor/graphql/order/sorters.py+++ b/saleor/graphql/order/sorters.py@@ -28,9 +28,7 @@ class OrderSortField(graphene.Enum):raise ValueError("Unsupported enum value: %s" % self.value)@staticmethod- def prepare_qs_for_sort_payment(- queryset: QuerySet, sort_by: SortInputObjectType- ) -> QuerySet:+ def qs_with_payment(queryset: QuerySet, sort_by: SortInputObjectType) -> QuerySet:last_payments = (queryset.exclude(payments__isnull=True).annotate(payment_id=Max("payments__pk"))diff --git a/saleor/graphql/product/sorters.py b/saleor/graphql/product/sorters.pyindex 1438ae1d7c..0dd9de7bab 100644--- a/saleor/graphql/product/sorters.py+++ b/saleor/graphql/product/sorters.py@@ -90,7 +90,7 @@ class CategorySortField(graphene.Enum):raise ValueError("Unsupported enum value: %s" % self.value)@staticmethod- def prepare_qs_for_sort_product_count(queryset: QuerySet) -> QuerySet:+ def qs_with_product_count(queryset: QuerySet) -> QuerySet:return queryset.annotate(product_count=Coalesce(Subquery(@@ -106,7 +106,7 @@ class CategorySortField(graphene.Enum):)@staticmethod- def prepare_qs_for_sort_subcategory_count(queryset: QuerySet) -> QuerySet:+ def qs_with_subcategory_count(queryset: QuerySet) -> QuerySet:return queryset.annotate(subcategory_count=Count("children__id"))@@ -134,7 +134,7 @@ class CollectionSortField(graphene.Enum):raise ValueError("Unsupported enum value: %s" % self.value)@staticmethod- def prepare_qs_for_sort_product_count(queryset: QuerySet) -> QuerySet:+ def qs_with_product_count(queryset: QuerySet) -> QuerySet:return queryset.annotate(product_count=Count("collectionproduct__id"))diff --git a/saleor/graphql/utils.py b/saleor/graphql/utils.pyindex 5a9ad1835b..0540ffc593 100644--- a/saleor/graphql/utils.py+++ b/saleor/graphql/utils.py@@ -124,8 +124,8 @@ def filter_by_query_param(queryset, query, search_fields):def sort_queryset(queryset: QuerySet, sort_by: SortInputObjectType) -> QuerySet:- """- Sort queryset according to given parameters.+ """Sort queryset according to given parameters.+rules:- sorting_field and sorting_attribute cannot be together)- when sorting_attribute is passed, it is expected that@@ -169,9 +169,7 @@ def sort_queryset(queryset: QuerySet, sort_by: SortInputObjectType) -> QuerySet:else [sorting_field_value])- custom_sort_by = getattr(- sort_enum, f"prepare_qs_for_sort_{sorting_field_name}", None- )+ custom_sort_by = getattr(sort_enum, f"qs_with_{sorting_field_name}", None)if custom_sort_by:queryset = custom_sort_by(queryset)sorting_list = [f"{sorting_direction}{field}" for field in sorting_field_value]@@ -179,6 +177,16 @@ def sort_queryset(queryset: QuerySet, sort_by: SortInputObjectType) -> QuerySet:return queryset.order_by(*sorting_list)+def sort_queryset_by_default(queryset: QuerySet) -> QuerySet:+ """Sort queryset by it's default ordering."""+ queryset_model = queryset.model+ deafault_ordering = queryset_model._meta.ordering if queryset_model else []+ if deafault_ordering:+ return queryset.order_by(*deafault_ordering)+ else:+ return queryset++def reporting_period_to_date(period):now = timezone.now()if period == ReportingPeriod.TODAY: