選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

195 行
5.5 KiB

  1. """
  2. Sync's doctype and docfields from txt files to database
  3. perms will get synced only if none exist
  4. """
  5. import webnotes
  6. def sync_all(force=0):
  7. sync_core_doctypes(force)
  8. sync_modules(force)
  9. webnotes.conn.sql("DELETE FROM __CacheItem")
  10. def sync_core_doctypes(force=0):
  11. import os
  12. import core
  13. # doctypes
  14. walk_and_sync(os.path.abspath(os.path.dirname(core.__file__)), force)
  15. def sync_modules(force=0):
  16. import conf
  17. walk_and_sync(conf.modules_path, force)
  18. def walk_and_sync(start_path, force=0):
  19. """walk and sync all doctypes and pages"""
  20. import os
  21. from webnotes.modules import reload_doc
  22. modules = []
  23. for path, folders, files in os.walk(start_path):
  24. for f in files:
  25. if f.endswith(".txt"):
  26. # great grand-parent folder is module_name
  27. module_name = path.split(os.sep)[-3]
  28. if not module_name in modules:
  29. modules.append(module_name)
  30. # grand parent folder is doctype
  31. doctype = path.split(os.sep)[-2]
  32. # parent folder is the name
  33. name = path.split(os.sep)[-1]
  34. if doctype == 'doctype':
  35. sync(module_name, name, force)
  36. elif doctype == 'page':
  37. reload_doc(module_name, 'page', name)
  38. print module_name + ' | ' + doctype + ' | ' + name
  39. # load install docs
  40. load_install_docs(modules)
  41. # docname in small letters with underscores
  42. def sync(module_name, docname, force=0):
  43. with open(get_file_path(module_name, docname), 'r') as f:
  44. from webnotes.model.utils import peval_doclist
  45. doclist = peval_doclist(f.read())
  46. modified = doclist[0]['modified']
  47. if not doclist:
  48. raise Exception('DocList could not be evaluated')
  49. if modified == str(webnotes.conn.get_value(doclist[0].get('doctype'),
  50. doclist[0].get('name'), 'modified')) and not force:
  51. return
  52. webnotes.conn.begin()
  53. delete_doctype_docfields(doclist)
  54. save_doctype_docfields(doclist)
  55. save_perms_if_none_exist(doclist)
  56. webnotes.conn.sql("""UPDATE `tab{doctype}`
  57. SET modified=%s WHERE name=%s""".format(doctype=doclist[0]['doctype']),
  58. (modified, doclist[0]['name']))
  59. webnotes.conn.commit()
  60. print module_name, '|', docname
  61. #raise Exception
  62. return doclist[0].get('name')
  63. def get_file_path(module_name, docname):
  64. if not (module_name and docname):
  65. raise Exception('No Module Name or DocName specified')
  66. import os
  67. module = __import__(module_name)
  68. module_init_path = os.path.abspath(module.__file__)
  69. module_path = os.sep.join(module_init_path.split(os.sep)[:-1])
  70. return os.sep.join([module_path, 'doctype', docname, docname + '.txt'])
  71. def delete_doctype_docfields(doclist):
  72. parent = doclist[0].get('name')
  73. if not parent: raise Exception('Could not determine parent')
  74. webnotes.conn.sql("DELETE FROM `tabDocType` WHERE name=%s", parent)
  75. webnotes.conn.sql("DELETE FROM `tabDocField` WHERE parent=%s", parent)
  76. def save_doctype_docfields(doclist):
  77. from webnotes.model.doc import Document
  78. from webnotes.model.code import get_obj
  79. parent_doc = Document(fielddata=doclist[0])
  80. parent_doc.save(1, check_links=0,
  81. ignore_fields=1)
  82. idx = 1
  83. for d in doclist:
  84. if d.get('doctype') != 'DocField': continue
  85. d['idx'] = idx
  86. Document(fielddata=d).save(1, check_links=0, ignore_fields=1)
  87. idx += 1
  88. update_schema(parent_doc.name)
  89. def update_schema(docname):
  90. from webnotes.model.db_schema import updatedb
  91. updatedb(docname)
  92. from webnotes.utils.cache import CacheItem
  93. CacheItem(docname).clear()
  94. def save_perms_if_none_exist(doclist):
  95. res = webnotes.conn.sql("""SELECT name FROM `tabDocPerm`
  96. WHERE parent=%s""", doclist[0].get('name'))
  97. if res and res[0][0]: return
  98. from webnotes.model.doc import Document
  99. for d in doclist:
  100. if d.get('doctype') != 'DocPerm': continue
  101. Document(fielddata=d).save(1, check_links=0, ignore_fields=1)
  102. def load_install_docs(modules):
  103. import os
  104. if isinstance(modules, basestring): modules = [modules]
  105. for module_name in modules:
  106. module = __import__(module_name)
  107. if hasattr(module, 'install_docs'):
  108. webnotes.conn.begin()
  109. for data in module.install_docs:
  110. if data.get('name'):
  111. if not webnotes.conn.exists(data['doctype'], data.get('name')):
  112. create_doc(data)
  113. elif not webnotes.conn.exists(data):
  114. create_doc(data)
  115. webnotes.conn.commit()
  116. def create_doc(data):
  117. from webnotes.model.doc import Document
  118. d = Document(data['doctype'])
  119. d.fields.update(data)
  120. d.save()
  121. print 'Created %(doctype)s %(name)s' % d.fields
  122. import unittest
  123. class TestSync(unittest.TestCase):
  124. def setUp(self):
  125. self.test_case = {
  126. 'module_name': 'selling',
  127. 'docname': 'sales_order'
  128. }
  129. webnotes.conn.begin()
  130. def tearDown(self):
  131. pass
  132. #from webnotes.utils import cstr
  133. #webnotes.conn.rollback()
  134. def test_sync(self):
  135. #old_doctype, old_docfields = self.query('Profile')
  136. #self.parent = sync(self.test_case.get('module_name'), self.test_case.get('docname'))
  137. #new_doctype, new_docfields = self.query(self.parent)
  138. #self.assertNotEqual(old_doctype, new_doctype)
  139. #self.assertNotEqual(old_docfields, new_docfields)
  140. #from webnotes.utils import cstr
  141. #print new_doctype[0]
  142. #print
  143. #print "\n".join((cstr(d) for d in new_docfields))
  144. #print "\n\n"
  145. pass
  146. def test_sync_all(self):
  147. sync_all()
  148. def query(self, parent):
  149. doctype = webnotes.conn.sql("SELECT name FROM `tabDocType` \
  150. WHERE name=%s", parent)
  151. docfields = webnotes.conn.sql("SELECT idx, fieldname, label, fieldtype FROM `tabDocField` \
  152. WHERE parent=%s order by idx", parent)
  153. #doctype = webnotes.conn.sql("SELECT * FROM `tabDocType` \
  154. # WHERE name=%s", parent)
  155. #docfields = webnotes.conn.sql("SELECT * FROM `tabDocField` \
  156. # WHERE parent=%s order by idx", parent)
  157. return doctype, docfields