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.
 
 
 
 
 
 

86 lines
2.9 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. from webnotes.utils import cstr
  29. import datetime
  30. doctypes = json.loads(webnotes.form_dict.get("doctypes"))
  31. out = {}
  32. start = datetime.datetime.now()
  33. for d in doctypes:
  34. args = data_map[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. out[d] = {}
  43. start = datetime.datetime.now()
  44. out[d]["data"] = [list(t) for t in webnotes.conn.sql("""select %s from `tab%s` %s %s""" % (",".join(args["columns"]),
  45. d, conditions, order_by))]
  46. out[d]["time"] = str(datetime.datetime.now() - start)
  47. out[d]["columns"] = map(lambda c: c.split(" as ")[-1], args["columns"])
  48. if args.get("links"):
  49. out[d]["links"] = args["links"]
  50. for d in out:
  51. if out[d].get("links"):
  52. for link_key in out[d]["links"]:
  53. link = out[d]["links"][link_key]
  54. if link[0] in out:
  55. # make a map of link ids
  56. # to index
  57. link_map = {}
  58. doctype_data = out[link[0]]
  59. col_idx = doctype_data["columns"].index(link[1])
  60. for row_idx in xrange(len(doctype_data["data"])):
  61. row = doctype_data["data"][row_idx]
  62. link_map[row[col_idx]] = row_idx
  63. for row in out[d]["data"]:
  64. col_idx = out[d]["columns"].index(link_key)
  65. # replace by id
  66. if row[col_idx]:
  67. row[col_idx] = link_map[row[col_idx]]
  68. missing = {}
  69. # don't send everything
  70. # send only missing!
  71. # (but we need to load all to make links)
  72. for d in out:
  73. if d in webnotes.form_dict.get("missing",[]):
  74. missing[d] = out[d]
  75. return missing