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.
 
 
 
 
 
 

106 lines
3.5 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. if args.get("from"):
  37. modified_table = "item."
  38. else:
  39. modified_table = ""
  40. if d in last_modified:
  41. if not args.get("conditions"):
  42. args['conditions'] = []
  43. args['conditions'].append(modified_table + "modified > '" + last_modified[d] + "'")
  44. conditions = order_by = ""
  45. if args.get("force_index"):
  46. conditions = " force index (%s) " % args["force_index"]
  47. if args.get("conditions"):
  48. conditions += " where " + " and ".join(args["conditions"])
  49. if args.get("order_by"):
  50. order_by = " order by " + args["order_by"]
  51. table = args.get("from") or ("`tab%s`" % dt)
  52. out[dt] = {}
  53. out[dt]["data"] = [list(t) for t in webnotes.conn.sql("""select %s from %s %s %s""" \
  54. % (",".join(args["columns"]), table, conditions, order_by))]
  55. # last modified
  56. modified_table = table
  57. if "," in table:
  58. modified_table = " ".join(table.split(",")[0].split(" ")[:-1])
  59. tmp = webnotes.conn.sql("""select `modified`
  60. from %s order by modified desc limit 1""" % modified_table)
  61. out[dt]["last_modified"] = tmp and tmp[0][0] or ""
  62. out[dt]["columns"] = map(lambda c: c.split(" as ")[-1], args["columns"])
  63. if args.get("links"):
  64. out[dt]["links"] = args["links"]
  65. for d in out:
  66. unused_links = []
  67. # only compress full dumps (not partial)
  68. if out[d].get("links") and (not d in last_modified):
  69. for link_key in out[d]["links"]:
  70. link = out[d]["links"][link_key]
  71. if link[0] in out:
  72. # make a map of link ids
  73. # to index
  74. link_map = {}
  75. doctype_data = out[link[0]]
  76. col_idx = doctype_data["columns"].index(link[1])
  77. for row_idx in xrange(len(doctype_data["data"])):
  78. row = doctype_data["data"][row_idx]
  79. link_map[row[col_idx]] = row_idx
  80. for row in out[d]["data"]:
  81. col_idx = out[d]["columns"].index(link_key)
  82. # replace by id
  83. if row[col_idx]:
  84. row[col_idx] = link_map.get(row[col_idx])
  85. else:
  86. unused_links.append(link_key)
  87. for link in unused_links:
  88. del out[d]["links"][link]
  89. return out