From ce666c05bec2a3a91b4be3ce2665f5ff067747d2 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Tue, 9 Jul 2013 11:58:10 +0530 Subject: [PATCH] [notifications] added match permissions #552 --- webnotes/widgets/moduleview.py | 3 ++- webnotes/widgets/notification.py | 8 ++++--- webnotes/widgets/reportview.py | 36 ++++++++++++++++---------------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/webnotes/widgets/moduleview.py b/webnotes/widgets/moduleview.py index 543d7d9dbd..d7de21259a 100644 --- a/webnotes/widgets/moduleview.py +++ b/webnotes/widgets/moduleview.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import webnotes, json +from webnotes.widgets import reportview @webnotes.whitelist() def get_data(module, doctypes='[]'): @@ -20,7 +21,7 @@ def get_count(doctypes): def get_doctype_count_from_table(doctype): try: - count = webnotes.conn.sql("""select count(*) from `tab%s`""" % doctype)[0][0] + count = reportview.execute(doctype, fields=["count(*)"], as_list=True)[0][0] except Exception, e: if e.args[0]==1146: count = None diff --git a/webnotes/widgets/notification.py b/webnotes/widgets/notification.py index 7608cec0b2..78c5f2887c 100644 --- a/webnotes/widgets/notification.py +++ b/webnotes/widgets/notification.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import webnotes +from webnotes.widgets import reportview @webnotes.whitelist() def get(): @@ -19,11 +20,12 @@ def get(): if d in can_read: condition = for_doctype[d] key = condition.keys()[0] - query = "select count(*) from `tab%s` where `%s`=%s and docstatus<2" % (d, key, '%s') - result = webnotes.conn.sql(query, condition[key])[0][0] + + result = reportview.execute(d, fields=["count(*)"], + filters=[[d, key, "=", condition[key]]], as_list=True)[0][0] if result: open_count_doctype[d] = result - + for m in for_module: open_count_module[m] = for_module[m]() diff --git a/webnotes/widgets/reportview.py b/webnotes/widgets/reportview.py index 8b35e0d6b0..8dac4a9962 100644 --- a/webnotes/widgets/reportview.py +++ b/webnotes/widgets/reportview.py @@ -174,22 +174,25 @@ def build_filter_conditions(filters, conditions): from webnotes.utils import cstr for f in filters: - tname = ('`tab' + f[0] + '`') - if not tname in tables: - tables.append(tname) - - # prepare in condition - if f[2]=='in': - opts = ["'" + t.strip().replace("'", "\\'") + "'" for t in f[3].split(',')] - f[3] = "(" + ', '.join(opts) + ")" - conditions.append(tname + '.' + f[1] + " " + f[2] + " " + f[3]) + if isinstance(f, basestring): + conditions.append(f) else: - if isinstance(f[3], basestring): - f[3] = "'" + f[3].replace("'", "\\'") + "'" + tname = ('`tab' + f[0] + '`') + if not tname in tables: + tables.append(tname) + + # prepare in condition + if f[2]=='in': + opts = ["'" + t.strip().replace("'", "\\'") + "'" for t in f[3].split(',')] + f[3] = "(" + ', '.join(opts) + ")" conditions.append(tname + '.' + f[1] + " " + f[2] + " " + f[3]) else: - conditions.append('ifnull(' + tname + '.' + f[1] + ",0) " + f[2] \ - + " " + cstr(f[3])) + if isinstance(f[3], basestring): + f[3] = "'" + f[3].replace("'", "\\'") + "'" + conditions.append(tname + '.' + f[1] + " " + f[2] + " " + f[3]) + else: + conditions.append('ifnull(' + tname + '.' + f[1] + ",0) " + f[2] \ + + " " + cstr(f[3])) def build_match_conditions(doctype, fields=None): """add match conditions if applicable""" @@ -235,7 +238,6 @@ def get_tables(doctype, fields): table_name = f.split('.')[0] if table_name.lower().startswith('group_concat('): table_name = table_name[13:] - # check if ifnull function is used if table_name.lower().startswith('ifnull('): table_name = table_name[7:] if not table_name[0]=='`': @@ -347,10 +349,8 @@ def get_stats(stats, doctype): columns = get_table_columns(doctype) for tag in tags: if not tag in columns: continue - tagcount = webnotes.conn.sql("""select %(tag)s, count(*) - from `tab%(doctype)s` - where ifnull(%(tag)s, '')!='' - group by %(tag)s;""" % locals(), as_list=1) + tagcount = execute(doctype, fields=[tag, "count(*)"], + filters=["ifnull(%s,'')!=''" % tag], group_by=tag, as_list=True) if tag=='_user_tags': stats[tag] = scrub_user_tags(tagcount)