Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

283 righe
9.2 KiB

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