コード例 #1
0
    def test_product_list_ordering_and_paging(self):
        "Test if website items appear by ranking on different pages."
        engine = ProductQuery()
        result = engine.query(attributes={},
                              fields={},
                              search_term=None,
                              start=0,
                              item_group=None)
        items = result.get("items")

        self.assertIsNotNone(items)
        self.assertEqual(len(items), 4)
        self.assertGreater(result.get("items_count"), 4)

        # check if items appear as per ranking set in setUpClass
        self.assertEqual(items[0].get("item_code"), "Test 17I Laptop")
        self.assertEqual(items[1].get("item_code"), "Test 16I Laptop")
        self.assertEqual(items[2].get("item_code"), "Test 15I Laptop")
        self.assertEqual(items[3].get("item_code"), "Test 14I Laptop")

        # check next page
        result = engine.query(attributes={},
                              fields={},
                              search_term=None,
                              start=4,
                              item_group=None)
        items = result.get("items")

        # check if items appear as per ranking set in setUpClass on next page
        self.assertEqual(items[0].get("item_code"), "Test 13I Laptop")
        self.assertEqual(items[1].get("item_code"), "Test 12I Laptop")
        self.assertEqual(items[2].get("item_code"), "Test 11I Laptop")
コード例 #2
0
    def test_product_list_with_discount_filters(self):
        "Test if discount filters are applied correctly."
        from erpnext.e_commerce.doctype.website_item.test_website_item import (
            make_web_item_price,
            make_web_pricing_rule,
        )

        field_filters = {"discount": [10]}

        make_web_item_price(item_code="Test 12I Laptop")
        make_web_pricing_rule(
            title="Test Pricing Rule for Test 12I Laptop",  # 10% discount
            item_code="Test 12I Laptop",
            selling=1)
        make_web_item_price(item_code="Test 13I Laptop")
        make_web_pricing_rule(
            title="Test Pricing Rule for Test 13I Laptop",  # 15% discount
            item_code="Test 13I Laptop",
            discount_percentage=15,
            selling=1)

        setup_e_commerce_settings({"show_price": 1})
        frappe.local.shopping_cart_settings = None

        engine = ProductQuery()
        result = engine.query(attributes={},
                              fields=field_filters,
                              search_term=None,
                              start=0,
                              item_group=None)
        items = result.get("items")

        # check if only product with 10% and below discount are fetched
        self.assertEqual(len(items), 1)
        self.assertEqual(items[0].get("item_code"), "Test 12I Laptop")
コード例 #3
0
    def test_product_list_with_variants(self):
        "Test if variants are hideen on hiding variants in settings."
        create_variant_web_item()

        setup_e_commerce_settings({
            "enable_attribute_filters": 0,
            "hide_variants": 1
        })
        frappe.local.shopping_cart_settings = None

        attribute_filters = {"Test Size": ["Large"]}
        engine = ProductQuery()
        result = engine.query(attributes=attribute_filters,
                              fields={},
                              search_term=None,
                              start=0,
                              item_group=None)
        items = result.get("items")

        # check if any variants are fetched even though published variant exists
        self.assertEqual(len(items), 0)

        # tear down
        setup_e_commerce_settings({
            "enable_attribute_filters": 1,
            "hide_variants": 0
        })
コード例 #4
0
    def test_product_list_discount_filter_builder(self):
        "Test if discount filters are fetched correctly."
        from erpnext.e_commerce.doctype.website_item.test_website_item import (
            make_web_item_price,
            make_web_pricing_rule,
        )

        item_code = "Test 12I Laptop"
        make_web_item_price(item_code=item_code)
        make_web_pricing_rule(title=f"Test Pricing Rule for {item_code}",
                              item_code=item_code,
                              selling=1)

        setup_e_commerce_settings({"show_price": 1})
        frappe.local.shopping_cart_settings = None

        engine = ProductQuery()
        result = engine.query(attributes={},
                              fields={},
                              search_term=None,
                              start=4,
                              item_group=None)
        self.assertTrue(bool(result.get("discounts")))

        filter_engine = ProductFiltersBuilder()
        discount_filters = filter_engine.get_discount_filters(
            result["discounts"])

        self.assertEqual(len(discount_filters[0]), 2)
        self.assertEqual(discount_filters[0][0], 10)
        self.assertEqual(discount_filters[0][1], "10% and below")
コード例 #5
0
    def test_change_product_ranking(self):
        "Test if item on second page appear on first if ranking is changed."
        item_code = "Test 12I Laptop"
        old_ranking = frappe.db.get_value("Website Item",
                                          {"item_code": item_code}, "ranking")

        # low rank, appears on second page
        self.assertEqual(old_ranking, 2)

        # set ranking as highest rank
        frappe.db.set_value("Website Item", {"item_code": item_code},
                            "ranking", 10)

        engine = ProductQuery()
        result = engine.query(attributes={},
                              fields={},
                              search_term=None,
                              start=0,
                              item_group=None)
        items = result.get("items")

        # check if item is the first item on the first page
        self.assertEqual(items[0].get("item_code"), item_code)
        self.assertEqual(items[1].get("item_code"), "Test 17I Laptop")

        # tear down
        frappe.db.set_value("Website Item", {"item_code": item_code},
                            "ranking", old_ranking)
コード例 #6
0
    def test_product_list_with_attribute_filter(self):
        "Test if attribute filters are applied correctly."
        create_variant_web_item()

        attribute_filters = {"Test Size": ["Large"]}
        engine = ProductQuery()
        result = engine.query(attributes=attribute_filters,
                              fields={},
                              search_term=None,
                              start=0,
                              item_group=None)
        items = result.get("items")

        # check if only items with Test Size 'Large' are fetched
        self.assertEqual(len(items), 1)
        self.assertEqual(items[0].get("item_code"), "Test Web Item-L")
コード例 #7
0
    def test_product_list_with_field_filter(self):
        "Test if field filters are applied correctly."
        field_filters = {"item_group": "Raw Material"}

        engine = ProductQuery()
        result = engine.query(attributes={},
                              fields=field_filters,
                              search_term=None,
                              start=0,
                              item_group=None)
        items = result.get("items")

        # check if only 'Raw Material' are fetched in the right order
        self.assertEqual(len(items), 3)
        self.assertEqual(items[0].get("item_code"), "Test 16I Laptop")
        self.assertEqual(items[1].get("item_code"), "Test 15I Laptop")
コード例 #8
0
    def test_custom_field_as_filter(self):
        "Test if custom field functions as filter correctly."
        from frappe.custom.doctype.custom_field.custom_field import create_custom_field

        create_custom_field(
            "Website Item",
            dict(
                owner="Administrator",
                fieldname="supplier",
                label="Supplier",
                fieldtype="Link",
                options="Supplier",
                insert_after="on_backorder",
            ),
        )

        frappe.db.set_value("Website Item", {"item_code": "Test 11I Laptop"},
                            "supplier", "_Test Supplier")
        frappe.db.set_value("Website Item", {"item_code": "Test 12I Laptop"},
                            "supplier", "_Test Supplier 1")

        settings = frappe.get_doc("E Commerce Settings")
        settings.append("filter_fields", {"fieldname": "supplier"})
        settings.save()

        filter_engine = ProductFiltersBuilder()
        field_filters = filter_engine.get_field_filters()
        custom_filter = field_filters[1]
        filter_values = custom_filter[1]

        self.assertEqual(custom_filter[0].options, "Supplier")
        self.assertEqual(len(filter_values), 2)
        self.assertIn("_Test Supplier", filter_values)

        # test if custom filter works in query
        field_filters = {"supplier": "_Test Supplier 1"}
        engine = ProductQuery()
        result = engine.query(attributes={},
                              fields=field_filters,
                              search_term=None,
                              start=0,
                              item_group=None)
        items = result.get("items")

        # check if only 'Raw Material' are fetched in the right order
        self.assertEqual(len(items), 1)
        self.assertEqual(items[0].get("item_code"), "Test 12I Laptop")
コード例 #9
0
ファイル: api.py プロジェクト: MorezMartin/erpnext
def get_product_filter_data(query_args=None):
	"""
		Returns filtered products and discount filters.
		:param query_args (dict): contains filters to get products list

		Query Args filters:
		search (str): Search Term.
		field_filters (dict): Keys include item_group, brand, etc.
		attribute_filters(dict): Keys include Color, Size, etc.
		start (int): Offset items by
		item_group (str): Valid Item Group
		from_filters (bool): Set as True to jump to page 1
	"""
	if isinstance(query_args, str):
		query_args = json.loads(query_args)

	query_args = frappe._dict(query_args)
	if query_args:
		search = query_args.get("search")
		field_filters = query_args.get("field_filters", {})
		attribute_filters = query_args.get("attribute_filters", {})
		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")
	else:
		search, attribute_filters, item_group, from_filters = None, None, None, None
		field_filters = {}
		start = 0

	# if new filter is checked, reset start to show filtered items from page 1
	if from_filters:
		start = 0

	sub_categories = []
	if item_group:
		field_filters['item_group'] = item_group
		sub_categories = get_child_groups_for_website(item_group, immediate=True)

	engine = ProductQuery()
	try:
		result = engine.query(
			attribute_filters,
			field_filters,
			search_term=search,
			start=start,
			item_group=item_group
		)
	except Exception:
		traceback = frappe.get_traceback()
		frappe.log_error(traceback, frappe._("Product Engine Error"))
		return {"exc": "Something went wrong!"}

	# discount filter data
	filters = {}
	discounts = result["discounts"]

	if discounts:
		filter_engine = ProductFiltersBuilder()
		filters["discount_filters"] = filter_engine.get_discount_filters(discounts)

	return {
		"items": result["items"] or [],
		"filters": filters,
		"settings": engine.settings,
		"sub_categories": sub_categories,
		"items_count": result["items_count"]
	}