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.
 
 
 
 
 
 

94 lines
3.1 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():
  27. from startup.report_data_map import data_map
  28. import datetime
  29. doctypes = json.loads(webnotes.form_dict.get("doctypes"))
  30. out = {}
  31. start = datetime.datetime.now()
  32. for d in doctypes:
  33. args = data_map[d]
  34. dt = d.find("[") != -1 and d[:d.find("[")] or d
  35. conditions = order_by = ""
  36. if args.get("force_index"):
  37. conditions = " force index (%s) " % args["force_index"]
  38. if args.get("conditions"):
  39. conditions += " where " + " and ".join(args["conditions"])
  40. if args.get("order_by"):
  41. order_by = " order by " + args["order_by"]
  42. table = args.get("from") or ("`tab%s`" % dt)
  43. out[dt] = {}
  44. start = datetime.datetime.now()
  45. out[dt]["data"] = [list(t) for t in webnotes.conn.sql("""select %s from %s %s %s""" \
  46. % (",".join(args["columns"]), table, conditions, order_by))]
  47. out[dt]["time"] = str(datetime.datetime.now() - start)
  48. out[dt]["columns"] = map(lambda c: c.split(" as ")[-1], args["columns"])
  49. if args.get("links"):
  50. out[dt]["links"] = args["links"]
  51. for d in out:
  52. unused_links = []
  53. if out[d].get("links"):
  54. for link_key in out[d]["links"]:
  55. link = out[d]["links"][link_key]
  56. if link[0] in out:
  57. # make a map of link ids
  58. # to index
  59. link_map = {}
  60. doctype_data = out[link[0]]
  61. col_idx = doctype_data["columns"].index(link[1])
  62. for row_idx in xrange(len(doctype_data["data"])):
  63. row = doctype_data["data"][row_idx]
  64. link_map[row[col_idx]] = row_idx
  65. for row in out[d]["data"]:
  66. col_idx = out[d]["columns"].index(link_key)
  67. # replace by id
  68. if row[col_idx]:
  69. row[col_idx] = link_map.get(row[col_idx])
  70. else:
  71. unused_links.append(link_key)
  72. for link in unused_links:
  73. del out[d]["links"][link]
  74. missing = {}
  75. # don't send everything
  76. # send only missing!
  77. # (but we need to load all to make links)
  78. for d in out:
  79. if d in webnotes.form_dict.get("missing",[]):
  80. missing[d] = out[d]
  81. return missing