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:
parent
b98740b44a
commit
1c1b476d67
@ -99,7 +99,7 @@ class ItemGroup(NestedSet, WebsiteGenerator):
|
|||||||
filter_engine = ProductFiltersBuilder(self.name)
|
filter_engine = ProductFiltersBuilder(self.name)
|
||||||
|
|
||||||
context.field_filters = filter_engine.get_field_filters()
|
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({
|
context.update({
|
||||||
"parents": get_parent_item_groups(self.parent_item_group),
|
"parents": get_parent_item_groups(self.parent_item_group),
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _dict
|
|
||||||
|
|
||||||
|
|
||||||
class ProductFiltersBuilder:
|
class ProductFiltersBuilder:
|
||||||
@ -57,37 +56,31 @@ class ProductFiltersBuilder:
|
|||||||
|
|
||||||
return filter_data
|
return filter_data
|
||||||
|
|
||||||
def get_attribute_fitlers(self):
|
def get_attribute_filters(self):
|
||||||
attributes = [row.attribute for row in self.doc.filter_attributes]
|
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:
|
result = frappe.db.sql(
|
||||||
selected_attributes = []
|
"""
|
||||||
for attr in attr_doc.item_attribute_values:
|
select
|
||||||
or_filters = []
|
distinct attribute, attribute_value
|
||||||
filters= [
|
from
|
||||||
["Item Variant Attribute", "attribute", "=", attr.parent],
|
`tabItem Variant Attribute`
|
||||||
["Item Variant Attribute", "attribute_value", "=", attr.attribute_value]
|
where
|
||||||
]
|
attribute in %(attributes)s
|
||||||
if self.item_group:
|
and attribute_value is not null
|
||||||
or_filters.extend([
|
""",
|
||||||
["item_group", "=", self.item_group],
|
{"attributes": attributes},
|
||||||
["Website Item Group", "item_group", "=", self.item_group]
|
as_dict=1,
|
||||||
])
|
)
|
||||||
|
|
||||||
if frappe.db.get_all("Item", filters, or_filters=or_filters, limit=1):
|
attribute_value_map = {}
|
||||||
selected_attributes.append(attr)
|
for d in result:
|
||||||
|
attribute_value_map.setdefault(d.attribute, []).append(d.attribute_value)
|
||||||
|
|
||||||
if selected_attributes:
|
out = []
|
||||||
valid_attributes.append(
|
for name, values in attribute_value_map.items():
|
||||||
_dict(
|
out.append(frappe._dict(name=name, item_attribute_values=values))
|
||||||
item_attribute_values=selected_attributes,
|
return out
|
||||||
name=attr_doc.name
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
return valid_attributes
|
|
||||||
|
@ -98,14 +98,14 @@
|
|||||||
<div class="filter-options">
|
<div class="filter-options">
|
||||||
{% for attr_value in attribute.item_attribute_values %}
|
{% for attr_value in attribute.item_attribute_values %}
|
||||||
<div class="checkbox">
|
<div class="checkbox">
|
||||||
<label data-value="{{ value }}">
|
<label>
|
||||||
<input type="checkbox"
|
<input type="checkbox"
|
||||||
class="product-filter attribute-filter"
|
class="product-filter attribute-filter"
|
||||||
id="{{attr_value.name}}"
|
id="{{attr_value}}"
|
||||||
data-attribute-name="{{ attribute.name }}"
|
data-attribute-name="{{ attribute.name }}"
|
||||||
data-attribute-value="{{ attr_value.attribute_value }}"
|
data-attribute-value="{{ attr_value }}"
|
||||||
{% if attr_value.checked %} checked {% endif %}>
|
{% if attr_value.checked %} checked {% endif %}>
|
||||||
<span class="label-area">{{ attr_value.attribute_value }}</span>
|
<span class="label-area">{{ attr_value }}</span>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
@ -27,7 +27,7 @@ def get_context(context):
|
|||||||
filter_engine = ProductFiltersBuilder()
|
filter_engine = ProductFiltersBuilder()
|
||||||
|
|
||||||
context.field_filters = filter_engine.get_field_filters()
|
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.product_settings = product_settings
|
||||||
context.body_class = "product-page"
|
context.body_class = "product-page"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user