瀏覽代碼

Listview get_count fix (#4740)

version-14
Faris Ansari 7 年之前
committed by Nabin Hait
父節點
當前提交
909234ab9d
共有 3 個文件被更改,包括 9 次插入38 次删除
  1. +3
    -1
      frappe/desk/reportview.py
  2. +2
    -34
      frappe/model/db_query.py
  3. +4
    -3
      frappe/public/js/frappe/list/list_renderer.js

+ 3
- 1
frappe/desk/reportview.py 查看文件

@@ -50,11 +50,13 @@ def get_form_params():
for field in fields:
key = field.split(" as ")[0]

if key.startswith('count('): continue

if "." in key:
parenttype, fieldname = key.split(".")[0][4:-1], key.split(".")[1].strip("`")
else:
parenttype = data.doctype
fieldname = fieldname.strip("`")
fieldname = field.strip("`")

df = frappe.get_meta(parenttype).get_field(fieldname)



+ 2
- 34
frappe/model/db_query.py 查看文件

@@ -185,8 +185,8 @@ class DatabaseQuery(object):
# add tables from fields
if self.fields:
for f in self.fields:
if ( not ("tab" in f and "." in f) ) or ("locate(" in f): continue
if ( not ("tab" in f and "." in f) ) or ("locate(" in f) or ("count(" in f):
continue

table_name = f.split('.')[0]
if table_name.lower().startswith('group_concat('):
@@ -571,38 +571,6 @@ def get_list(doctype, *args, **kwargs):
kwargs.pop('cmd', None)
return DatabaseQuery(doctype).execute(None, *args, **kwargs)

@frappe.whitelist()
def get_count(doctype, filters=None):
if filters:
filters = json.loads(filters)

if is_parent_only_filter(doctype, filters):
if isinstance(filters, list):
filters = frappe.utils.make_filter_dict(filters)

return frappe.db.count(doctype, filters=filters)

else:
# If filters contain child table as well as parent doctype - Join
tables, conditions = ['`tab{0}`'.format(doctype)], []
for f in filters:
fieldname = '`tab{0}`.{1}'.format(f[0], f[1])
table = '`tab{0}`'.format(f[0])

if table not in tables:
tables.append(table)

conditions.append('{fieldname} {operator} "{value}"'.format(fieldname=fieldname,
operator=f[2], value=f[3]))

if doctype != f[0]:
join_condition = '`tab{child_doctype}`.parent =`tab{doctype}`.name'.format(child_doctype=f[0], doctype=doctype)
if join_condition not in conditions:
conditions.append(join_condition)

return frappe.db.sql_list("""select count(*) from {0}
where {1}""".format(','.join(tables), ' and '.join(conditions)), debug=0)

def is_parent_only_filter(doctype, filters):
#check if filters contains only parent doctype
only_parent_doctype = True


+ 4
- 3
frappe/public/js/frappe/list/list_renderer.js 查看文件

@@ -364,13 +364,14 @@ frappe.views.ListRenderer = Class.extend({
const current_count = this.list_view.data.length;

frappe.call({
method: 'frappe.model.db_query.get_count',
method: 'frappe.desk.reportview.get',
args: {
doctype: this.doctype,
filters: this.list_view.get_filters_args()
filters: this.list_view.get_filters_args(),
fields: ['count(`tab' + this.doctype + '`.name) as total_count']
}
}).then(r => {
const count = r.message || current_count;
const count = r.message.values[0][0] || current_count;
const str = __('{0} of {1}', [current_count, count]);
const $html = $(`<span>${str}</span>`);



Loading…
取消
儲存