您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

109 行
3.7 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. import webnotes
  24. import json
  25. @webnotes.whitelist()
  26. def get_data(doctypes, last_modified):
  27. from startup.report_data_map import data_map
  28. import datetime
  29. out = {}
  30. doctypes = json.loads(doctypes)
  31. last_modified = json.loads(last_modified)
  32. start = datetime.datetime.now()
  33. for d in doctypes:
  34. args = data_map[d]
  35. dt = d.find("[") != -1 and d[:d.find("[")] or d
  36. out[dt] = {}
  37. if args.get("from"):
  38. modified_table = "item."
  39. else:
  40. modified_table = ""
  41. conditions = order_by = ""
  42. table = args.get("from") or ("`tab%s`" % dt)
  43. if d in last_modified:
  44. if not args.get("conditions"):
  45. args['conditions'] = []
  46. args['conditions'].append(modified_table + "modified > '" + last_modified[d] + "'")
  47. out[dt]["modified_names"] = webnotes.conn.sql_list("""select %sname from %s
  48. where %smodified > %s""" % (modified_table, table, modified_table, "%s"), last_modified[d])
  49. if args.get("force_index"):
  50. conditions = " force index (%s) " % args["force_index"]
  51. if args.get("conditions"):
  52. conditions += " where " + " and ".join(args["conditions"])
  53. if args.get("order_by"):
  54. order_by = " order by " + args["order_by"]
  55. out[dt]["data"] = [list(t) for t in webnotes.conn.sql("""select %s from %s %s %s""" \
  56. % (",".join(args["columns"]), table, conditions, order_by))]
  57. # last modified
  58. modified_table = table
  59. if "," in table:
  60. modified_table = " ".join(table.split(",")[0].split(" ")[:-1])
  61. tmp = webnotes.conn.sql("""select `modified`
  62. from %s order by modified desc limit 1""" % modified_table)
  63. out[dt]["last_modified"] = tmp and tmp[0][0] or ""
  64. out[dt]["columns"] = map(lambda c: c.split(" as ")[-1], args["columns"])
  65. if args.get("links"):
  66. out[dt]["links"] = args["links"]
  67. for d in out:
  68. unused_links = []
  69. # only compress full dumps (not partial)
  70. if out[d].get("links") and (not d in last_modified):
  71. for link_key in out[d]["links"]:
  72. link = out[d]["links"][link_key]
  73. if link[0] in out:
  74. # make a map of link ids
  75. # to index
  76. link_map = {}
  77. doctype_data = out[link[0]]
  78. col_idx = doctype_data["columns"].index(link[1])
  79. for row_idx in xrange(len(doctype_data["data"])):
  80. row = doctype_data["data"][row_idx]
  81. link_map[row[col_idx]] = row_idx
  82. for row in out[d]["data"]:
  83. col_idx = out[d]["columns"].index(link_key)
  84. # replace by id
  85. if row[col_idx]:
  86. row[col_idx] = link_map.get(row[col_idx])
  87. else:
  88. unused_links.append(link_key)
  89. for link in unused_links:
  90. del out[d]["links"][link]
  91. return out