def get_list(doctype, fields=None, filters=None, order_by=None, limit_start=None, limit_page_length=20, parent=None, debug=False, as_dict=True): '''Returns a list of records by filters, fields, ordering and limit :param doctype: DocType of the data to be queried :param fields: fields to be returned. Default is `name` :param filters: filter list by this dict :param order_by: Order by this fieldname :param limit_start: Start at this index :param limit_page_length: Number of records to be returned (default 20)''' if frappe.is_table(doctype): check_parent_permission(parent, doctype) args = frappe._dict( doctype=doctype, fields=fields, filters=filters, order_by=order_by, limit_start=limit_start, limit_page_length=limit_page_length, debug=debug, as_list=not as_dict ) validate_args(args) return frappe.get_list(**args)
def get_value(doctype, fieldname, filters=None, as_dict=True, debug=False, parent=None): """Returns a value form a document :param doctype: DocType to be queried :param fieldname: Field to be returned (default `name`) :param filters: dict or string for identifying the record""" if frappe.is_table(doctype): check_parent_permission(parent, doctype) if not frappe.has_permission(doctype): frappe.throw( _("No permission for {0}").format(doctype), frappe.PermissionError) filters = get_safe_filters(filters) if isinstance(filters, string_types): filters = {"name": filters} try: fields = frappe.parse_json(fieldname) except (TypeError, ValueError): # name passed, not json fields = [fieldname] # check whether the used filters were really parseable and usable # and did not just result in an empty string or dict if not filters: filters = None if frappe.get_meta(doctype).issingle: value = frappe.db.get_values_from_single(fields, filters, doctype, as_dict=as_dict, debug=debug) else: value = get_list( doctype, filters=filters, fields=fields, debug=debug, limit_page_length=1, parent=parent, as_dict=as_dict, ) if as_dict: return value[0] if value else {} if not value: return return value[0] if len(fields) > 1 else value[0][0]
def get(doctype, name=None, filters=None, parent=None): '''Returns a document by name or filters :param doctype: DocType of the document to be returned :param name: return document of this `name` :param filters: If name is not set, filter by these values and return the first match''' if frappe.is_table(doctype): check_parent_permission(parent, doctype) if filters and not name: name = frappe.db.get_value(doctype, json.loads(filters)) if not name: frappe.throw(_("No document found for given filters")) doc = frappe.get_doc(doctype, name) if not doc.has_permission("read"): raise frappe.PermissionError return frappe.get_doc(doctype, name).as_dict()