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.
 
 
 
 
 
 

138 lines
3.8 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. """build query for doclistview and return results"""
  23. import webnotes, json
  24. @webnotes.whitelist()
  25. def get(arg=None):
  26. """
  27. build query
  28. gets doctype, subject, filters
  29. limit_start, limit_page_length
  30. """
  31. data = webnotes.form_dict
  32. filters = json.loads(data['filters'])
  33. fields = json.loads(data['fields'])
  34. tables = ['`tab' + data['doctype'] + '`']
  35. docstatus = json.loads(data['docstatus'])
  36. if docstatus:
  37. conditions = [tables[0] + '.docstatus in (' + ','.join(docstatus) + ')']
  38. else:
  39. conditions = [tables[0] + '.docstatus < 2']
  40. # add table explict to field
  41. joined = [tables[0]]
  42. # make conditions from filters
  43. for f in filters:
  44. tname = ('`tab' + f[0] + '`')
  45. if not tname in tables:
  46. tables.append(tname)
  47. # prepare in condition
  48. if f[2]=='in':
  49. opts = ["'" + t.strip().replace("'", "\'") + "'" for t in f[3].split(',')]
  50. f[3] = "(" + ', '.join(opts) + ")"
  51. else:
  52. f[3] = "'" + f[3].replace("'", "\'") + "'"
  53. conditions.append(tname + '.' + f[1] + " " + f[2] + " " + f[3])
  54. if not tname in joined:
  55. conditions.append(tname + '.parent = ' + tables[0] + '.name')
  56. joined.append(tname)
  57. data['tables'] = ', '.join(tables)
  58. data['conditions'] = ' and '.join(conditions)
  59. data['fields'] = ', '.join(fields)
  60. if not data.get('order_by'):
  61. data['order_by'] = tables[0] + '.modified desc'
  62. query = """select %(fields)s from %(tables)s where %(conditions)s
  63. order by %(order_by)s
  64. limit %(limit_start)s, %(limit_page_length)s""" % data
  65. return webnotes.conn.sql(query, as_dict=1)
  66. @webnotes.whitelist()
  67. def delete_items():
  68. """delete selected items"""
  69. import json
  70. from webnotes.model import delete_doc
  71. from webnotes.model.code import get_obj
  72. il = json.loads(webnotes.form_dict.get('items'))
  73. doctype = webnotes.form_dict.get('doctype')
  74. for d in il:
  75. dt_obj = get_obj(doctype, d)
  76. if hasattr(dt_obj, 'on_trash'):
  77. dt_obj.on_trash()
  78. delete_doc(doctype, d)
  79. @webnotes.whitelist()
  80. def get_stats():
  81. """get tag info"""
  82. import json
  83. tags = json.loads(webnotes.form_dict.get('stats'))
  84. doctype = webnotes.form_dict['doctype']
  85. stats = {}
  86. for tag in tags:
  87. tagcount = webnotes.conn.sql("""select %(tag)s, count(*)
  88. from `tab%(doctype)s`
  89. where ifnull(%(tag)s, '')!=''
  90. group by %(tag)s;""" % locals(), as_list=1)
  91. if tag=='_user_tags':
  92. stats[tag] = scrub_user_tags(tagcount)
  93. else:
  94. stats[tag] = tagcount
  95. return stats
  96. def scrub_user_tags(tagcount):
  97. """rebuild tag list for tags"""
  98. rdict = {}
  99. tagdict = dict(tagcount)
  100. for t in tagdict:
  101. alltags = t.split(',')
  102. for tag in alltags:
  103. if tag:
  104. if not tag in rdict:
  105. rdict[tag] = 0
  106. rdict[tag] += tagdict[t]
  107. rlist = []
  108. for tag in rdict:
  109. rlist.append([tag, rdict[tag]])
  110. return rlist