Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

94 рядки
2.8 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes
  5. import json
  6. import copy
  7. @webnotes.whitelist()
  8. def get_data(doctypes, last_modified):
  9. data_map = {}
  10. for dump_report_map in webnotes.get_hooks().dump_report_map:
  11. data_map.update(webnotes.get_attr(dump_report_map))
  12. import datetime
  13. out = {}
  14. doctypes = json.loads(doctypes)
  15. last_modified = json.loads(last_modified)
  16. start = datetime.datetime.now()
  17. for d in doctypes:
  18. args = copy.deepcopy(data_map[d])
  19. dt = d.find("[") != -1 and d[:d.find("[")] or d
  20. out[dt] = {}
  21. if args.get("from"):
  22. modified_table = "item."
  23. else:
  24. modified_table = ""
  25. conditions = order_by = ""
  26. table = args.get("from") or ("`tab%s`" % dt)
  27. if d in last_modified:
  28. if not args.get("conditions"):
  29. args['conditions'] = []
  30. args['conditions'].append(modified_table + "modified > '" + last_modified[d] + "'")
  31. out[dt]["modified_names"] = webnotes.conn.sql_list("""select %sname from %s
  32. where %smodified > %s""" % (modified_table, table, modified_table, "%s"), last_modified[d])
  33. if args.get("force_index"):
  34. conditions = " force index (%s) " % args["force_index"]
  35. if args.get("conditions"):
  36. conditions += " where " + " and ".join(args["conditions"])
  37. if args.get("order_by"):
  38. order_by = " order by " + args["order_by"]
  39. out[dt]["data"] = [list(t) for t in webnotes.conn.sql("""select %s from %s %s %s""" \
  40. % (",".join(args["columns"]), table, conditions, order_by))]
  41. # last modified
  42. modified_table = table
  43. if "," in table:
  44. modified_table = " ".join(table.split(",")[0].split(" ")[:-1])
  45. tmp = webnotes.conn.sql("""select `modified`
  46. from %s order by modified desc limit 1""" % modified_table)
  47. out[dt]["last_modified"] = tmp and tmp[0][0] or ""
  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. # only compress full dumps (not partial)
  54. if out[d].get("links") and (d not in last_modified):
  55. for link_key in out[d]["links"]:
  56. link = out[d]["links"][link_key]
  57. if link[0] in out and (link[0] not in last_modified):
  58. # make a map of link ids
  59. # to index
  60. link_map = {}
  61. doctype_data = out[link[0]]
  62. col_idx = doctype_data["columns"].index(link[1])
  63. for row_idx in xrange(len(doctype_data["data"])):
  64. row = doctype_data["data"][row_idx]
  65. link_map[row[col_idx]] = row_idx
  66. for row in out[d]["data"]:
  67. col_idx = out[d]["columns"].index(link_key)
  68. # replace by id
  69. if row[col_idx]:
  70. row[col_idx] = link_map.get(row[col_idx])
  71. else:
  72. unused_links.append(link_key)
  73. for link in unused_links:
  74. del out[d]["links"][link]
  75. return out