Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

117 rindas
3.1 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. 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=force))
  11. return out
  12. else:
  13. return import_file(module, dt, dn, force=force)
  14. def import_file(module, dt, dn, force=False):
  15. """Sync a file from txt if modifed, return false if not updated"""
  16. dt, dn = scrub_dt_dn(dt, dn)
  17. path = os.path.join(get_module_path(module),
  18. os.path.join(dt, dn, dn + '.txt'))
  19. ret = import_file_by_path(path, force)
  20. return ret
  21. def import_file_by_path(path, force=False):
  22. webnotes.flags.in_import = True
  23. if os.path.exists(path):
  24. from webnotes.modules.utils import peval_doclist
  25. with open(path, 'r') as f:
  26. doclist = peval_doclist(f.read())
  27. if doclist:
  28. doc = doclist[0]
  29. if not force:
  30. # check if timestamps match
  31. if doc['modified']==str(webnotes.conn.get_value(doc['doctype'], doc['name'], 'modified')):
  32. return False
  33. original_modified = doc["modified"]
  34. import_doclist(doclist)
  35. # since there is a new timestamp on the file, update timestamp in
  36. webnotes.conn.sql("update `tab%s` set modified=%s where name=%s" % \
  37. (doc['doctype'], '%s', '%s'),
  38. (original_modified, doc['name']))
  39. else:
  40. raise Exception, '%s missing' % path
  41. webnotes.flags.in_import = False
  42. return True
  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_links = True
  67. new_bean.ignore_validate = True
  68. new_bean.ignore_permissions = True
  69. new_bean.ignore_mandatory = True
  70. new_bean.ignore_restrictions = True
  71. if doctype=="DocType" and name in ["DocField", "DocType"]:
  72. new_bean.ignore_fields = True
  73. new_bean.insert()
  74. def remove_ignored_docs_if_they_already_exist(doclist, ignore, name):
  75. doclist1 = doclist
  76. if ignore:
  77. has_records = []
  78. for d in ignore:
  79. if webnotes.conn.get_value(d, {"parent":name}):
  80. has_records.append(d)
  81. if has_records:
  82. doclist1 = filter(lambda d: d["doctype"] not in has_records, doclist)
  83. return doclist1
  84. def update_original_values(doclist, doctype, old_doc):
  85. for key in ignore_values[doctype]:
  86. doclist[0][key] = old_doc.fields[key]