Skip to content

Commit

Permalink
Merge pull request frappe#13 from ssiyad/refactor/rm_get_attribute_codes
Browse files Browse the repository at this point in the history
refactor: `get_item_codes_by_attributes ` (used in erpnext)
  • Loading branch information
ssiyad authored Jan 4, 2023
2 parents 1d60c6b + 49e9ff7 commit 37f8cbc
Showing 1 changed file with 0 additions and 63 deletions.
63 changes: 0 additions & 63 deletions webshop/webshop/variant_selector/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,69 +12,6 @@
from webshop.shopping_cart.cart import get_party


def get_item_codes_by_attributes(attribute_filters, template_item_code=None):
items = []

for attribute, values in attribute_filters.items():
attribute_values = values

if not isinstance(attribute_values, list):
attribute_values = [attribute_values]

if not attribute_values:
continue

wheres = []
query_values = []
for attribute_value in attribute_values:
wheres.append("( attribute = %s and attribute_value = %s )")
query_values += [attribute, attribute_value]

attribute_query = " or ".join(wheres)

if template_item_code:
variant_of_query = "AND t2.variant_of = %s"
query_values.append(template_item_code)
else:
variant_of_query = ""

query = """
SELECT
t1.parent
FROM
`tabItem Variant Attribute` t1
WHERE
1 = 1
AND (
{attribute_query}
)
AND EXISTS (
SELECT
1
FROM
`tabItem` t2
WHERE
t2.name = t1.parent
{variant_of_query}
)
GROUP BY
t1.parent
ORDER BY
NULL
""".format(
attribute_query=attribute_query, variant_of_query=variant_of_query
)

item_codes = set(
[r[0] for r in frappe.db.sql(query, query_values)]
) # nosemgrep
items.append(item_codes)

res = list(set.intersection(*items))

return res


@frappe.whitelist(allow_guest=True)
def get_attributes_and_values(item_code):
"""Build a list of attributes and their possible values.
Expand Down

0 comments on commit 37f8cbc

Please sign in to comment.