Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use names instead of primary keys in product filtering API
Marcin Gębala552521facee8
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.pyindex 7a4c2b2597..b638075ba9 100644--- a/saleor/graphql/api.py+++ b/saleor/graphql/api.py@@ -89,26 +89,41 @@ class CategoryType(DjangoObjectType):price_lte = args.get('price_lte')price_gte = args.get('price_gte')if attributes_filter:++ attributes = ProductAttribute.objects.prefetch_related('values')+ attributes_map = {attribute.name: attribute.pk+ for attribute in attributes}+ values_map = {attr.name: {value.slug: value.pk+ for value in attr.values.all()}+ for attr in attributes}+queries = {}# Convert attribute:value pairs into a dictionary where# attributes are keys and values are grouped in lists- for attr_pk, attr_val_pk in attributes_filter:+ for attr_name, val_slug in attributes_filter:try:- attr_pk, attr_val_pk = int(attr_pk), int(attr_val_pk)- except ValueError:- pass+ attr_pk = attributes_map[attr_name]+ except KeyError:+ raise ValueError("Invalid attribute name: %s" % attr_name)else:- key = 'variants__attributes__%s' % attr_pk- if key not in queries:- queries[key] = [attr_val_pk]+ try:+ attr_val_pk = values_map[attr_name][val_slug]+ except KeyError:+ raise ValueError("Invalid attribute value: %s" %+ val_slug)else:- queries[key].append(attr_val_pk)+ key = 'variants__attributes__%s' % attr_pk+ if key not in queries:+ queries[key] = [attr_val_pk]+ else:+ queries[key].append(attr_val_pk)+if queries:# Combine filters of the same attribute with OR operator# and then combine full query with AND operator.- combine_and = [reduce(operator.or_, [- Q(**{key: v}) for v in values])- for key, values in queries.items()]+ combine_and = [reduce(operator.or_, [Q(**{key: v})+ for v in values])+ for key, values in queries.items()]query = reduce(operator.and_, combine_and)qs = qs.filter(query)if order_by:diff --git a/saleor/product/models/utils.py b/saleor/product/models/utils.pyindex 624a71b801..009950b84d 100644--- a/saleor/product/models/utils.py+++ b/saleor/product/models/utils.py@@ -11,5 +11,5 @@ def get_attributes_display_map(obj, attributes):if choice_obj:display_map[attribute.pk] = choice_objelse:- display_map[attribute.pk] = value+ display_map[attribute.pk] = value_pkreturn display_mapdiff --git a/saleor/static/js/components/ProductFilters.js b/saleor/static/js/components/ProductFilters.jsindex 8f8ee2453b..f6c011dbf3 100644--- a/saleor/static/js/components/ProductFilters.js+++ b/saleor/static/js/components/ProductFilters.js@@ -17,8 +17,9 @@ class ProductFilters extends Component {};}- onClick = (attribute, value) => {- const attrValue = `${attribute}:${value}`;+ onClick = (event) => {+ const { name, value } = event.target;+ const attrValue = `${name}:${value}`;this.setState({filters: Object.assign(this.state.filters,@@ -26,7 +27,6 @@ class ProductFilters extends Component {});const enabled = Object.keys(this.state.filters).filter(key => this.state.filters[key] === true);this.props.onFilterChanged(enabled);-}render() {@@ -42,7 +42,7 @@ class ProductFilters extends Component {return (<li key={value.id} className="item"><label>- <input id={'attr' + attribute.pk + value.pk} type="checkbox" value="" onClick={() => this.onClick(attribute.pk, value.pk)} />+ <input name={attribute.name} type="checkbox" value={value.slug} onClick={this.onClick} />{value.display}</label></li>@@ -67,7 +67,7 @@ export default Relay.createContainer(ProductFilters, {displayvalues {id- pk+ slugdisplaycolor}