Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

13 лет назад
12 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
12 лет назад
13 лет назад
12 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. from __future__ import unicode_literals
  2. import webnotes
  3. @webnotes.whitelist()
  4. def rename_doc(doctype, old, new, debug=0, force=False):
  5. """
  6. Renames a doc(dt, old) to doc(dt, new) and
  7. updates all linked fields of type "Link" or "Select" with "link:"
  8. """
  9. import webnotes.utils
  10. import webnotes.model.doctype
  11. from webnotes.model.code import get_obj
  12. # get doclist of given doctype
  13. doclist = webnotes.model.doctype.get(doctype)
  14. if webnotes.conn.exists(doctype, new):
  15. webnotes.msgprint("%s: %s exists, select a new, new name." % (doctype, new))
  16. if not webnotes.has_permission(doctype, "write"):
  17. webnotes.msgprint("You need write permission to rename", raise_exception=1)
  18. if not force and not doclist[0].allow_rename:
  19. webnotes.msgprint("%s cannot be renamed" % doctype, raise_exception=1)
  20. # without child fields of table type fields (form=0)
  21. # call on_rename method if exists
  22. obj = get_obj(doctype, old)
  23. if hasattr(obj, 'on_rename'):
  24. new = obj.on_rename(new, old) or new
  25. # rename the doc
  26. webnotes.conn.sql("update `tab%s` set name=%s where name=%s" \
  27. % (doctype, '%s', '%s'), (new, old), debug=debug)
  28. update_child_docs(old, new, doclist, debug=debug)
  29. if debug: webnotes.errprint("executed update_child_docs")
  30. if doctype=='DocType':
  31. # rename the table or change doctype of singles
  32. issingle = webnotes.conn.sql("""\
  33. select ifnull(issingle, 0) from `tabDocType`
  34. where name=%s""", new)
  35. if issingle and webnotes.utils.cint(issingle[0][0]) or 0:
  36. webnotes.conn.sql("""\
  37. update tabSingles set doctype=%s
  38. where doctype=%s""", (new, old))
  39. else:
  40. webnotes.conn.sql("rename table `tab%s` to `tab%s`" % (old, new))
  41. if debug: webnotes.errprint("executed rename table")
  42. # update link fields' values
  43. link_fields = get_link_fields(doctype)
  44. if debug: webnotes.errprint(link_fields)
  45. update_link_field_values(link_fields, old, new, debug=debug)
  46. if debug: webnotes.errprint("executed update_link_field_values")
  47. if doctype=='DocType':
  48. rename_doctype(doctype, old, new, debug, force)
  49. return new
  50. def rename_doctype(doctype, old, new, debug=0, force=False):
  51. # change options for fieldtype Table
  52. update_parent_of_fieldtype_table(old, new, debug=debug)
  53. if debug: webnotes.errprint("executed update_parent_of_fieldtype_table")
  54. # change options where select options are hardcoded i.e. listed
  55. select_fields = get_select_fields(old, new, debug=debug)
  56. update_link_field_values(select_fields, old, new, debug=debug)
  57. if debug: webnotes.errprint("executed update_link_field_values")
  58. update_select_field_values(old, new, debug=debug)
  59. if debug: webnotes.errprint("executed update_select_field_values")
  60. # change parenttype for fieldtype Table
  61. update_parenttype_values(old, new, debug=debug)
  62. if debug: webnotes.errprint("executed update_parenttype_values")
  63. # update mapper
  64. rename_mapper(new)
  65. def rename_mapper(new):
  66. for mapper in webnotes.conn.sql("""select name, from_doctype, to_doctype
  67. from `tabDocType Mapper` where from_doctype=%s or to_doctype=%s""", (new, new), as_dict=1):
  68. rename_doc("DocType Mapper", mapper.name, mapper.from_doctype + "-" + mapper.to_doctype, force=True)
  69. def update_child_docs(old, new, doclist, debug=0):
  70. """
  71. updates 'parent' field of child documents
  72. """
  73. # generator of a list of child doctypes
  74. child_doctypes = (d.options for d in doclist
  75. if d.doctype=='DocField' and d.fieldtype=='Table')
  76. for child in child_doctypes:
  77. webnotes.conn.sql("update `tab%s` set parent=%s where parent=%s" \
  78. % (child, '%s', '%s'), (new, old), debug=debug)
  79. def update_link_field_values(link_fields, old, new, debug=0):
  80. """
  81. updates values in tables where current doc is stored as a
  82. link field or select field
  83. """
  84. update_list = []
  85. # update values
  86. for field in link_fields:
  87. # if already updated, do not do it again
  88. if [field['parent'], field['fieldname']] in update_list:
  89. continue
  90. update_list.append([field['parent'], field['fieldname']])
  91. if field['issingle']:
  92. webnotes.conn.sql("""\
  93. update `tabSingles` set value=%s
  94. where doctype=%s and field=%s and value=%s""",
  95. (new, field['parent'], field['fieldname'], old),
  96. debug=debug)
  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. debug=debug)
  105. def get_link_fields(doctype, debug=0):
  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, debug=debug)
  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, debug=debug)
  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, debug=debug)
  141. link_fields += property_setter_link_fields
  142. return link_fields
  143. def update_parent_of_fieldtype_table(old, new, debug=0):
  144. webnotes.conn.sql("""\
  145. update `tabDocField` set options=%s
  146. where fieldtype='Table' and options=%s""", (new, old), debug=debug)
  147. webnotes.conn.sql("""\
  148. update `tabCustom Field` set options=%s
  149. where fieldtype='Table' and options=%s""", (new, old), debug=debug)
  150. webnotes.conn.sql("""\
  151. update `tabProperty Setter` set value=%s
  152. where property='options' and value=%s""", (new, old), debug=debug)
  153. def get_select_fields(old, new, debug=0):
  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, debug=debug)
  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, debug=debug)
  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, debug=debug)
  197. select_fields += property_setter_select_fields
  198. return select_fields
  199. def update_select_field_values(old, new, debug=0):
  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), debug=debug)
  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), debug=debug)
  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), debug=debug)
  221. def update_parenttype_values(old, new, debug=0):
  222. child_doctypes = webnotes.conn.sql("""\
  223. select options, fieldname from `tabDocField`
  224. where parent=%s and fieldtype='Table'""", new, as_dict=1, debug=debug)
  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, debug=debug)
  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, debug=debug)
  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), debug=debug)