Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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