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.
 
 
 
 
 
 

276 lignes
9.1 KiB

  1. from __future__ import unicode_literals
  2. import webnotes
  3. from webnotes import _
  4. import webnotes.utils
  5. import webnotes.model.doctype
  6. @webnotes.whitelist()
  7. def rename_doc(doctype, old, new, force=False, merge=False):
  8. """
  9. Renames a doc(dt, old) to doc(dt, new) and
  10. updates all linked fields of type "Link" or "Select" with "link:"
  11. """
  12. if not webnotes.conn.exists(doctype, old):
  13. return
  14. # get doclist of given doctype
  15. doclist = webnotes.model.doctype.get(doctype)
  16. validate_rename(doctype, new, doclist, merge, force)
  17. # call on_rename
  18. obj = webnotes.get_obj(doctype, old)
  19. if hasattr(obj, 'on_rename'):
  20. new = obj.on_rename(new, old) or new
  21. if not merge:
  22. rename_parent_and_child(doctype, old, new, doclist)
  23. # update link fields' values
  24. link_fields = get_link_fields(doctype)
  25. update_link_field_values(link_fields, old, new)
  26. if doctype=='DocType':
  27. rename_doctype(doctype, old, new, force)
  28. if merge:
  29. webnotes.delete_doc(doctype, old)
  30. return new
  31. def rename_parent_and_child(doctype, old, new, doclist):
  32. # rename the doc
  33. webnotes.conn.sql("update `tab%s` set name=%s where name=%s" \
  34. % (doctype, '%s', '%s'), (new, old))
  35. update_child_docs(old, new, doclist)
  36. def validate_rename(doctype, new, doclist, merge, force):
  37. exists = webnotes.conn.exists(doctype, new)
  38. if merge and not exists:
  39. webnotes.msgprint("%s: %s does not exist, select a new target to merge." % (doctype, new), raise_exception=1)
  40. if not merge and exists:
  41. webnotes.msgprint("%s: %s exists, select a new, new name." % (doctype, new), raise_exception=1)
  42. if not webnotes.has_permission(doctype, "write"):
  43. webnotes.msgprint("You need write permission to rename", raise_exception=1)
  44. if not force and not doclist[0].allow_rename:
  45. webnotes.msgprint("%s cannot be renamed" % doctype, raise_exception=1)
  46. def rename_doctype(doctype, old, new, force=False):
  47. # change options for fieldtype Table
  48. update_parent_of_fieldtype_table(old, new)
  49. # change options where select options are hardcoded i.e. listed
  50. select_fields = get_select_fields(old, new)
  51. update_link_field_values(select_fields, old, new)
  52. update_select_field_values(old, new)
  53. # change parenttype for fieldtype Table
  54. update_parenttype_values(old, new)
  55. # rename comments
  56. webnotes.conn.sql("""update tabComment set comment_doctype=%s where comment_doctype=%s""",
  57. (new, old))
  58. # update mapper
  59. rename_mapper(new)
  60. def rename_mapper(new):
  61. for mapper in webnotes.conn.sql("""select name, from_doctype, to_doctype
  62. from `tabDocType Mapper` where from_doctype=%s or to_doctype=%s""", (new, new), as_dict=1):
  63. rename_doc("DocType Mapper", mapper.name, mapper.from_doctype + "-" + mapper.to_doctype, force=True)
  64. def update_child_docs(old, new, doclist):
  65. # update "parent"
  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))
  71. def update_link_field_values(link_fields, old, new):
  72. update_list = []
  73. # update values
  74. for field in link_fields:
  75. # if already updated, do not do it again
  76. if [field['parent'], field['fieldname']] in update_list:
  77. continue
  78. update_list.append([field['parent'], field['fieldname']])
  79. if field['issingle']:
  80. webnotes.conn.sql("""\
  81. update `tabSingles` set value=%s
  82. where doctype=%s and field=%s and value=%s""",
  83. (new, field['parent'], field['fieldname'], old))
  84. else:
  85. webnotes.conn.sql("""\
  86. update `tab%s` set `%s`=%s
  87. where `%s`=%s""" \
  88. % (field['parent'], field['fieldname'], '%s',
  89. field['fieldname'], '%s'),
  90. (new, old))
  91. def get_link_fields(doctype):
  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)
  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)
  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)
  127. link_fields += property_setter_link_fields
  128. return link_fields
  129. def update_parent_of_fieldtype_table(old, new):
  130. webnotes.conn.sql("""\
  131. update `tabDocField` set options=%s
  132. where fieldtype='Table' and options=%s""", (new, old))
  133. webnotes.conn.sql("""\
  134. update `tabCustom Field` set options=%s
  135. where fieldtype='Table' and options=%s""", (new, old))
  136. webnotes.conn.sql("""\
  137. update `tabProperty Setter` set value=%s
  138. where property='options' and value=%s""", (new, old))
  139. def get_select_fields(old, new):
  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)
  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)
  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)
  183. select_fields += property_setter_select_fields
  184. return select_fields
  185. def update_select_field_values(old, new):
  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))
  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))
  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))
  207. def update_parenttype_values(old, new):
  208. child_doctypes = webnotes.conn.sql("""\
  209. select options, fieldname from `tabDocField`
  210. where parent=%s and fieldtype='Table'""", new, as_dict=1)
  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)
  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)
  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))