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

257 行
9.0 KiB

  1. import webnotes
  2. def rename_doc(doctype, old, new, is_doctype=0, debug=0):
  3. """
  4. Renames a doc(dt, old) to doc(dt, new) and
  5. updates all linked fields of type "Link" or "Select" with "link:"
  6. """
  7. import webnotes.utils
  8. import webnotes.model.doctype
  9. # get doclist of given doctype
  10. # without child fields of table type fields (form=0)
  11. doclist = webnotes.model.doctype.get(doctype, form=0)
  12. # rename the doc
  13. webnotes.conn.sql("update `tab%s` set name=%s where name=%s" \
  14. % (doctype, '%s', '%s'), (new, old), debug=debug)
  15. update_child_docs(old, new, doclist, debug=debug)
  16. if debug: webnotes.errprint("executed update_child_docs")
  17. if doctype=='DocType':
  18. # rename the table or change doctype of singles
  19. issingle = webnotes.conn.sql("""\
  20. select ifnull(issingle, 0) from `tabDocType`
  21. where name=%s""", new)
  22. if issingle and webnotes.utils.cint(issingle[0][0]) or 0:
  23. webnotes.conn.sql("""\
  24. update tabSingles set doctype=%s
  25. where doctype=%s""", (new, old))
  26. else:
  27. webnotes.conn.sql("rename table `tab%s` to `tab%s`" % (old, new))
  28. if debug: webnotes.errprint("executed rename table")
  29. # update link fields' values
  30. link_fields = get_link_fields(doctype)
  31. if debug: webnotes.errprint(link_fields)
  32. update_link_field_values(link_fields, old, new, debug=debug)
  33. if debug: webnotes.errprint("executed update_link_field_values")
  34. if doctype=='DocType':
  35. # change options for fieldtype Table
  36. update_parent_of_fieldtype_table(old, new, debug=debug)
  37. if debug: webnotes.errprint("executed update_parent_of_fieldtype_table")
  38. # change options where select options are hardcoded i.e. listed
  39. select_fields = get_select_fields(old, new, debug=debug)
  40. update_link_field_values(select_fields, old, new, debug=debug)
  41. if debug: webnotes.errprint("executed update_link_field_values")
  42. update_select_field_values(old, new, debug=debug)
  43. if debug: webnotes.errprint("executed update_select_field_values")
  44. # change parenttype for fieldtype Table
  45. update_parenttype_values(old, new, debug=debug)
  46. if debug: webnotes.errprint("executed update_parenttype_values")
  47. def update_child_docs(old, new, doclist, debug=0):
  48. """
  49. updates 'parent' field of child documents
  50. """
  51. # generator of a list of child doctypes
  52. child_doctypes = (d.options for d in doclist
  53. if d.doctype=='DocField' and d.fieldtype=='Table')
  54. for child in child_doctypes:
  55. webnotes.conn.sql("update `tab%s` set parent=%s where parent=%s" \
  56. % (child, '%s', '%s'), (new, old), debug=debug)
  57. def update_link_field_values(link_fields, old, new, debug=0):
  58. """
  59. updates values in tables where current doc is stored as a
  60. link field or select field
  61. """
  62. update_list = []
  63. # update values
  64. for field in link_fields:
  65. # if already updated, do not do it again
  66. if [field['parent'], field['fieldname']] in update_list:
  67. continue
  68. update_list.append([field['parent'], field['fieldname']])
  69. if field['issingle']:
  70. webnotes.conn.sql("""\
  71. update `tabSingles` set value=%s
  72. where doctype=%s and field=%s and value=%s""",
  73. (new, field['parent'], field['fieldname'], old),
  74. debug=debug)
  75. else:
  76. webnotes.conn.sql("""\
  77. update `tab%s` set `%s`=%s
  78. where `%s`=%s""" \
  79. % (field['parent'], field['fieldname'], '%s',
  80. field['fieldname'], '%s'),
  81. (new, old),
  82. debug=debug)
  83. def get_link_fields(doctype, debug=0):
  84. # get link fields from tabDocField
  85. link_fields = webnotes.conn.sql("""\
  86. select parent, fieldname,
  87. (select ifnull(issingle, 0) from tabDocType dt
  88. where dt.name = df.parent) as issingle
  89. from tabDocField df
  90. where
  91. df.parent not like "old%%%%" and df.parent != '0' and
  92. ((df.options=%s and df.fieldtype='Link') or
  93. (df.options='link:%s' and df.fieldtype='Select'))""" \
  94. % ('%s', doctype), doctype, as_dict=1, debug=debug)
  95. # get link fields from tabCustom Field
  96. custom_link_fields = webnotes.conn.sql("""\
  97. select dt as parent, fieldname,
  98. (select ifnull(issingle, 0) from tabDocType dt
  99. where dt.name = df.dt) as issingle
  100. from `tabCustom Field` df
  101. where
  102. df.dt not like "old%%%%" and df.dt != '0' and
  103. ((df.options=%s and df.fieldtype='Link') or
  104. (df.options='link:%s' and df.fieldtype='Select'))""" \
  105. % ('%s', doctype), doctype, as_dict=1, debug=debug)
  106. # add custom link fields list to link fields list
  107. link_fields += custom_link_fields
  108. # remove fields whose options have been changed using property setter
  109. property_setter_link_fields = webnotes.conn.sql("""\
  110. select ps.doc_type as parent, ps.field_name as fieldname,
  111. (select ifnull(issingle, 0) from tabDocType dt
  112. where dt.name = ps.doc_type) as issingle
  113. from `tabProperty Setter` ps
  114. where
  115. ps.property_type='options' and
  116. ps.field_name is not null and
  117. (ps.value=%s or ps.value='link:%s')""" \
  118. % ('%s', doctype), doctype, as_dict=1, debug=debug)
  119. link_fields += property_setter_link_fields
  120. return link_fields
  121. def update_parent_of_fieldtype_table(old, new, debug=0):
  122. webnotes.conn.sql("""\
  123. update `tabDocField` set options=%s
  124. where fieldtype='Table' and options=%s""", (new, old), debug=debug)
  125. webnotes.conn.sql("""\
  126. update `tabCustom Field` set options=%s
  127. where fieldtype='Table' and options=%s""", (new, old), debug=debug)
  128. webnotes.conn.sql("""\
  129. update `tabProperty Setter` set value=%s
  130. where property='options' and value=%s""", (new, old), debug=debug)
  131. def get_select_fields(old, new, debug=0):
  132. """
  133. get select type fields where doctype's name is hardcoded as
  134. new line separated list
  135. """
  136. # get link fields from tabDocField
  137. select_fields = webnotes.conn.sql("""\
  138. select parent, fieldname,
  139. (select ifnull(issingle, 0) from tabDocType dt
  140. where dt.name = df.parent) as issingle
  141. from tabDocField df
  142. where
  143. df.parent not like "old%%%%" and df.parent != '0' and
  144. df.parent != %s and df.fieldtype = 'Select' and
  145. df.options not like "link:%%%%" and
  146. (df.options like "%%%%%s%%%%")""" \
  147. % ('%s', old), new, as_dict=1, debug=debug)
  148. # get link fields from tabCustom Field
  149. custom_select_fields = webnotes.conn.sql("""\
  150. select dt as parent, fieldname,
  151. (select ifnull(issingle, 0) from tabDocType dt
  152. where dt.name = df.dt) as issingle
  153. from `tabCustom Field` df
  154. where
  155. df.dt not like "old%%%%" and df.dt != '0' and
  156. df.dt != %s and df.fieldtype = 'Select' and
  157. df.options not like "link:%%%%" and
  158. (df.options like "%%%%%s%%%%")""" \
  159. % ('%s', old), new, as_dict=1, debug=debug)
  160. # add custom link fields list to link fields list
  161. select_fields += custom_select_fields
  162. # remove fields whose options have been changed using property setter
  163. property_setter_select_fields = webnotes.conn.sql("""\
  164. select ps.doc_type as parent, ps.field_name as fieldname,
  165. (select ifnull(issingle, 0) from tabDocType dt
  166. where dt.name = ps.doc_type) as issingle
  167. from `tabProperty Setter` ps
  168. where
  169. ps.doc_type != %s and
  170. ps.property_type='options' and
  171. ps.field_name is not null and
  172. ps.value not like "link:%%%%" and
  173. (ps.value like "%%%%%s%%%%")""" \
  174. % ('%s', old), new, as_dict=1, debug=debug)
  175. select_fields += property_setter_select_fields
  176. return select_fields
  177. def update_select_field_values(old, new, debug=0):
  178. webnotes.conn.sql("""\
  179. update `tabDocField` set options=replace(options, %s, %s)
  180. where
  181. parent != %s and parent not like "old%%%%" and
  182. fieldtype = 'Select' and options not like "link:%%%%" and
  183. (options like "%%%%\\n%s%%%%" or options like "%%%%%s\\n%%%%")""" % \
  184. ('%s', '%s', '%s', old, old), (old, new, new), debug=debug)
  185. webnotes.conn.sql("""\
  186. update `tabCustom Field` set options=replace(options, %s, %s)
  187. where
  188. dt != %s and dt not like "old%%%%" and
  189. fieldtype = 'Select' and options not like "link:%%%%" and
  190. (options like "%%%%\\n%s%%%%" or options like "%%%%%s\\n%%%%")""" % \
  191. ('%s', '%s', '%s', old, old), (old, new, new), debug=debug)
  192. webnotes.conn.sql("""\
  193. update `tabProperty Setter` set value=replace(value, %s, %s)
  194. where
  195. doc_type != %s and field_name is not null and
  196. property='options' and value not like "link%%%%" and
  197. (value like "%%%%\\n%s%%%%" or value like "%%%%%s\\n%%%%")""" % \
  198. ('%s', '%s', '%s', old, old), (old, new, new), debug=debug)
  199. def update_parenttype_values(old, new, debug=0):
  200. child_doctypes = webnotes.conn.sql("""\
  201. select options, fieldname from `tabDocField`
  202. where parent=%s and fieldtype='Table'""", new, as_dict=1, debug=debug)
  203. custom_child_doctypes = webnotes.conn.sql("""\
  204. select options, fieldname from `tabCustom Field`
  205. where dt=%s and fieldtype='Table'""", new, as_dict=1, debug=debug)
  206. child_doctypes += custom_child_doctypes
  207. fields = [d['fieldname'] for d in child_doctypes]
  208. property_setter_child_doctypes = webnotes.conn.sql("""\
  209. select value as options from `tabProperty Setter`
  210. where doc_type=%s and property='options' and
  211. field_name in ("%s")""" % ('%s', '", "'.join(fields)),
  212. new, debug=debug)
  213. child_doctypes += property_setter_child_doctypes
  214. child_doctypes = (d['options'] for d in child_doctypes)
  215. for doctype in child_doctypes:
  216. webnotes.conn.sql("""\
  217. update `tab%s` set parenttype=%s
  218. where parenttype=%s""" % (doctype, '%s', '%s'),
  219. (new, old), debug=debug)