perf: Optimize get_attribute_filters (#26729)

* perf: Optimize get_attribute_filters

* fix: handle when filter attributes are undefined

* chore: unused imports

Co-authored-by: Ankush Menat <ankush@iwebnotes.com>
This commit is contained in:
Faris Ansari 2021-09-14 11:41:19 +05:30 committed by GitHub
parent b98740b44a
commit 1c1b476d67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 36 deletions

View File

@ -99,7 +99,7 @@ class ItemGroup(NestedSet, WebsiteGenerator):
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),

View File

@ -4,7 +4,6 @@
from __future__ import unicode_literals
import frappe
from frappe import _dict
class ProductFiltersBuilder:
@ -57,37 +56,31 @@ class ProductFiltersBuilder:
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 = []
if not attributes:
return []
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]
])
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,
)
if frappe.db.get_all("Item", filters, or_filters=or_filters, limit=1):
selected_attributes.append(attr)
attribute_value_map = {}
for d in result:
attribute_value_map.setdefault(d.attribute, []).append(d.attribute_value)
if selected_attributes:
valid_attributes.append(
_dict(
item_attribute_values=selected_attributes,
name=attr_doc.name
)
)
return valid_attributes
out = []
for name, values in attribute_value_map.items():
out.append(frappe._dict(name=name, item_attribute_values=values))
return out

View File

@ -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 %}

View File

@ -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"