Skip to content

Commit

Permalink
fix: sort order functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohan Bansal committed Aug 6, 2024
1 parent ac53a55 commit f3ab5fb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 55 deletions.
12 changes: 1 addition & 11 deletions inventory_tools/inventory_tools/faceted_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@
from inventory_tools.inventory_tools.faceted_search_query import FacetedSearchQuery


SORT_ORDER_LOOKUP = {
"Title A-Z": "item_name ASC, ranking DESC",
"Title Z-A": "item_name DESC, ranking DESC",
"Item Code A-Z": "item_code ASC, ranking DESC",
"Item Code Z-A": "item_code DESC, ranking DESC",
}


@frappe.whitelist(allow_guest=True)
def show_faceted_search_components(doctype="Item", filters=None):
attributes = frappe.get_all(
Expand Down Expand Up @@ -94,9 +86,7 @@ def get_product_filter_data(query_args=None):
search = query_args.get("search")
field_filters = query_args.get("field_filters", {})
attribute_filters = query_args.get("attributes", {})
sort_order = (
query_args.get("sort_order").get("sort_order") if query_args.get("sort_order") else ""
)
sort_order = query_args.get("sort_order") if query_args.get("sort_order") else ""
start = cint(query_args.start) if query_args.get("start") else 0
item_group = query_args.get("item_group")
from_filters = query_args.get("from_filters")
Expand Down
20 changes: 12 additions & 8 deletions inventory_tools/inventory_tools/faceted_search_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,36 @@
# For license information, please see license.txt

import json
from time import localtime

import frappe
from frappe.utils.data import flt, getdate, fmt_money

from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item
from erpnext.stock.get_item_details import get_conversion_factor
from webshop.webshop.api import *
from webshop.webshop.doctype.webshop_settings.webshop_settings import (
get_shopping_cart_settings,
show_quantity_in_website,
)
from webshop.webshop.shopping_cart.cart import _get_cart_quotation, _set_price_list
from webshop.webshop.doctype.override_doctype.item_group import get_item_for_list_in_html
from webshop.webshop.doctype.item_review.item_review import get_customer
from webshop.webshop.product_data_engine.query import ProductQuery
from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item
from webshop.webshop.shopping_cart.cart import _get_cart_quotation, _set_price_list, get_party
from webshop.webshop.utils.product import (
get_non_stock_item_status,
adjust_qty_for_expired_items,
)
from frappe.utils.data import cint, flt, getdate, fmt_money

from inventory_tools.inventory_tools.doctype.specification.specification import (
convert_to_epoch,
)

SORT_ORDER_LOOKUP = {
"Title A-Z": "item_name ASC, ranking DESC",
"Title Z-A": "item_name DESC, ranking DESC",
"Item Code A-Z": "item_code ASC, ranking DESC",
"Item Code Z-A": "item_code DESC, ranking DESC",
}


class FacetedSearchQuery(ProductQuery):
def query(
Expand All @@ -50,7 +56,7 @@ def query(
if self.settings.hide_variants:
self.filters.append(["variant_of", "is", "not set"])

sort_order = sort_order_lookup.get(sort_order) if sort_order else "item_name ASC"
sort_order = SORT_ORDER_LOOKUP.get(sort_order) if sort_order else "item_name ASC"

# query results
if attributes:
Expand Down Expand Up @@ -332,8 +338,6 @@ def get_product_list(search=None, start=0, limit=12):


def _get_price(item_code, price_list, customer_group, company, qty=1, uom=None):
from erpnext.e_commerce.shopping_cart.cart import get_party

template_item_code = frappe.db.get_value("Item", item_code, "variant_of")

if price_list:
Expand Down
72 changes: 36 additions & 36 deletions inventory_tools/tests/test_faceted_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,40 @@ def test_values_updated_on_item_save():
values = frappe.get_all(
"Specification Value", {"reference_doctype": "Item", "reference_name": "Double Plum Pie"}
)
# assert values == []
assert values == []
doc = frappe.get_doc("Item", "Double Plum Pie")
doc.save()
values = frappe.get_all(
"Specification Value", {"reference_doctype": "Item", "reference_name": "Double Plum Pie"}
)
# assert len(values) == 4
assert len(values) == 4
doc.weight_per_unit = 12
doc.save()
values = frappe.get_all(
"Specification Value",
{"reference_doctype": "Item", "reference_name": "Double Plum Pie"},
)
# assert len(values) == 4
assert len(values) == 4
new_weight = frappe.get_all(
"Specification Value",
{"reference_doctype": "Item", "reference_name": "Double Plum Pie", "attribute": "Weight"},
"value",
)
# assert len(new_weight) == 1
# assert int(new_weight[0].value) == 12
assert len(new_weight) == 1
assert int(new_weight[0].value) == 12
doc.weight_per_unit = 8
doc.save()
new_weight = frappe.get_all(
"Specification Value",
{"reference_doctype": "Item", "reference_name": "Double Plum Pie", "attribute": "Weight"},
"value",
)
# assert len(new_weight) == 1
# assert int(new_weight[0].value) == 8
assert len(new_weight) == 1
assert int(new_weight[0].value) == 8
values = frappe.get_all(
"Specification Value", {"reference_doctype": "Item", "reference_name": "Double Plum Pie"}
)
# assert len(values) == 4
assert len(values) == 4
# cleanup
for value in values:
frappe.delete_doc("Specification Value", value.name, force=True)
Expand All @@ -69,7 +69,7 @@ def test_values_updated_on_item_save():
def test_generate_values():
frappe.flags.in_test = True
doc = frappe.get_doc("Specification", "Items")
# assert len(doc.attributes) == 3
assert len(doc.attributes) == 3
frappe.call(
"inventory_tools.inventory_tools.doctype.specification.specification.create_specification_values",
**{
Expand All @@ -83,17 +83,17 @@ def test_generate_values():
],
}
)
# assert (
# len(frappe.get_all("Specification Value", {"specification": doc.name})) == 36 * 2
# ) # total items x computed attributes
assert (
len(frappe.get_all("Specification Value", {"specification": doc.name})) == 36 * 2
) # total items x computed attributes


@pytest.mark.order(72)
def test_generate_values_on_overlapping_items():
frappe.flags.in_test = True
doc = frappe.get_doc("Specification", "Baked Goods")
# assert len(doc.attributes) == 4
# assert len(frappe.get_all("Specification Value", {"specification": doc.name})) == 0
assert len(doc.attributes) == 4
assert len(frappe.get_all("Specification Value", {"specification": doc.name})) == 0
frappe.call(
"inventory_tools.inventory_tools.doctype.specification.specification.create_specification_values",
**{
Expand All @@ -107,9 +107,9 @@ def test_generate_values_on_overlapping_items():
],
}
)
# assert (
# len(frappe.get_all("Specification Value", {"specification": doc.name})) == 6 * 2
# ) # total items x computed attributes
assert (
len(frappe.get_all("Specification Value", {"specification": doc.name})) == 6 * 2
) # total items x computed attributes


@pytest.mark.order(73)
Expand Down Expand Up @@ -148,10 +148,10 @@ def test_manual_attribute_addition():
"inventory_tools.inventory_tools.doctype.specification.specification.update_specification_values",
**args
)
# assert (
# len(frappe.get_all("Specification Value", {"specification": "Items", "attribute": "Color"}))
# == 25
# ) # all colors in baked goods items from fixtures
assert (
len(frappe.get_all("Specification Value", {"specification": "Items", "attribute": "Color"}))
== 25
) # all colors in baked goods items from fixtures


@pytest.mark.order(74)
Expand All @@ -167,8 +167,8 @@ def test_delete_of_specification_value():
**_args
)
colors = [v.value for v in values if v.attribute == "Color"]
# assert "Blue" in colors
# assert len(colors) == 2
assert "Blue" in colors
assert len(colors) == 2

for v in values:
if v.attribute == "Color" and v.value == "Blue":
Expand All @@ -184,16 +184,16 @@ def test_delete_of_specification_value():
"inventory_tools.inventory_tools.doctype.specification.specification.update_specification_values",
**args
)
# assert (
# len(
# frappe.get_all(
# "Specification Value",
# {
# "specification": "Items",
# "attribute": "Color",
# "reference_name": _args.get("reference_name"),
# },
# )
# )
# == 1
# ) # all colors in baked goods items from fixtures
assert (
len(
frappe.get_all(
"Specification Value",
{
"specification": "Items",
"attribute": "Color",
"reference_name": _args.get("reference_name"),
},
)
)
== 1
) # all colors in baked goods items from fixtures

0 comments on commit f3ab5fb

Please sign in to comment.