您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

132 行
4.1 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, os
  24. import webnotes.model.doc
  25. from webnotes.modules import scrub, get_module_path, lower_case_files_for, \
  26. code_fields_dict, scrub_dt_dn
  27. def export_doc(doc):
  28. export_to_files([[doc.doctype, doc.name]])
  29. def export_to_files(record_list=[], record_module=None, verbose=0):
  30. """
  31. Export record_list to files. record_list is a list of lists ([doctype],[docname] ) ,
  32. """
  33. from webnotes.modules.import_merge import in_transfer
  34. if in_transfer:
  35. return
  36. module_doclist =[]
  37. if record_list:
  38. for record in record_list:
  39. write_document_file(webnotes.model.doc.get(record[0], record[1]),
  40. record_module)
  41. def write_document_file(doclist, record_module=None):
  42. from webnotes.modules.utils import pprint_doclist
  43. doclist = [filter_fields(d.fields) for d in doclist]
  44. module = record_module or get_module_name(doclist)
  45. code_type = doclist[0]['doctype'] in lower_case_files_for
  46. # create folder
  47. folder = create_folder(module, doclist[0]['doctype'], doclist[0]['name'], code_type)
  48. # separate code files
  49. clear_code_fields(doclist, folder, code_type)
  50. # write the data file
  51. fname = (code_type and scrub(doclist[0]['name'])) or doclist[0]['name']
  52. with open(os.path.join(folder, fname +'.txt'),'w+') as txtfile:
  53. txtfile.write(pprint_doclist(doclist))
  54. def filter_fields(doc):
  55. from webnotes.model.doctype import get
  56. from webnotes.model import default_fields
  57. doctypelist = get(doc.doctype, False)
  58. valid_fields = [d.fieldname for d in doctypelist.get({"parent":doc.doctype,
  59. "doctype":"DocField"})]
  60. to_remove = []
  61. for key in doc:
  62. if (not key in default_fields) and (not key in valid_fields):
  63. to_remove.append(key)
  64. elif doc[key]==None:
  65. to_remove.append(key)
  66. for key in to_remove:
  67. del doc[key]
  68. return doc
  69. def get_module_name(doclist):
  70. if doclist[0]['doctype'] == 'Module Def':
  71. module = doclist[0]['name']
  72. elif doclist[0]['doctype']=='Control Panel':
  73. module = 'Core'
  74. elif doclist[0]['doctype']=="Workflow":
  75. module = webnotes.conn.get_value("DocType", doclist[0]["document_type"], "module")
  76. else:
  77. module = doclist[0]['module']
  78. return module
  79. def create_folder(module, dt, dn, code_type):
  80. # get module path by importing the module
  81. module_path = get_module_path(module)
  82. dt, dn = scrub_dt_dn(dt, dn)
  83. # create folder
  84. folder = os.path.join(module_path, dt, dn)
  85. webnotes.create_folder(folder)
  86. # create init_py_files
  87. if code_type:
  88. create_init_py(module_path, dt, dn)
  89. return folder
  90. def create_init_py(module_path, dt, dn):
  91. def create_if_not_exists(path):
  92. initpy = os.path.join(path, '__init__.py')
  93. if not os.path.exists(initpy):
  94. open(initpy, 'w').close()
  95. create_if_not_exists(os.path.join(module_path))
  96. create_if_not_exists(os.path.join(module_path, dt))
  97. create_if_not_exists(os.path.join(module_path, dt, dn))
  98. def clear_code_fields(doclist, folder, code_type):
  99. code_fields = code_fields_dict.get(doclist[0]['doctype'], [])
  100. for code_field in code_fields:
  101. if doclist[0].get(code_field[0]):
  102. doclist[0][code_field[0]] = None