Merge pull request #13685 from surajshetty3416/fix-tag-filter
This commit is contained in:
commit
713fba3d62
2 changed files with 45 additions and 15 deletions
|
|
@ -1,8 +1,26 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2019, Frappe Technologies and Contributors
|
||||
# See license.txt
|
||||
# import frappe
|
||||
import unittest
|
||||
import frappe
|
||||
|
||||
from frappe.desk.reportview import get_stats
|
||||
from frappe.desk.doctype.tag.tag import add_tag
|
||||
|
||||
class TestTag(unittest.TestCase):
|
||||
pass
|
||||
def setUp(self) -> None:
|
||||
frappe.db.sql("DELETE from `tabTag`")
|
||||
frappe.db.sql("UPDATE `tabDocType` set _user_tags=''")
|
||||
|
||||
def test_tag_count_query(self):
|
||||
self.assertDictEqual(get_stats('["_user_tags"]', 'DocType'),
|
||||
{'_user_tags': [['No Tags', frappe.db.count('DocType')]]})
|
||||
add_tag('Standard', 'DocType', 'User')
|
||||
add_tag('Standard', 'DocType', 'ToDo')
|
||||
|
||||
# count with no filter
|
||||
self.assertDictEqual(get_stats('["_user_tags"]', 'DocType'),
|
||||
{'_user_tags': [['Standard', 2], ['No Tags', frappe.db.count('DocType') - 2]]})
|
||||
|
||||
# count with child table field filter
|
||||
self.assertDictEqual(get_stats('["_user_tags"]',
|
||||
'DocType',
|
||||
filters='[["DocField", "fieldname", "like", "%last_name%"], ["DocType", "name", "like", "%use%"]]'),
|
||||
{'_user_tags': [['Standard', 1], ['No Tags', 0]]})
|
||||
|
|
@ -445,24 +445,36 @@ def get_stats(stats, doctype, filters=[]):
|
|||
for tag in tags:
|
||||
if not tag in columns: continue
|
||||
try:
|
||||
tagcount = frappe.get_list(doctype, fields=[tag, "count(*)"],
|
||||
#filters=["ifnull(`%s`,'')!=''" % tag], group_by=tag, as_list=True)
|
||||
filters = filters + ["ifnull(`%s`,'')!=''" % tag], group_by = tag, as_list = True)
|
||||
tag_count = frappe.get_list(doctype,
|
||||
fields=[tag, "count(*)"],
|
||||
filters=filters + [[tag, '!=', '']],
|
||||
group_by=tag,
|
||||
as_list=True,
|
||||
distinct=1,
|
||||
)
|
||||
|
||||
if tag=='_user_tags':
|
||||
stats[tag] = scrub_user_tags(tagcount)
|
||||
stats[tag].append([_("No Tags"), frappe.get_list(doctype,
|
||||
if tag == '_user_tags':
|
||||
stats[tag] = scrub_user_tags(tag_count)
|
||||
no_tag_count = frappe.get_list(doctype,
|
||||
fields=[tag, "count(*)"],
|
||||
filters=filters +["({0} = ',' or {0} = '' or {0} is null)".format(tag)], as_list=True)[0][1]])
|
||||
filters=filters + [[tag, "in", ('', ',')]],
|
||||
as_list=True,
|
||||
group_by=tag,
|
||||
order_by=tag,
|
||||
)
|
||||
|
||||
no_tag_count = no_tag_count[0][1] if no_tag_count else 0
|
||||
|
||||
stats[tag].append([_("No Tags"), no_tag_count])
|
||||
else:
|
||||
stats[tag] = tagcount
|
||||
stats[tag] = tag_count
|
||||
|
||||
except frappe.db.SQLError:
|
||||
# does not work for child tables
|
||||
pass
|
||||
except frappe.db.InternalError:
|
||||
except frappe.db.InternalError as e:
|
||||
# raised when _user_tags column is added on the fly
|
||||
pass
|
||||
|
||||
return stats
|
||||
|
||||
@frappe.whitelist()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue