You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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