Skip to content

Commit

Permalink
fix: Validate field filter wrt to Website Item & re-use validation in…
Browse files Browse the repository at this point in the history
… Item Group
  • Loading branch information
marination committed Apr 18, 2022
1 parent bed9e09 commit 34437a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def onload(self):
self.is_redisearch_loaded = is_search_module_loaded()

def validate(self):
self.validate_field_filters()
self.validate_field_filters(self.filter_fields, self.enable_field_filters)
self.validate_attribute_filters()
self.validate_checkout()
self.validate_search_index_fields()
Expand All @@ -50,21 +50,22 @@ def create_redisearch_indexes(self):
define_autocomplete_dictionary()
create_website_items_index()

def validate_field_filters(self):
if not (self.enable_field_filters and self.filter_fields):
@staticmethod
def validate_field_filters(filter_fields, enable_field_filters):
if not (enable_field_filters and filter_fields):
return

item_meta = frappe.get_meta("Website Item")
web_item_meta = frappe.get_meta("Website Item")
valid_fields = [
df.fieldname for df in item_meta.fields if df.fieldtype in ["Link", "Table MultiSelect"]
df.fieldname for df in web_item_meta.fields if df.fieldtype in ["Link", "Table MultiSelect"]
]

for f in self.filter_fields:
if f.fieldname not in valid_fields:
for row in filter_fields:
if row.fieldname not in valid_fields:
frappe.throw(
_(
"Filter Fields Row #{0}: Fieldname <b>{1}</b> must be of type 'Link' or 'Table MultiSelect'"
).format(f.idx, f.fieldname)
"Filter Fields Row #{0}: Fieldname {1} must be of type 'Link' or 'Table MultiSelect'"
).format(row.idx, frappe.bold(row.fieldname))
)

def validate_attribute_filters(self):
Expand Down
2 changes: 2 additions & 0 deletions erpnext/setup/doctype/item_group/item_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from frappe.website.website_generator import WebsiteGenerator
from six.moves.urllib.parse import quote

from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import ECommerceSettings
from erpnext.e_commerce.product_data_engine.filters import ProductFiltersBuilder


Expand All @@ -36,6 +37,7 @@ def validate(self):

self.make_route()
self.validate_item_group_defaults()
ECommerceSettings.validate_field_filters(self.filter_fields, enable_field_filters=True)

def on_update(self):
NestedSet.on_update(self)
Expand Down

0 comments on commit 34437a8

Please sign in to comment.