You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

384 line
10 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. """build query for doclistview and return results"""
  24. import webnotes, json
  25. import webnotes.defaults
  26. tables = None
  27. doctypes = {}
  28. roles = []
  29. @webnotes.whitelist()
  30. def get():
  31. return compress(execute(**get_form_params()))
  32. def get_form_params():
  33. data = webnotes._dict(webnotes.form_dict)
  34. del data["cmd"]
  35. if isinstance(data.get("filters"), basestring):
  36. data["filters"] = json.loads(data["filters"])
  37. if isinstance(data.get("fields"), basestring):
  38. data["fields"] = json.loads(data["fields"])
  39. if isinstance(data.get("docstatus"), basestring):
  40. data["docstatus"] = json.loads(data["docstatus"])
  41. return data
  42. def execute(doctype, query=None, filters=None, fields=None, docstatus=None,
  43. group_by=None, order_by=None, limit_start=0, limit_page_length=None,
  44. as_list=False, debug=False):
  45. if query:
  46. return run_custom_query(query)
  47. if not filters: filters = []
  48. if not docstatus: docstatus = []
  49. args = prepare_args(doctype, filters, fields, docstatus, group_by, order_by)
  50. args.limit = add_limit(limit_start, limit_page_length)
  51. query = """select %(fields)s from %(tables)s where %(conditions)s
  52. %(group_by)s order by %(order_by)s %(limit)s""" % args
  53. return webnotes.conn.sql(query, as_dict=not as_list, debug=debug)
  54. def prepare_args(doctype, filters, fields, docstatus, group_by, order_by):
  55. global tables
  56. tables = get_tables(doctype, fields)
  57. load_doctypes()
  58. remove_user_tags(doctype, fields)
  59. conditions = build_conditions(doctype, fields, filters, docstatus)
  60. args = webnotes._dict()
  61. # query dict
  62. args.tables = ', '.join(tables)
  63. args.conditions = ' and '.join(conditions)
  64. args.fields = ', '.join(fields)
  65. args.order_by = order_by or tables[0] + '.modified desc'
  66. args.group_by = group_by and (" group by " + group_by) or ""
  67. check_sort_by_table(args.order_by)
  68. return args
  69. def compress(data):
  70. """separate keys and values"""
  71. if not data: return data
  72. values = []
  73. keys = data[0].keys()
  74. for row in data:
  75. new_row = []
  76. for key in keys:
  77. new_row.append(row[key])
  78. values.append(new_row)
  79. return {
  80. "keys": keys,
  81. "values": values
  82. }
  83. def check_sort_by_table(sort_by):
  84. """check atleast 1 column selected from the sort by table """
  85. tbl = sort_by.split('.')[0]
  86. if tbl not in tables:
  87. if tbl.startswith('`'):
  88. tbl = tbl[4:-1]
  89. webnotes.msgprint("Please select atleast 1 column from '%s' to sort"\
  90. % tbl, raise_exception=1)
  91. def run_custom_query(query):
  92. """run custom query"""
  93. if '%(key)s' in query:
  94. query = query.replace('%(key)s', 'name')
  95. return webnotes.conn.sql(query, as_dict=1)
  96. def load_doctypes():
  97. """load all doctypes and roles"""
  98. global doctypes, roles
  99. import webnotes.model.doctype
  100. roles = webnotes.get_roles()
  101. for t in tables:
  102. if t.startswith('`'):
  103. doctype = t[4:-1]
  104. if not webnotes.has_permission(doctype):
  105. webnotes.response['403'] = 1
  106. raise webnotes.PermissionError, doctype
  107. doctypes[doctype] = webnotes.model.doctype.get(doctype)
  108. def remove_user_tags(doctype, fields):
  109. """remove column _user_tags if not in table"""
  110. for fld in fields:
  111. if '_user_tags' in fld:
  112. if not '_user_tags' in get_table_columns(doctype):
  113. del fields[fields.index(fld)]
  114. break
  115. def add_limit(limit_start, limit_page_length):
  116. if limit_page_length:
  117. return 'limit %s, %s' % (limit_start, limit_page_length)
  118. else:
  119. return ''
  120. def build_conditions(doctype, fields, filters, docstatus):
  121. """build conditions"""
  122. if docstatus:
  123. conditions = [tables[0] + '.docstatus in (' + ','.join(docstatus) + ')']
  124. else:
  125. # default condition
  126. conditions = [tables[0] + '.docstatus < 2']
  127. # make conditions from filters
  128. build_filter_conditions(filters, conditions)
  129. # join parent, child tables
  130. for tname in tables[1:]:
  131. conditions.append(tname + '.parent = ' + tables[0] + '.name')
  132. # match conditions
  133. match_conditions = build_match_conditions(doctype, fields)
  134. if match_conditions:
  135. conditions.append(match_conditions)
  136. return conditions
  137. def build_filter_conditions(filters, conditions):
  138. """build conditions from user filters"""
  139. from webnotes.utils import cstr
  140. for f in filters:
  141. tname = ('`tab' + f[0] + '`')
  142. if not tname in tables:
  143. tables.append(tname)
  144. # prepare in condition
  145. if f[2]=='in':
  146. opts = ["'" + t.strip().replace("'", "\\'") + "'" for t in f[3].split(',')]
  147. f[3] = "(" + ', '.join(opts) + ")"
  148. conditions.append(tname + '.' + f[1] + " " + f[2] + " " + f[3])
  149. else:
  150. if isinstance(f[3], basestring):
  151. f[3] = "'" + f[3].replace("'", "\\'") + "'"
  152. conditions.append(tname + '.' + f[1] + " " + f[2] + " " + f[3])
  153. else:
  154. conditions.append('ifnull(' + tname + '.' + f[1] + ",0) " + f[2] \
  155. + " " + cstr(f[3]))
  156. def build_match_conditions(doctype, fields=None):
  157. """add match conditions if applicable"""
  158. match_conditions = []
  159. match = True
  160. if not tables or not doctypes:
  161. global tables
  162. tables = get_tables(doctype, fields)
  163. load_doctypes()
  164. for d in doctypes[doctype]:
  165. if d.doctype == 'DocPerm' and d.parent == doctype:
  166. if d.role in roles:
  167. if d.match: # role applicable
  168. if ':' in d.match:
  169. document_key, default_key = d.match.split(":")
  170. else:
  171. default_key = document_key = d.match
  172. for v in webnotes.defaults.get_user_default_as_list(default_key) or ["** No Match **"]:
  173. match_conditions.append('`tab%s`.%s="%s"' % (doctype,
  174. document_key, v))
  175. elif d.read == 1 and d.permlevel == 0:
  176. # don't restrict if another read permission at level 0
  177. # exists without a match restriction
  178. match = False
  179. if match_conditions and match:
  180. return '('+ ' or '.join(match_conditions) +')'
  181. else:
  182. return ""
  183. def get_tables(doctype, fields):
  184. """extract tables from fields"""
  185. tables = ['`tab' + doctype + '`']
  186. # add tables from fields
  187. for f in fields or []:
  188. if "." not in f: continue
  189. table_name = f.split('.')[0]
  190. if table_name.lower().startswith('group_concat('):
  191. table_name = table_name[13:]
  192. # check if ifnull function is used
  193. if table_name.lower().startswith('ifnull('):
  194. table_name = table_name[7:]
  195. if not table_name[0]=='`':
  196. table_name = '`' + table_name + '`'
  197. if not table_name in tables:
  198. tables.append(table_name)
  199. return tables
  200. @webnotes.whitelist()
  201. def save_report():
  202. """save report"""
  203. from webnotes.model.doc import Document
  204. data = webnotes.form_dict
  205. if webnotes.conn.exists('Report', data['name']):
  206. d = Document('Report', data['name'])
  207. else:
  208. d = Document('Report')
  209. d.report_name = data['name']
  210. d.ref_doctype = data['doctype']
  211. d.report_type = "Report Builder"
  212. d.json = data['json']
  213. webnotes.bean([d]).save()
  214. webnotes.msgprint("%s saved." % d.name)
  215. return d.name
  216. @webnotes.whitelist()
  217. def export_query():
  218. """export from report builder"""
  219. # TODO: validate use is allowed to export
  220. verify_export_allowed()
  221. ret = execute(**get_form_params())
  222. columns = [x[0] for x in webnotes.conn.get_description()]
  223. data = [['Sr'] + get_labels(columns),]
  224. # flatten dict
  225. cnt = 1
  226. for row in ret:
  227. flat = [cnt,]
  228. for c in columns:
  229. flat.append(row.get(c))
  230. data.append(flat)
  231. cnt += 1
  232. # convert to csv
  233. from cStringIO import StringIO
  234. import csv
  235. f = StringIO()
  236. writer = csv.writer(f)
  237. from webnotes.utils import cstr
  238. for r in data:
  239. # encode only unicode type strings and not int, floats etc.
  240. writer.writerow(map(lambda v: isinstance(v, unicode) and v.encode('utf-8') or v, r))
  241. f.seek(0)
  242. webnotes.response['result'] = unicode(f.read(), 'utf-8')
  243. webnotes.response['type'] = 'csv'
  244. webnotes.response['doctype'] = [t[4:-1] for t in tables][0]
  245. def verify_export_allowed():
  246. """throw exception if user is not allowed to export"""
  247. global roles
  248. roles = webnotes.get_roles()
  249. if not ('Administrator' in roles or 'System Manager' in roles or 'Report Manager' in roles):
  250. raise webnotes.PermissionError
  251. def get_labels(columns):
  252. """get column labels based on column names"""
  253. label_dict = {}
  254. for doctype in doctypes:
  255. for d in doctypes[doctype]:
  256. if d.doctype=='DocField' and d.fieldname:
  257. label_dict[d.fieldname] = d.label
  258. return map(lambda x: label_dict.get(x, x.title()), columns)
  259. @webnotes.whitelist()
  260. def delete_items():
  261. """delete selected items"""
  262. import json
  263. from webnotes.model import delete_doc
  264. from webnotes.model.code import get_obj
  265. il = json.loads(webnotes.form_dict.get('items'))
  266. doctype = webnotes.form_dict.get('doctype')
  267. for d in il:
  268. try:
  269. dt_obj = get_obj(doctype, d)
  270. if hasattr(dt_obj, 'on_trash'):
  271. dt_obj.on_trash()
  272. delete_doc(doctype, d)
  273. except Exception, e:
  274. webnotes.errprint(webnotes.getTraceback())
  275. pass
  276. @webnotes.whitelist()
  277. def get_stats(stats, doctype):
  278. """get tag info"""
  279. import json
  280. tags = json.loads(stats)
  281. stats = {}
  282. columns = get_table_columns(doctype)
  283. for tag in tags:
  284. if not tag in columns: continue
  285. tagcount = webnotes.conn.sql("""select %(tag)s, count(*)
  286. from `tab%(doctype)s`
  287. where ifnull(%(tag)s, '')!=''
  288. group by %(tag)s;""" % locals(), as_list=1)
  289. if tag=='_user_tags':
  290. stats[tag] = scrub_user_tags(tagcount)
  291. else:
  292. stats[tag] = tagcount
  293. return stats
  294. def scrub_user_tags(tagcount):
  295. """rebuild tag list for tags"""
  296. rdict = {}
  297. tagdict = dict(tagcount)
  298. for t in tagdict:
  299. alltags = t.split(',')
  300. for tag in alltags:
  301. if tag:
  302. if not tag in rdict:
  303. rdict[tag] = 0
  304. rdict[tag] += tagdict[t]
  305. rlist = []
  306. for tag in rdict:
  307. rlist.append([tag, rdict[tag]])
  308. return rlist
  309. def get_table_columns(table):
  310. res = webnotes.conn.sql("DESC `tab%s`" % table, as_dict=1)
  311. if res: return [r['Field'] for r in res]