You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

267 lines
9.3 KiB

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