Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes, json
  5. from webnotes import _
  6. from webnotes.utils import cstr
  7. from webnotes.model import default_fields
  8. def get_mapped_doclist(from_doctype, from_docname, table_maps, target_doclist=None,
  9. postprocess=None, ignore_permissions=False):
  10. if target_doclist is None:
  11. target_doclist = []
  12. if isinstance(target_doclist, basestring):
  13. target_doclist = json.loads(target_doclist)
  14. source = webnotes.bean(from_doctype, from_docname)
  15. if not ignore_permissions and not webnotes.has_permission(from_doctype, "read", source.doc):
  16. webnotes.msgprint("No Permission", raise_exception=webnotes.PermissionError)
  17. source_meta = webnotes.get_doctype(from_doctype)
  18. target_meta = webnotes.get_doctype(table_maps[from_doctype]["doctype"])
  19. # main
  20. if target_doclist:
  21. if isinstance(target_doclist[0], dict):
  22. target_doc = webnotes.doc(fielddata=target_doclist[0])
  23. else:
  24. target_doc = target_doclist[0]
  25. else:
  26. target_doc = webnotes.new_doc(table_maps[from_doctype]["doctype"])
  27. map_doc(source.doc, target_doc, table_maps[source.doc.doctype], source_meta, target_meta)
  28. if target_doclist:
  29. target_doclist[0] = target_doc
  30. else:
  31. target_doclist = [target_doc]
  32. target_doclist = webnotes.doclist(target_doclist)
  33. # children
  34. for source_d in source.doclist[1:]:
  35. table_map = table_maps.get(source_d.doctype)
  36. if table_map:
  37. if "condition" in table_map:
  38. if not table_map["condition"](source_d):
  39. continue
  40. target_doctype = table_map["doctype"]
  41. parentfield = target_meta.get({
  42. "parent": target_doc.doctype,
  43. "doctype": "DocField",
  44. "fieldtype": "Table",
  45. "options": target_doctype
  46. })[0].fieldname
  47. if table_map.get("add_if_empty") and row_exists_in_target(parentfield, target_doclist):
  48. continue
  49. target_d = webnotes.new_doc(target_doctype, target_doc, parentfield)
  50. map_doc(source_d, target_d, table_map, source_meta, target_meta, source.doclist[0])
  51. target_d.idx = None
  52. target_doclist.append(target_d)
  53. target_doclist = webnotes.doclist(target_doclist)
  54. if postprocess:
  55. new_target_doclist = postprocess(source, target_doclist)
  56. if new_target_doclist:
  57. target_doclist = new_target_doclist
  58. return target_doclist
  59. def map_doc(source_doc, target_doc, table_map, source_meta, target_meta, source_parent=None):
  60. no_copy_fields = set(\
  61. [d.fieldname for d in source_meta.get({"no_copy": 1,
  62. "parent": source_doc.doctype})] \
  63. + [d.fieldname for d in target_meta.get({"no_copy": 1,
  64. "parent": target_doc.doctype})] \
  65. + default_fields)
  66. if table_map.get("validation"):
  67. for key, condition in table_map["validation"].items():
  68. if condition[0]=="=":
  69. if source_doc.fields.get(key) != condition[1]:
  70. webnotes.msgprint(_("Cannot map because following condition fails: ")
  71. + key + "=" + cstr(condition[1]), raise_exception=webnotes.ValidationError)
  72. # map same fields
  73. target_fields = target_meta.get({"doctype": "DocField", "parent": target_doc.doctype})
  74. for key in [d.fieldname for d in target_fields]:
  75. if key not in no_copy_fields:
  76. val = source_doc.fields.get(key)
  77. if val not in (None, ""):
  78. target_doc.fields[key] = val
  79. # map other fields
  80. field_map = table_map.get("field_map")
  81. if field_map:
  82. if isinstance(field_map, dict):
  83. for source_key, target_key in field_map.items():
  84. val = source_doc.fields.get(source_key)
  85. if val not in (None, ""):
  86. target_doc.fields[target_key] = val
  87. else:
  88. for fmap in field_map:
  89. val = source_doc.fields.get(fmap[0])
  90. if val not in (None, ""):
  91. target_doc.fields[fmap[1]] = val
  92. # map idx
  93. if source_doc.idx:
  94. target_doc.idx = source_doc.idx
  95. if "postprocess" in table_map:
  96. table_map["postprocess"](source_doc, target_doc, source_parent)
  97. row_exists_for_parentfield = {}
  98. def row_exists_in_target(parentfield, target_doclist):
  99. global row_exists_for_parentfield
  100. if parentfield not in row_exists_for_parentfield:
  101. row_exists_for_parentfield[parentfield] = True if \
  102. webnotes.doclist(target_doclist).get({"parentfield": parentfield}) else False
  103. return row_exists_for_parentfield[parentfield]