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

119 行
3.3 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes, os
  5. from webnotes.modules import scrub, get_module_path, scrub_dt_dn
  6. import webnotes.plugins
  7. def import_files(module, dt=None, dn=None, plugin=None, force=False):
  8. if type(module) is list:
  9. out = []
  10. for m in module:
  11. out.append(import_file(m[0], m[1], m[2], plugin=plugin, force=force))
  12. return out
  13. else:
  14. return import_file(module, dt, dn, plugin=plugin, force=force)
  15. def import_file(module, dt, dn, plugin=None, force=False):
  16. """Sync a file from txt if modifed, return false if not updated"""
  17. webnotes.flags.in_import = True
  18. dt, dn = scrub_dt_dn(dt, dn)
  19. if plugin:
  20. path = webnotes.plugins.get_path(module, dt, dn, plugin, extn="txt")
  21. else:
  22. path = os.path.join(get_module_path(module),
  23. os.path.join(dt, dn, dn + '.txt'))
  24. ret = import_file_by_path(path, force)
  25. webnotes.flags.in_import = False
  26. return ret
  27. def import_file_by_path(path, force=False):
  28. if os.path.exists(path):
  29. from webnotes.modules.utils import peval_doclist
  30. with open(path, 'r') as f:
  31. doclist = peval_doclist(f.read())
  32. if doclist:
  33. doc = doclist[0]
  34. if not force:
  35. # check if timestamps match
  36. if doc['modified']==str(webnotes.conn.get_value(doc['doctype'], doc['name'], 'modified')):
  37. return False
  38. original_modified = doc["modified"]
  39. import_doclist(doclist)
  40. # since there is a new timestamp on the file, update timestamp in
  41. webnotes.conn.sql("update `tab%s` set modified=%s where name=%s" % \
  42. (doc['doctype'], '%s', '%s'),
  43. (original_modified, doc['name']))
  44. return True
  45. else:
  46. raise Exception, '%s missing' % path
  47. ignore_values = {
  48. "Report": ["disabled"],
  49. }
  50. ignore_doctypes = ["Page Role", "DocPerm"]
  51. def import_doclist(doclist):
  52. doctype = doclist[0]["doctype"]
  53. name = doclist[0]["name"]
  54. old_doc = None
  55. doctypes = set([d["doctype"] for d in doclist])
  56. ignore = list(doctypes.intersection(set(ignore_doctypes)))
  57. if doctype in ignore_values:
  58. if webnotes.conn.exists(doctype, name):
  59. old_doc = webnotes.doc(doctype, name)
  60. # delete old
  61. webnotes.delete_doc(doctype, name, force=1, ignore_doctypes=ignore, for_reload=True)
  62. # don't overwrite ignored docs
  63. doclist1 = remove_ignored_docs_if_they_already_exist(doclist, ignore, name)
  64. # update old values (if not to be overwritten)
  65. if doctype in ignore_values and old_doc:
  66. update_original_values(doclist1, doctype, old_doc)
  67. # reload_new
  68. new_bean = webnotes.bean(doclist1)
  69. new_bean.ignore_children_type = ignore
  70. new_bean.ignore_links = True
  71. new_bean.ignore_validate = True
  72. new_bean.ignore_permissions = True
  73. new_bean.ignore_mandatory = True
  74. if doctype=="DocType" and name in ["DocField", "DocType"]:
  75. new_bean.ignore_fields = True
  76. new_bean.insert()
  77. def remove_ignored_docs_if_they_already_exist(doclist, ignore, name):
  78. doclist1 = doclist
  79. if ignore:
  80. has_records = []
  81. for d in ignore:
  82. if webnotes.conn.get_value(d, {"parent":name}):
  83. has_records.append(d)
  84. if has_records:
  85. doclist1 = filter(lambda d: d["doctype"] not in has_records, doclist)
  86. return doclist1
  87. def update_original_values(doclist, doctype, old_doc):
  88. for key in ignore_values[doctype]:
  89. doclist[0][key] = old_doc.fields[key]