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

288 行
9.3 KiB

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