mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-18 12:39:18 +00:00
Merge pull request #27931 from marination/optimize-get-attribute-filters-hotfix
perf: Optimize get_attribute_filters (#26729)
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
|
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
|
||||||
# License: GNU General Public License v3. See license.txt
|
# License: GNU General Public License v3. See license.txt
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _dict
|
|
||||||
from frappe.utils import floor
|
from frappe.utils import floor
|
||||||
|
|
||||||
|
|
||||||
@@ -96,38 +95,32 @@ class ProductFiltersBuilder:
|
|||||||
return
|
return
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
def get_discount_filters(self, discounts):
|
def get_discount_filters(self, discounts):
|
||||||
discount_filters = []
|
discount_filters = []
|
||||||
@@ -147,4 +140,4 @@ class ProductFiltersBuilder:
|
|||||||
label = f"{discount}% and below"
|
label = f"{discount}% and below"
|
||||||
discount_filters.append([discount, label])
|
discount_filters.append([discount, label])
|
||||||
|
|
||||||
return discount_filters
|
return discount_filters
|
||||||
|
|||||||
@@ -175,9 +175,7 @@ class TestProductDataEngine(unittest.TestCase):
|
|||||||
|
|
||||||
filter_engine = ProductFiltersBuilder()
|
filter_engine = ProductFiltersBuilder()
|
||||||
attribute_filter = filter_engine.get_attribute_filters()[0]
|
attribute_filter = filter_engine.get_attribute_filters()[0]
|
||||||
attributes = attribute_filter.item_attribute_values
|
attribute_values = attribute_filter.item_attribute_values
|
||||||
|
|
||||||
attribute_values = [d.attribute_value for d in attributes]
|
|
||||||
|
|
||||||
self.assertEqual(attribute_filter.name, "Test Size")
|
self.assertEqual(attribute_filter.name, "Test Size")
|
||||||
self.assertGreater(len(attribute_values), 0)
|
self.assertGreater(len(attribute_values), 0)
|
||||||
@@ -349,4 +347,4 @@ def create_variant_web_item():
|
|||||||
variant.save()
|
variant.save()
|
||||||
|
|
||||||
if not frappe.db.exists("Website Item", {"variant_of": "Test Web Item"}):
|
if not frappe.db.exists("Website Item", {"variant_of": "Test Web Item"}):
|
||||||
make_website_item(variant, save=True)
|
make_website_item(variant, save=True)
|
||||||
|
|||||||
@@ -339,15 +339,15 @@
|
|||||||
<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="{{ attr_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 }}"
|
||||||
style="width: 14px !important"
|
style="width: 14px !important"
|
||||||
{% 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 %}
|
||||||
|
|||||||
Reference in New Issue
Block a user