def run_standard_report(self, filters, limit, user): params = json.loads(self.json) columns = self.get_standard_report_columns(params) result = [] order_by, group_by, group_by_args = self.get_standard_report_order_by(params) _result = frappe.get_list(self.ref_doctype, fields = [ get_group_by_field(group_by_args, c[1]) if c[0] == '_aggregate_column' and group_by_args else Report._format([c[1], c[0]]) for c in columns ], filters = self.get_standard_report_filters(params, filters), order_by = order_by, group_by = group_by, as_list = True, limit = limit, user = user) columns = self.build_standard_report_columns(columns, group_by_args) result = result + [list(d) for d in _result] if params.get('add_totals_row'): result = append_totals_row(result) return columns, result
def get_data(self, filters=None, limit=None, user=None, as_dict=False): columns = [] out = [] if self.report_type in ('Query Report', 'Script Report', 'Custom Report'): # query and script reports data = frappe.desk.query_report.run(self.name, filters=filters, user=user) for d in data.get('columns'): if isinstance(d, dict): col = frappe._dict(d) if not col.fieldname: col.fieldname = col.label columns.append(col) else: fieldtype, options = "Data", None parts = d.split(':') if len(parts) > 1: if parts[1]: fieldtype, options = parts[1], None if fieldtype and '/' in fieldtype: fieldtype, options = fieldtype.split('/') columns.append( frappe._dict(label=parts[0], fieldtype=fieldtype, fieldname=parts[0], options=options)) out += data.get('result') else: # standard report params = json.loads(self.json) if params.get('fields'): columns = params.get('fields') elif params.get('columns'): columns = params.get('columns') elif params.get('fields'): columns = params.get('fields') else: columns = [['name', self.ref_doctype]] for df in frappe.get_meta(self.ref_doctype).fields: if df.in_list_view: columns.append([df.fieldname, self.ref_doctype]) _filters = params.get('filters') or [] if filters: for key, value in iteritems(filters): condition, _value = '=', value if isinstance(value, (list, tuple)): condition, _value = value _filters.append([key, condition, _value]) def _format(parts): # sort by is saved as DocType.fieldname, covert it to sql return '`tab{0}`.`{1}`'.format(*parts) if params.get('sort_by'): order_by = _format(params.get('sort_by').split( '.')) + ' ' + params.get('sort_order') elif params.get('order_by'): order_by = params.get('order_by') else: order_by = _format([self.ref_doctype, 'modified']) + ' desc' if params.get('sort_by_next'): order_by += ', ' + _format( params.get('sort_by_next').split('.')) + ' ' + params.get( 'sort_order_next') result = frappe.get_list( self.ref_doctype, fields=[_format([c[1], c[0]]) for c in columns], filters=_filters, order_by=order_by, as_list=True, limit=limit, user=user) _columns = [] for (fieldname, doctype) in columns: meta = frappe.get_meta(doctype) if meta.get_field(fieldname): field = meta.get_field(fieldname) else: field = frappe._dict(fieldname=fieldname, label=meta.get_label(fieldname)) # since name is the primary key for a document, it will always be a Link datatype if fieldname == "name": field.fieldtype = "Link" field.options = doctype _columns.append(field) columns = _columns out = out + [list(d) for d in result] if params.get('add_totals_row'): out = append_totals_row(out) if as_dict: data = [] for row in out: if isinstance(row, (list, tuple)): _row = frappe._dict() for i, val in enumerate(row): _row[columns[i].get('fieldname')] = val elif isinstance(row, dict): # no need to convert from dict to dict _row = frappe._dict(row) data.append(_row) else: data = out return columns, data