25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

104 lines
3.8 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. if not webnotes.has_permission(from_doctype, from_docname):
  31. webnotes.msgprint("No Permission", raise_exception=webnotes.PermissionError)
  32. source = webnotes.bean(from_doctype, from_docname)
  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. target_doctype = table_maps[source_d.doctype]["doctype"]
  45. parentfield = target_meta.get({
  46. "parent": target_doc.doctype,
  47. "doctype": "DocField",
  48. "fieldtype": "Table",
  49. "options": target_doctype
  50. })[0].fieldname
  51. target_d = webnotes.new_doc(target_doctype, target_doc, parentfield)
  52. map_doc(source_d, target_d, table_maps[source_d.doctype], source_meta, target_meta)
  53. doclist.append(target_d)
  54. if postprocess:
  55. postprocess(source, doclist)
  56. return doclist
  57. def map_doc(source_doc, target_doc, table_map, source_meta, target_meta):
  58. no_copy_fields = set(\
  59. [d.fieldname for d in source_meta.get({"no_copy": 1,
  60. "parent": source_doc.doctype})] \
  61. + [d.fieldname for d in target_meta.get({"no_copy": 1,
  62. "parent": target_doc.doctype})] \
  63. + default_fields)
  64. if table_map.get("validation"):
  65. for key, condition in table_map["validation"].items():
  66. if condition[0]=="=":
  67. if source_doc.fields.get(key) != condition[1]:
  68. webnotes.msgprint(_("Cannot map because following condition fails: ")
  69. + key + "=" + cstr(condition[1]), raise_exception=webnotes.ValidationError)
  70. # map same fields
  71. target_fields = target_meta.get({"doctype": "DocField", "parent": target_doc.doctype})
  72. for key in [d.fieldname for d in target_fields]:
  73. if key not in no_copy_fields:
  74. val = source_doc.fields.get(key)
  75. if val not in (None, ""):
  76. target_doc.fields[key] = val
  77. # map other fields
  78. for source_key, target_key in table_map.get("field_map", {}).items():
  79. val = source_doc.fields.get(source_key)
  80. if val not in (None, ""):
  81. target_doc.fields[target_key] = val
  82. # map idx
  83. if source_doc.idx:
  84. target_doc.idx = source_doc.idx
  85. if "postprocess" in table_map:
  86. table_map["postprocess"](source_doc, target_doc)