Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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