Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix (merge) migrations after rebase
Krzysztof Świątek82318b7f6a5e
diff --git a/saleor/graphql/core/fields.py b/saleor/graphql/core/fields.pyindex edbd1f654d..49ed12bf17 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_for_connection+from ..utils.sorting import sort_queryset_for_connectionfrom .connection import connection_from_queryset_slicefrom .types.common import Weightfrom .types.money import Money, TaxedMoneydiff --git a/saleor/graphql/product/sorters.py b/saleor/graphql/product/sorters.pyindex b511366e5d..b629d82049 100644--- a/saleor/graphql/product/sorters.py+++ b/saleor/graphql/product/sorters.py@@ -53,10 +53,12 @@ class AttributeSortField(graphene.Enum):"Sort attributes by their position in storefront"),AttributeSortField.DASHBOARD_VARIANT_POSITION.name: (- "Deprecated: Sort variant attributes by their position in dashboard."+ "DEPRECATED: Will be removed in Saleor 2.11. "+ "Sort variant attributes by their position in dashboard."),AttributeSortField.DASHBOARD_PRODUCT_POSITION.name: (- "Deprecated: Sort product attributes by their position in dashboard."+ "DEPRECATED: Will be removed in Saleor 2.11. "+ "Sort product attributes by their position in dashboard."),AttributeSortField.AVAILABLE_IN_GRID.name: ("Sort attributes based on whether they can be displayed "diff --git a/saleor/graphql/utils.py b/saleor/graphql/utils/__init__.pysimilarity index 58%rename from saleor/graphql/utils.pyrename to saleor/graphql/utils/__init__.pyindex be471efb42..27f4798a27 100644--- a/saleor/graphql/utils.py+++ b/saleor/graphql/utils/__init__.py@@ -1,15 +1,15 @@-from typing import Tuple, Union+from typing import Unionimport graphene-from django.db.models import Q, QuerySet+from django.db.models import Qfrom django.utils import timezonefrom graphene_django.registry import get_global_registryfrom graphql.error import GraphQLErrorfrom graphql_jwt.utils import jwt_payloadfrom graphql_relay import from_global_id-from .core.enums import OrderDirection, PermissionEnum, ReportingPeriod-from .core.types import PermissionDisplay, SortInputObjectType+from ..core.enums import PermissionEnum, ReportingPeriod+from ..core.types import PermissionDisplayERROR_COULD_NO_RESOLVE_GLOBAL_ID = ("Could not resolve to a node with the global id list of '%s'."@@ -127,107 +127,6 @@ def filter_by_query_param(queryset, query, search_fields):return queryset-def _sort_queryset_by_attribute(queryset, sorting_attribute, sorting_direction):- if sorting_attribute != "":- graphene_type, sorting_attribute = from_global_id(sorting_attribute)- descending = sorting_direction == OrderDirection.DESC- queryset = queryset.sort_by_attribute(sorting_attribute, descending=descending)- return queryset---def sort_queryset_for_connection(iterable, args):- sort_by = args.get("sort_by")- reversed = True if "before" in args else False- if sort_by:- iterable = sort_queryset(queryset=iterable, sort_by=sort_by, reversed=reversed)- else:- iterable, sort_by = sort_queryset_by_default(- queryset=iterable, reversed=reversed- )- args["sort_by"] = sort_by- return iterable, sort_by---def sort_queryset(- queryset: QuerySet, sort_by: SortInputObjectType, reversed: bool-) -> QuerySet:- """Sort queryset according to given parameters.-- rules:- - sorting_field and sorting_attribute cannot be together)- - when sorting_attribute is passed, it is expected that- queryset will have method to sort by attributes- - when sorter has custom sorting method it's name must be like- `prepare_qs_for_sort_{enum_name}` and it must return sorted queryset-- Keyword Arguments:- queryset - queryset to be sorted- sort_by - dictionary with sorting field and direction-- """- sorting_direction = sort_by.direction- if reversed:- sorting_direction = REVERSED_DIRECTION[sorting_direction]-- sorting_field = sort_by.field- sorting_attribute = getattr(sort_by, "attribute_id", None)-- if sorting_field is not None and sorting_attribute is not None:- raise GraphQLError(- "You must provide either `field` or `attributeId` to sort the products."- )- elif sorting_attribute is not None: # empty string as sorting_attribute is valid- return _sort_queryset_by_attribute(- queryset, sorting_attribute, sorting_direction- )-- sort_enum = sort_by._meta.sort_enum- sorting_fields = sort_enum.get(sorting_field)- sorting_field_name = sorting_fields.name.lower()-- custom_sort_by = getattr(sort_enum, f"qs_with_{sorting_field_name}", None)- if custom_sort_by:- queryset = custom_sort_by(queryset)-- sorting_field_value = sorting_fields.value- sorting_list = [f"{sorting_direction}{field}" for field in sorting_field_value]-- return queryset.order_by(*sorting_list)---def get_model_default_ordering(model_class):- deafault_ordering = []- model_ordering = model_class._meta.ordering- for field in model_ordering:- if isinstance(field, str):- deafault_ordering.append(field)- else:- direction = "-" if field.descending else ""- deafault_ordering.append(f"{direction}{field.expression.name}")- return deafault_ordering---def sort_queryset_by_default(- queryset: QuerySet, reversed: bool-) -> Tuple[QuerySet, dict]:- """Sort queryset by it's default ordering."""- queryset_model = queryset.model- deafault_ordering = ["pk"]- if queryset_model and queryset_model._meta.ordering:- deafault_ordering = get_model_default_ordering(queryset_model)-- ordering_fields = [field.replace("-", "") for field in deafault_ordering]- direction = "-" if "-" in deafault_ordering[0] else ""- if reversed:- reversed_direction = REVERSED_DIRECTION[direction]- deafault_ordering = [- f"{reversed_direction}{field}" for field in ordering_fields- ]-- order_by = {"field": ordering_fields, "direction": direction}- return queryset.order_by(*deafault_ordering), order_by--def reporting_period_to_date(period):now = timezone.now()if period == ReportingPeriod.TODAY:diff --git a/saleor/graphql/utils/sorting.py b/saleor/graphql/utils/sorting.pynew file mode 100644index 0000000000..d41d4985b0--- /dev/null+++ b/saleor/graphql/utils/sorting.py@@ -0,0 +1,114 @@+from typing import Tuple++from django.db.models import QuerySet+from graphql.error import GraphQLError+from graphql_relay import from_global_id++from ..core.enums import OrderDirection+from ..core.types import SortInputObjectType++REVERSED_DIRECTION = {+ "-": "",+ "": "-",+}+++def _sort_queryset_by_attribute(queryset, sorting_attribute, sorting_direction):+ if sorting_attribute != "":+ graphene_type, sorting_attribute = from_global_id(sorting_attribute)+ descending = sorting_direction == OrderDirection.DESC+ queryset = queryset.sort_by_attribute(sorting_attribute, descending=descending)+ return queryset+++def sort_queryset_for_connection(iterable, args):+ sort_by = args.get("sort_by")+ reversed = True if "before" in args else False+ if sort_by:+ iterable = sort_queryset(queryset=iterable, sort_by=sort_by, reversed=reversed)+ else:+ iterable, sort_by = sort_queryset_by_default(+ queryset=iterable, reversed=reversed+ )+ args["sort_by"] = sort_by+ return iterable, sort_by+++def sort_queryset(+ queryset: QuerySet, sort_by: SortInputObjectType, reversed: bool+) -> QuerySet:+ """Sort queryset according to given parameters.++ rules:+ - sorting_field and sorting_attribute cannot be together)+ - when sorting_attribute is passed, it is expected that+ queryset will have method to sort by attributes+ - when sorter has custom sorting method it's name must be like+ `prepare_qs_for_sort_{enum_name}` and it must return sorted queryset++ Keyword Arguments:+ queryset - queryset to be sorted+ sort_by - dictionary with sorting field and direction++ """+ sorting_direction = sort_by.direction+ if reversed:+ sorting_direction = REVERSED_DIRECTION[sorting_direction]++ sorting_field = sort_by.field+ sorting_attribute = getattr(sort_by, "attribute_id", None)++ if sorting_field is not None and sorting_attribute is not None:+ raise GraphQLError(+ "You must provide either `field` or `attributeId` to sort the products."+ )+ elif sorting_attribute is not None: # empty string as sorting_attribute is valid+ return _sort_queryset_by_attribute(+ queryset, sorting_attribute, sorting_direction+ )++ sort_enum = sort_by._meta.sort_enum+ sorting_fields = sort_enum.get(sorting_field)+ sorting_field_name = sorting_fields.name.lower()++ custom_sort_by = getattr(sort_enum, f"qs_with_{sorting_field_name}", None)+ if custom_sort_by:+ queryset = custom_sort_by(queryset)++ sorting_field_value = sorting_fields.value+ sorting_list = [f"{sorting_direction}{field}" for field in sorting_field_value]++ return queryset.order_by(*sorting_list)+++def get_model_default_ordering(model_class):+ default_ordering = []+ model_ordering = model_class._meta.ordering+ for field in model_ordering:+ if isinstance(field, str):+ default_ordering.append(field)+ else:+ direction = "-" if field.descending else ""+ default_ordering.append(f"{direction}{field.expression.name}")+ return default_ordering+++def sort_queryset_by_default(+ queryset: QuerySet, reversed: bool+) -> Tuple[QuerySet, dict]:+ """Sort queryset by it's default ordering."""+ queryset_model = queryset.model+ default_ordering = ["pk"]+ if queryset_model and queryset_model._meta.ordering:+ default_ordering = get_model_default_ordering(queryset_model)++ ordering_fields = [field.replace("-", "") for field in default_ordering]+ direction = "-" if "-" in default_ordering[0] else ""+ if reversed:+ reversed_direction = REVERSED_DIRECTION[direction]+ default_ordering = [+ f"{reversed_direction}{field}" for field in ordering_fields+ ]++ order_by = {"field": ordering_fields, "direction": direction}+ return queryset.order_by(*default_ordering), order_bydiff --git a/saleor/order/migrations/0081_merge_20200309_0952.py b/saleor/order/migrations/0081_merge_20200309_0952.pynew file mode 100644index 0000000000..b950a1f4b9--- /dev/null+++ b/saleor/order/migrations/0081_merge_20200309_0952.py@@ -0,0 +1,13 @@+# Generated by Django 2.2.10 on 2020-03-09 14:52++from django.db import migrations+++class Migration(migrations.Migration):++ dependencies = [+ ("order", "0080_invoice"),+ ("order", "0079_auto_20200225_0237"),+ ]++ operations = []