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

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