Skip to content

Commit

Permalink
Fix reordering products duplicates (#6666)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-wysocki authored and maarcingebala committed Jan 8, 2021
1 parent ed8484a commit 38545ba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 4 additions & 4 deletions saleor/graphql/product/sorters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import graphene
from django.db.models import Count, F, IntegerField, Min, OuterRef, QuerySet, Subquery
from django.db.models.expressions import Window
from django.db.models.functions import Coalesce, RowNumber
from django.db.models.functions import Coalesce, DenseRank

from ...product.models import Category, Product
from ..core.types import SortInputObjectType
Expand Down Expand Up @@ -140,7 +140,7 @@ class ProductOrderField(graphene.Enum):
TYPE = ["product_type__name", "name", "slug"]
PUBLISHED = ["is_published", "name", "slug"]
PUBLICATION_DATE = ["publication_date", "name", "slug"]
COLLECTION = ["row_number"]
COLLECTION = ["sort_order"]

@property
def description(self):
Expand Down Expand Up @@ -173,8 +173,8 @@ def qs_with_price(queryset: QuerySet) -> QuerySet:
@staticmethod
def qs_with_collection(queryset: QuerySet) -> QuerySet:
return queryset.annotate(
row_number=Window(
expression=RowNumber(),
sort_order=Window(
expression=DenseRank(),
order_by=(
F("collectionproduct__sort_order").asc(nulls_last=True),
F("collectionproduct__id"),
Expand Down
9 changes: 6 additions & 3 deletions saleor/graphql/product/tests/test_product_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,19 @@ def test_sort_products_within_collection(

variables = {
"collectionId": collection_id,
"moves": [{"productId": product, "sortOrder": 1}],
"moves": [
{"productId": product, "sortOrder": 1},
{"productId": second_product, "sortOrder": -1},
],
}
content = get_graphql_content(
staff_api_client.post_graphql(COLLECTION_RESORT_QUERY, variables)
)["data"]["collectionReorderProducts"]

products = content["collection"]["products"]["edges"]
assert products[0]["node"]["id"] == third_product
assert products[1]["node"]["id"] == product
assert products[2]["node"]["id"] == second_product
assert products[1]["node"]["id"] == second_product
assert products[2]["node"]["id"] == product


GET_SORTED_PRODUCTS_QUERY = """
Expand Down

0 comments on commit 38545ba

Please sign in to comment.