Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

rename_doc.py 9.7 KiB

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