Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

228 lignes
7.7 KiB

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