Skip to content

Commit

Permalink
perf: Optimize get_attribute_filters (frappe#26729)
Browse files Browse the repository at this point in the history
* perf: Optimize get_attribute_filters

* fix: handle when filter attributes are undefined

* chore: unused imports

Co-authored-by: Ankush Menat <ankush@iwebnotes.com>
  • Loading branch information
netchampfaris and ankush authored Sep 14, 2021
1 parent b98740b commit 1c1b476
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 39 deletions.
2 changes: 1 addition & 1 deletion erpnext/setup/doctype/item_group/item_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def get_context(self, context):
filter_engine = ProductFiltersBuilder(self.name)

context.field_filters = filter_engine.get_field_filters()
context.attribute_filters = filter_engine.get_attribute_fitlers()
context.attribute_filters = filter_engine.get_attribute_filters()

context.update({
"parents": get_parent_item_groups(self.parent_item_group),
Expand Down
59 changes: 26 additions & 33 deletions erpnext/shopping_cart/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from __future__ import unicode_literals

import frappe
from frappe import _dict


class ProductFiltersBuilder:
Expand Down Expand Up @@ -57,37 +56,31 @@ def get_field_filters(self):

return filter_data

def get_attribute_fitlers(self):
def get_attribute_filters(self):
attributes = [row.attribute for row in self.doc.filter_attributes]
attribute_docs = [
frappe.get_doc('Item Attribute', attribute) for attribute in attributes
]

valid_attributes = []

for attr_doc in attribute_docs:
selected_attributes = []
for attr in attr_doc.item_attribute_values:
or_filters = []
filters= [
["Item Variant Attribute", "attribute", "=", attr.parent],
["Item Variant Attribute", "attribute_value", "=", attr.attribute_value]
]
if self.item_group:
or_filters.extend([
["item_group", "=", self.item_group],
["Website Item Group", "item_group", "=", self.item_group]
])

if frappe.db.get_all("Item", filters, or_filters=or_filters, limit=1):
selected_attributes.append(attr)

if selected_attributes:
valid_attributes.append(
_dict(
item_attribute_values=selected_attributes,
name=attr_doc.name
)
)

return valid_attributes
if not attributes:
return []

result = frappe.db.sql(
"""
select
distinct attribute, attribute_value
from
`tabItem Variant Attribute`
where
attribute in %(attributes)s
and attribute_value is not null
""",
{"attributes": attributes},
as_dict=1,
)

attribute_value_map = {}
for d in result:
attribute_value_map.setdefault(d.attribute, []).append(d.attribute_value)

out = []
for name, values in attribute_value_map.items():
out.append(frappe._dict(name=name, item_attribute_values=values))
return out
8 changes: 4 additions & 4 deletions erpnext/www/all-products/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@
<div class="filter-options">
{% for attr_value in attribute.item_attribute_values %}
<div class="checkbox">
<label data-value="{{ value }}">
<label>
<input type="checkbox"
class="product-filter attribute-filter"
id="{{attr_value.name}}"
id="{{attr_value}}"
data-attribute-name="{{ attribute.name }}"
data-attribute-value="{{ attr_value.attribute_value }}"
data-attribute-value="{{ attr_value }}"
{% if attr_value.checked %} checked {% endif %}>
<span class="label-area">{{ attr_value.attribute_value }}</span>
<span class="label-area">{{ attr_value }}</span>
</label>
</div>
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion erpnext/www/all-products/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_context(context):
filter_engine = ProductFiltersBuilder()

context.field_filters = filter_engine.get_field_filters()
context.attribute_filters = filter_engine.get_attribute_fitlers()
context.attribute_filters = filter_engine.get_attribute_filters()

context.product_settings = product_settings
context.body_class = "product-page"
Expand Down

0 comments on commit 1c1b476

Please sign in to comment.