Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix attribute filters in parent category #3535

Merged
merged 4 commits into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ All notable, unreleased changes to this project will be documented in this file.
- Fixed hard-coded site name in order PDFs - #3526 by @NyanKiyoshi
- Extract enums to separate files - #3523 by @maarcingebala
- Add default variant create to Product mutations - #3505 by @fowczarek
- Fix attribute filters in parent category - #3535 by @fowczarek
6 changes: 4 additions & 2 deletions saleor/product/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def _get_product_attributes_lookup(self):
return Q(product_type__products__category=self.category)
categories = self.category.get_descendants(include_self=True)
return Q(product_type__products__category__in=categories)

def _get_variant_attributes_lookup(self):
return Q(product_variant_type__products__category=self.category)
categories = self.category.get_descendants(include_self=True)
return Q(product_variant_type__products__category__in=categories)


class ProductCollectionFilter(ProductFilter):
Expand Down
13 changes: 12 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,22 @@ def category_with_image(db, image): # pylint: disable=W0613


@pytest.fixture
def categories_tree(db):
def categories_tree(db, product_type): # pylint: disable=W0613
parent = Category.objects.create(name='Parent', slug='parent')
parent.children.create(name='Child', slug='child')
child = parent.children.first()

product_attr = product_type.product_attributes.first()
attr_value = product_attr.values.first()
attributes = {smart_text(product_attr.pk): smart_text(attr_value.pk)}

Product.objects.create(
name='Test product', price=Money('10.00', 'USD'),
product_type=product_type, attributes=attributes, category=child)

return parent


@pytest.fixture
def non_default_category(db): # pylint: disable=W0613
return Category.objects.create(name='Not default', slug='not-default')
Expand Down
13 changes: 13 additions & 0 deletions tests/test_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from saleor.product.filters import ProductCategoryFilter


def test_product_category_filter_filters_from_child_category(
product_type, categories_tree):
product_filter = ProductCategoryFilter(data={}, category=categories_tree)
(product_attributes, variant_attributes) = product_filter._get_attributes()

attribut = product_type.product_attributes.get()
variant = product_type.variant_attributes.get()

assert attribut in product_attributes
assert variant in variant_attributes