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

Add attribute filters in subcategories #2949

Closed
wants to merge 5 commits into from
Closed

Conversation

pitero92
Copy link

@pitero92 pitero92 commented Sep 23, 2018

This PR fixes a problem with availability of filters in subcategories

Now filters are avaible in subcategories
example

Fixes #2697

@@ -88,10 +88,10 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def _get_product_attributes_lookup(self):
return Q(product_types__products__category=self.category)
return Q(product_types__products__category=self.get_descendants(include_self=True))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self is the ProductCategoryFilter instance, I think you've meant self.category?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm gonna push improved commit in few minutes

@maarcingebala
Copy link
Member

@pitero92 Thanks for opening the PR! We care not only about the quality of our code but also about the quality of all the written part on Github 😉. It would be great if you could:

  • Edit the issue description with an explanation of the problem it fixes and concerning the issue it resolves (if there is one). You should use Github syntax for closing issues.
  • Edit your commit messages to follow these rules of good commit messages (also applies to the PR title).

In case of any question feel free to ask. Thanks!

@pitero92 pitero92 changed the title category gets descendants in filters Take descendants of in filters Sep 25, 2018
@pitero92 pitero92 changed the title Take descendants of in filters Take descendants of self.category Sep 25, 2018
@pitero92 pitero92 changed the title Take descendants of self.category Add attribute filters in subcategories Sep 25, 2018
@pitero92
Copy link
Author

@maarcingebala Thank You for your feedback.

I've already corrected what you asked for.
It was my first PR, I hope that with next one will go better.

@@ -0,0 +1,106 @@
from collections import OrderedDict
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this whole file used? I think it was added accidentally.

@maarcingebala
Copy link
Member

@pitero92 Thanks, now it looks much better! It seems that one of the Python tests is failing on CI, it needs to be fixed before we can merge the PR.

@@ -88,10 +88,10 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def _get_product_attributes_lookup(self):
return Q(product_types__products__category=self.category)
return Q(product_types__products__category=self.category.get_descendants(include_self=True))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two notes about both changes:

  • get_descentants() returns a Queryset, therefore you should use in lookup instead of =, which is suitable for single objects (like it was previously with self.category).
  • Both changed lines exceed the allowed characters limit per line (79). We could improve the formatting e.g. by extracting a variable categories = self.category.get_descendants(include_self=True)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I'd also like to see a test for it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maarcingebala could You explain your 1st point?
This line should look something like return Q(product_types__products__category__in(categories))?
Where categories = self.category.get_descendants(include_self=True)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pitero92 Please refer to the Django's docs regarding the queryset methods: https://docs.djangoproject.com/en/2.1/ref/models/querysets/#in

Getting the category's descendants is correct.

filters.py Outdated

def _get_variant_attributes_lookup(self):
return Q(product_variant_types__products__category=self.category.get_descendants(include_self=True))
return Q(product_variant_types__products__category__in=categories)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Categories will be undefined at this point

filters.py Outdated
@@ -83,12 +83,12 @@ def _get_attribute_choices(self, attribute):


class ProductCategoryFilter(ProductFilter):
categories = self.category.get_descendants(include_self=True)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Pacu2 now is better?

Copy link
Contributor

@Pacu2 Pacu2 Oct 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should take a different approach in resolving bugs.

I think that you could start the Saleor on your local instance and reproduce the situation listed in the issue to confirm that the bug appears in your local copy of Saleor.

Having a bug reproduced, you could make attempts to fix it, as in the more complex situation it might not be possible to fix it via the guessing game.

It's not better, as that would throw an error as self will be undefined at this point. You should define it categories in each function calling for attributes lookup.

@maarcingebala
Copy link
Member

Closing due to inactivity.

@maarcingebala
Copy link
Member

Continued in #3535

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Attribute filters are not available in subcategories
3 participants