25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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