From 6d85e821c1e8faf87445c8582ebaab6eb781ce90 Mon Sep 17 00:00:00 2001 From: marination Date: Thu, 8 Apr 2021 15:25:43 +0530 Subject: [PATCH] fix: Sider and Tests --- .../e_commerce_settings.py | 8 ++-- .../doctype/item_review/item_review.py | 3 -- .../doctype/website_item/website_item.py | 18 +++---- .../e_commerce/doctype/wishlist/wishlist.py | 2 +- erpnext/e_commerce/filters.py | 9 ++-- .../test_product_configurator.py | 6 +-- .../e_commerce/product_configurator/utils.py | 3 +- erpnext/e_commerce/product_query.py | 47 ++++++++++--------- .../make_homepage_products_website_items.py | 3 +- erpnext/stock/doctype/item/item.py | 1 - erpnext/templates/pages/customer_reviews.js | 9 ++-- erpnext/templates/pages/wishlist.py | 4 +- 12 files changed, 59 insertions(+), 54 deletions(-) diff --git a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.py b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.py index f8a6cb635b0..cb61de1feef 100644 --- a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.py +++ b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.py @@ -5,7 +5,7 @@ from __future__ import unicode_literals import frappe from frappe.utils import cint -from frappe import _ +from frappe import _, msgprint from frappe.model.document import Document from frappe.utils import get_datetime, get_datetime_str, now_datetime @@ -30,7 +30,8 @@ class ECommerceSettings(Document): frappe.clear_document_cache("E Commerce Settings", "E Commerce Settings") def validate_field_filters(self): - if not (self.enable_field_filters and self.filter_fields): return + if not (self.enable_field_filters and self.filter_fields): + return item_meta = frappe.get_meta("Item") valid_fields = [df.fieldname for df in item_meta.fields if df.fieldtype in ["Link", "Table MultiSelect"]] @@ -40,7 +41,8 @@ class ECommerceSettings(Document): frappe.throw(_("Filter Fields Row #{0}: Fieldname {1} must be of type 'Link' or 'Table MultiSelect'").format(f.idx, f.fieldname)) def validate_attribute_filters(self): - if not (self.enable_attribute_filters and self.filter_attributes): return + if not (self.enable_attribute_filters and self.filter_attributes): + return # if attribute filters are enabled, hide_variants should be disabled self.hide_variants = 0 diff --git a/erpnext/e_commerce/doctype/item_review/item_review.py b/erpnext/e_commerce/doctype/item_review/item_review.py index 84a1274d4ca..637194e6b85 100644 --- a/erpnext/e_commerce/doctype/item_review/item_review.py +++ b/erpnext/e_commerce/doctype/item_review/item_review.py @@ -4,9 +4,6 @@ from __future__ import unicode_literals from datetime import datetime -from six import string_types -import json - import frappe from frappe.model.document import Document from frappe.contacts.doctype.contact.contact import get_contact_name diff --git a/erpnext/e_commerce/doctype/website_item/website_item.py b/erpnext/e_commerce/doctype/website_item/website_item.py index fe7de09d4f4..181efdd37f7 100644 --- a/erpnext/e_commerce/doctype/website_item/website_item.py +++ b/erpnext/e_commerce/doctype/website_item/website_item.py @@ -48,6 +48,7 @@ class WebsiteItem(WebsiteGenerator): self.update_template_item() def on_trash(self): + super(WebsiteItem, self).on_trash() self.publish_unpublish_desk_item(publish=False) def validate_duplicate_website_item(self): @@ -169,8 +170,8 @@ class WebsiteItem(WebsiteGenerator): context.parents = get_parent_item_groups(self.item_group, from_item=True) self.attributes = frappe.get_all("Item Variant Attribute", - fields=["attribute", "attribute_value"], - filters={"parent": self.item_code}) + fields=["attribute", "attribute_value"], + filters={"parent": self.item_code}) self.set_variant_context(context) self.set_attribute_context(context) self.set_disabled_attributes(context) @@ -181,7 +182,7 @@ class WebsiteItem(WebsiteGenerator): context.wished = False if frappe.db.exists("Wishlist Items", {"item_code": self.item_code, "parent": frappe.session.user}): - context.wished = True + context.wished = True return context @@ -192,8 +193,8 @@ class WebsiteItem(WebsiteGenerator): # load variants # also used in set_attribute_context context.variants = frappe.get_all("Item", - filters={"variant_of": self.name, "show_variant_in_website": 1}, - order_by="name asc") + filters={"variant_of": self.name, "show_variant_in_website": 1}, + order_by="name asc") variant = frappe.form_dict.variant if not variant and context.variants: @@ -227,8 +228,8 @@ class WebsiteItem(WebsiteGenerator): # load attributes for v in context.variants: v.attributes = frappe.get_all("Item Variant Attribute", - fields=["attribute", "attribute_value"], - filters={"parent": v.name}) + fields=["attribute", "attribute_value"], + filters={"parent": v.name}) # make a map for easier access in templates v.attribute_map = frappe._dict({}) for attr in v.attributes: @@ -380,7 +381,8 @@ def invalidate_cache_for_web_item(doc): @frappe.whitelist() def make_website_item(doc, save=True): - if not doc: return + if not doc: + return if isinstance(doc, string_types): doc = json.loads(doc) diff --git a/erpnext/e_commerce/doctype/wishlist/wishlist.py b/erpnext/e_commerce/doctype/wishlist/wishlist.py index eb86dc8f918..c817657a503 100644 --- a/erpnext/e_commerce/doctype/wishlist/wishlist.py +++ b/erpnext/e_commerce/doctype/wishlist/wishlist.py @@ -53,7 +53,7 @@ def remove_from_wishlist(item_code): delete from `tabWishlist Items` where item_code=%(item_code)s - """%{"item_code": frappe.db.escape(item_code)}) + """ % {"item_code": frappe.db.escape(item_code)}) frappe.db.commit() diff --git a/erpnext/e_commerce/filters.py b/erpnext/e_commerce/filters.py index 997c7dae8d3..f22e7ff5bf0 100644 --- a/erpnext/e_commerce/filters.py +++ b/erpnext/e_commerce/filters.py @@ -15,7 +15,8 @@ class ProductFiltersBuilder: self.item_group = item_group def get_field_filters(self): - if not self.item_group and not self.doc.enable_field_filters: return + if not self.item_group and not self.doc.enable_field_filters: + return filter_fields = [row.fieldname for row in self.doc.filter_fields] @@ -46,7 +47,8 @@ class ProductFiltersBuilder: values = [d.name for d in frappe.get_all(doctype, filters)] # Remove None - if None in values: values.remove(None) + if None in values: + values.remove(None) if values: filter_data.append([df, values]) @@ -54,7 +56,8 @@ class ProductFiltersBuilder: return filter_data def get_attribute_filters(self): - if not self.item_group and not self.doc.enable_attribute_filters: return + if not self.item_group and not self.doc.enable_attribute_filters: + return attributes = [row.attribute for row in self.doc.filter_attributes] attribute_docs = [ diff --git a/erpnext/e_commerce/product_configurator/test_product_configurator.py b/erpnext/e_commerce/product_configurator/test_product_configurator.py index ee49e2040f8..0f619b5c67b 100644 --- a/erpnext/e_commerce/product_configurator/test_product_configurator.py +++ b/erpnext/e_commerce/product_configurator/test_product_configurator.py @@ -2,10 +2,8 @@ from __future__ import unicode_literals from bs4 import BeautifulSoup import frappe, unittest -from frappe.utils import set_request, get_html_for_route -from frappe.website.render import render +from frappe.utils import get_html_for_route from erpnext.e_commerce.product_query import ProductQuery -from erpnext.stock.doctype.item.test_item import make_item_variant from erpnext.e_commerce.doctype.website_item.website_item import make_website_item test_dependencies = ["Item"] @@ -84,7 +82,7 @@ class TestProductConfigurator(unittest.TestCase): def publish_items_on_website(self): if frappe.db.exists("Item", "_Test Item") and not frappe.db.exists("Website Item", {"item_code": "_Test Item"}): - make_website_item(frappe.get_cached_doc("Item", "_Test Item")) + make_website_item(frappe.get_cached_doc("Item", "_Test Item")) if frappe.db.exists("Item", "_Test Variant Item") and not frappe.db.exists("Website Item", {"item_code": "_Test Variant Item"}): make_website_item(frappe.get_cached_doc("Item", "_Test Variant Item")) diff --git a/erpnext/e_commerce/product_configurator/utils.py b/erpnext/e_commerce/product_configurator/utils.py index f4192b93714..ed8d2027960 100644 --- a/erpnext/e_commerce/product_configurator/utils.py +++ b/erpnext/e_commerce/product_configurator/utils.py @@ -11,7 +11,8 @@ def get_item_codes_by_attributes(attribute_filters, template_item_code=None): if not isinstance(attribute_values, list): attribute_values = [attribute_values] - if not attribute_values: continue + if not attribute_values: + continue wheres = [] query_values = [] diff --git a/erpnext/e_commerce/product_query.py b/erpnext/e_commerce/product_query.py index 5a9635bf6df..9fb3c3a6d70 100644 --- a/erpnext/e_commerce/product_query.py +++ b/erpnext/e_commerce/product_query.py @@ -8,11 +8,11 @@ class ProductQuery: """Query engine for product listing Attributes: - fields (list): Fields to fetch in query - conditions (string): Conditions for query building - or_conditions (string): Search conditions - page_length (Int): Length of page for the query - settings (Document): E Commerce Settings DocType + fields (list): Fields to fetch in query + conditions (string): Conditions for query building + or_conditions (string): Search conditions + page_length (Int): Length of page for the query + settings (Document): E Commerce Settings DocType """ def __init__(self): @@ -29,16 +29,18 @@ class ProductQuery: """Summary Args: - attributes (dict, optional): Item Attribute filters - fields (dict, optional): Field level filters - search_term (str, optional): Search term to lookup - start (int, optional): Page start + attributes (dict, optional): Item Attribute filters + fields (dict, optional): Field level filters + search_term (str, optional): Search term to lookup + start (int, optional): Page start Returns: - list: List of results with set fields + list: List of results with set fields """ - if fields: self.build_fields_filters(fields) - if search_term: self.build_search_filters(search_term) + if fields: + self.build_fields_filters(fields) + if search_term: + self.build_search_filters(search_term) if self.settings.hide_variants: self.conditions += " and wi.variant_of is null" @@ -61,13 +63,13 @@ class ProductQuery: if self.settings.show_stock_availability: if item.get("website_warehouse"): stock_qty = frappe.utils.flt( - frappe.db.get_value("Bin", - { - "item_code": item.item_code, - "warehouse": item.get("website_warehouse") - }, - "actual_qty") - ) + frappe.db.get_value("Bin", + { + "item_code": item.item_code, + "warehouse": item.get("website_warehouse") + }, + "actual_qty") + ) item.in_stock = "green" if stock_qty else "red" elif not frappe.db.get_value("Item", item.item_code, "is_stock_item"): item.in_stock = "green" # non-stock item will always be available @@ -106,7 +108,8 @@ class ProductQuery: self.conditions += " and iva.parent = wi.item_code" for attribute, values in attributes.items(): - if not isinstance(values, list): values = [values] + if not isinstance(values, list): + values = [values] conditions_copy = self.conditions substitutions_copy = self.substitutions.copy() @@ -129,7 +132,7 @@ class ProductQuery: """Build filters for field values Args: - filters (dict): Filters + filters (dict): Filters """ for field, values in filters.items(): if not values: @@ -147,7 +150,7 @@ class ProductQuery: """Query search term in specified fields Args: - search_term (str): Search candidate + search_term (str): Search candidate """ # Default fields to search from default_fields = {'name', 'item_name', 'description', 'item_group'} diff --git a/erpnext/patches/v13_0/make_homepage_products_website_items.py b/erpnext/patches/v13_0/make_homepage_products_website_items.py index 67cccdf0237..8b51cad6411 100644 --- a/erpnext/patches/v13_0/make_homepage_products_website_items.py +++ b/erpnext/patches/v13_0/make_homepage_products_website_items.py @@ -6,7 +6,8 @@ def execute(): for row in homepage.products: web_item = frappe.db.get_value("Website Item", {"item_code": row.item_code}, "name") - if not web_item: continue + if not web_item: + continue row.item_code = web_item diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index ab514c0bce3..1dd1bfa6cc2 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -343,7 +343,6 @@ class Item(Document): (self.item_name, self.description, self.brand, self.name)) def on_trash(self): - super(Item, self).on_trash() frappe.db.sql("""delete from tabBin where item_code=%s""", self.name) frappe.db.sql("delete from `tabItem Price` where item_code=%s", self.name) for variant_of in frappe.get_all("Item", filters={"variant_of": self.name}): diff --git a/erpnext/templates/pages/customer_reviews.js b/erpnext/templates/pages/customer_reviews.js index 453b96a168c..9be12c7bf30 100644 --- a/erpnext/templates/pages/customer_reviews.js +++ b/erpnext/templates/pages/customer_reviews.js @@ -26,7 +26,6 @@ $(() => { {fieldname: "comment", fieldtype: "Small Text", label: "Your Review"} ], primary_action: function() { - let me = this; let data = d.get_values(); frappe.call({ method: "erpnext.e_commerce.doctype.item_review.item_review.add_item_review", @@ -39,7 +38,7 @@ $(() => { freeze: true, freeze_message: __("Submitting Review ..."), callback: (r) => { - if(!r.exc) { + if (!r.exc) { frappe.msgprint({ message: __("Thank you for submitting your review"), title: __("Review Submitted"), @@ -74,7 +73,7 @@ $(() => { end: me.page_length }, callback: (result) => { - if(result.message) { + if (result.message) { let res = result.message; me.get_user_review_html(res.reviews); @@ -85,7 +84,7 @@ $(() => { } } - }) + }); }); } @@ -121,7 +120,7 @@ $(() => { get_review_stars(rating) { let stars = ``; - for(let i = 1; i < 6; i++) { + for (let i = 1; i < 6; i++) { let fill_class = i <= rating ? 'star-click' : ''; stars += ` diff --git a/erpnext/templates/pages/wishlist.py b/erpnext/templates/pages/wishlist.py index 0dbcf2188a8..96c83da443a 100644 --- a/erpnext/templates/pages/wishlist.py +++ b/erpnext/templates/pages/wishlist.py @@ -19,7 +19,7 @@ def get_context(context): "warehouse": item.get("warehouse") }, "actual_qty") - ) + ) item.available = True if stock_qty else False context.items = items @@ -34,5 +34,5 @@ def get_wishlist_items(): from `tabWishlist Items` where - parent=%(user)s"""%{"user": frappe.db.escape(frappe.session.user)}, as_dict=1) + parent=%(user)s""" % {"user": frappe.db.escape(frappe.session.user)}, as_dict=1) return \ No newline at end of file