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.
 
 
 
 
 
 

112 lines
4.0 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, json
  24. from webnotes import _
  25. from webnotes.utils import cstr
  26. from webnotes.model import default_fields
  27. def get_mapped_doclist(from_doctype, from_docname, table_maps, target_doclist=[], postprocess=None):
  28. if isinstance(target_doclist, basestring):
  29. target_doclist = json.loads(target_doclist)
  30. source = webnotes.bean(from_doctype, from_docname)
  31. if not webnotes.has_permission(from_doctype, doc=source.doc):
  32. webnotes.msgprint("No Permission", raise_exception=webnotes.PermissionError)
  33. source_meta = webnotes.get_doctype(from_doctype)
  34. target_meta = webnotes.get_doctype(table_maps[from_doctype]["doctype"])
  35. # main
  36. if target_doclist:
  37. target_doc = webnotes.doc(target_doclist[0])
  38. else:
  39. target_doc = webnotes.new_doc(table_maps[from_doctype]["doctype"])
  40. map_doc(source.doc, target_doc, table_maps[source.doc.doctype], source_meta, target_meta)
  41. doclist = [target_doc]
  42. # children
  43. for source_d in source.doclist[1:]:
  44. table_map = table_maps.get(source_d.doctype)
  45. if table_map:
  46. if "condition" in table_map:
  47. if not table_map["condition"](source_d):
  48. continue
  49. target_doctype = table_map["doctype"]
  50. parentfield = target_meta.get({
  51. "parent": target_doc.doctype,
  52. "doctype": "DocField",
  53. "fieldtype": "Table",
  54. "options": target_doctype
  55. })[0].fieldname
  56. target_d = webnotes.new_doc(target_doctype, target_doc, parentfield)
  57. map_doc(source_d, target_d, table_map, source_meta, target_meta, source.doclist[0])
  58. doclist.append(target_d)
  59. doclist = webnotes.doclist(doclist)
  60. if postprocess:
  61. postprocess(source, doclist)
  62. return doclist
  63. def map_doc(source_doc, target_doc, table_map, source_meta, target_meta, source_parent=None):
  64. no_copy_fields = set(\
  65. [d.fieldname for d in source_meta.get({"no_copy": 1,
  66. "parent": source_doc.doctype})] \
  67. + [d.fieldname for d in target_meta.get({"no_copy": 1,
  68. "parent": target_doc.doctype})] \
  69. + default_fields)
  70. if table_map.get("validation"):
  71. for key, condition in table_map["validation"].items():
  72. if condition[0]=="=":
  73. if source_doc.fields.get(key) != condition[1]:
  74. webnotes.msgprint(_("Cannot map because following condition fails: ")
  75. + key + "=" + cstr(condition[1]), raise_exception=webnotes.ValidationError)
  76. # map same fields
  77. target_fields = target_meta.get({"doctype": "DocField", "parent": target_doc.doctype})
  78. for key in [d.fieldname for d in target_fields]:
  79. if key not in no_copy_fields:
  80. val = source_doc.fields.get(key)
  81. if val not in (None, ""):
  82. target_doc.fields[key] = val
  83. # map other fields
  84. for source_key, target_key in table_map.get("field_map", {}).items():
  85. val = source_doc.fields.get(source_key)
  86. if val not in (None, ""):
  87. target_doc.fields[target_key] = val
  88. # map idx
  89. if source_doc.idx:
  90. target_doc.idx = source_doc.idx
  91. if "postprocess" in table_map:
  92. table_map["postprocess"](source_doc, target_doc, source_parent)