feat: add docstrings
This commit is contained in:
parent
293f483cb4
commit
419c889a9a
@ -1,4 +1,4 @@
|
|||||||
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
|
||||||
# License: GNU General Public License v3. See license.txt
|
# License: GNU General Public License v3. See license.txt
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
@ -10,13 +10,17 @@ class ProductQuery:
|
|||||||
Attributes:
|
Attributes:
|
||||||
cart_settings (Document): Settings for Cart
|
cart_settings (Document): Settings for Cart
|
||||||
fields (list): Fields to fetch in query
|
fields (list): Fields to fetch in query
|
||||||
filters (list)
|
filters (TYPE): Description
|
||||||
or_filters (list)
|
or_filters (list): Description
|
||||||
page_length (Int): Length of page for the query
|
page_length (Int): Length of page for the query
|
||||||
settings (Document): Products Settings DocType
|
settings (Document): Products Settings DocType
|
||||||
|
filters (list)
|
||||||
|
or_filters (list)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
"""Summary
|
||||||
|
"""
|
||||||
self.settings = frappe.get_doc("Products Settings")
|
self.settings = frappe.get_doc("Products Settings")
|
||||||
self.cart_settings = frappe.get_doc("Shopping Cart Settings")
|
self.cart_settings = frappe.get_doc("Shopping Cart Settings")
|
||||||
self.page_length = self.settings.products_per_page or 20
|
self.page_length = self.settings.products_per_page or 20
|
||||||
@ -25,6 +29,17 @@ class ProductQuery:
|
|||||||
self.or_filters = []
|
self.or_filters = []
|
||||||
|
|
||||||
def query(self, attributes=None, fields=None, search_term=None, start=0):
|
def query(self, attributes=None, fields=None, search_term=None, start=0):
|
||||||
|
"""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
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list: List of results with set fields
|
||||||
|
"""
|
||||||
if fields: self.build_fields_filters(fields)
|
if fields: self.build_fields_filters(fields)
|
||||||
if search_term: self.build_search_filters(search_term)
|
if search_term: self.build_search_filters(search_term)
|
||||||
|
|
||||||
@ -49,6 +64,7 @@ class ProductQuery:
|
|||||||
)
|
)
|
||||||
|
|
||||||
items_dict = {item.name: item for item in items}
|
items_dict = {item.name: item for item in items}
|
||||||
|
# TODO: Replace Variants by their parent templates
|
||||||
|
|
||||||
all_items.append(set(items.keys()))
|
all_items.append(set(items.keys()))
|
||||||
|
|
||||||
@ -59,16 +75,28 @@ class ProductQuery:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
def build_fields_filters(self, filters):
|
def build_fields_filters(self, filters):
|
||||||
|
"""Build filters for field values
|
||||||
|
|
||||||
|
Args:
|
||||||
|
filters (dict): Filters
|
||||||
|
"""
|
||||||
for field, values in filters.items():
|
for field, values in filters.items():
|
||||||
if not values:
|
if not values:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if isinstance(values, list):
|
if isinstance(values, list):
|
||||||
|
# If value is a list use `IN` query
|
||||||
self.filters.append([field, 'IN', values])
|
self.filters.append([field, 'IN', values])
|
||||||
else:
|
else:
|
||||||
|
# `=` will be faster than `IN` for most cases
|
||||||
self.filters.append([field, '=', values])
|
self.filters.append([field, '=', values])
|
||||||
|
|
||||||
def build_search_filters(self, search_term):
|
def build_search_filters(self, search_term):
|
||||||
|
"""Query search term in specified fields
|
||||||
|
|
||||||
|
Args:
|
||||||
|
search_term (str): Search candidate
|
||||||
|
"""
|
||||||
# Default fields to search from
|
# Default fields to search from
|
||||||
default_fields = {'name', 'item_name', 'description', 'item_group'}
|
default_fields = {'name', 'item_name', 'description', 'item_group'}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user