Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

276 wiersze
9.6 KiB

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