Ви не можете вибрати більше 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. from webnotes.modules import scrub, get_module_path, scrub_dt_dn
  25. def import_files(module, dt=None, dn=None, force=False):
  26. if type(module) is list:
  27. out = []
  28. for m in module:
  29. out.append(import_file(m[0], m[1], m[2], force))
  30. return out
  31. else:
  32. return import_file(module, dt, dn, force)
  33. def import_file(module, dt, dn, force=False):
  34. """Sync a file from txt if modifed, return false if not updated"""
  35. webnotes.in_import = True
  36. dt, dn = scrub_dt_dn(dt, dn)
  37. path = os.path.join(get_module_path(module),
  38. os.path.join(dt, dn, dn + '.txt'))
  39. ret = import_file_by_path(path, force)
  40. webnotes.in_import = False
  41. return ret
  42. def import_file_by_path(path, force=False):
  43. if os.path.exists(path):
  44. from webnotes.modules.utils import peval_doclist
  45. with open(path, 'r') as f:
  46. doclist = peval_doclist(f.read())
  47. if doclist:
  48. doc = doclist[0]
  49. if not force:
  50. # check if timestamps match
  51. if doc['modified']== str(webnotes.conn.get_value(doc['doctype'], doc['name'], 'modified')):
  52. return False
  53. original_modified = doc["modified"]
  54. import_doclist(doclist)
  55. # since there is a new timestamp on the file, update timestamp in
  56. webnotes.conn.sql("update `tab%s` set modified=%s where name=%s" % \
  57. (doc['doctype'], '%s', '%s'),
  58. (original_modified, doc['name']))
  59. return True
  60. else:
  61. raise Exception, '%s missing' % path
  62. ignore_values = {
  63. "Report": ["disabled"],
  64. }
  65. ignore_doctypes = ["Page Role", "DocPerm"]
  66. def import_doclist(doclist):
  67. doctype = doclist[0]["doctype"]
  68. name = doclist[0]["name"]
  69. old_doc = None
  70. doctypes = set([d["doctype"] for d in doclist])
  71. ignore = list(doctypes.intersection(set(ignore_doctypes)))
  72. if doctype in ignore_values:
  73. if webnotes.conn.exists(doctype, name):
  74. old_doc = webnotes.doc(doctype, name)
  75. # delete old
  76. webnotes.delete_doc(doctype, name, force=1, ignore_doctypes=ignore, for_reload=True)
  77. # don't overwrite ignored docs
  78. doclist1 = remove_ignored_docs_if_they_already_exist(doclist, ignore, name)
  79. # update old values (if not to be overwritten)
  80. if doctype in ignore_values and old_doc:
  81. update_original_values(doclist1, doctype, old_doc)
  82. # reload_new
  83. new_bean = webnotes.bean(doclist1)
  84. new_bean.ignore_children_type = ignore
  85. new_bean.ignore_check_links = True
  86. new_bean.ignore_validate = True
  87. new_bean.ignore_permissions = True
  88. if doctype=="DocType" and name in ["DocField", "DocType"]:
  89. new_bean.ignore_fields = True
  90. new_bean.insert()
  91. def remove_ignored_docs_if_they_already_exist(doclist, ignore, name):
  92. doclist1 = doclist
  93. if ignore:
  94. has_records = []
  95. for d in ignore:
  96. if webnotes.conn.get_value(d, {"parent":name}):
  97. has_records.append(d)
  98. if has_records:
  99. doclist1 = filter(lambda d: d["doctype"] not in has_records, doclist)
  100. return doclist1
  101. def update_original_values(doclist, doctype, old_doc):
  102. for key in ignore_values[doctype]:
  103. doclist[0][key] = old_doc.fields[key]