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

114 行
3.0 KiB

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