您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

__init__.py 8.5 KiB

14 年前
14 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. # model __init__.py
  23. from __future__ import unicode_literals
  24. import webnotes
  25. no_value_fields = ['Section Break', 'Column Break', 'HTML', 'Table', 'FlexTable', 'Button', 'Image', 'Graph']
  26. default_fields = ['doctype','name','owner','creation','modified','modified_by','parent','parentfield','parenttype','idx','docstatus']
  27. #=================================================================================
  28. def check_if_doc_is_linked(dt, dn):
  29. """
  30. Raises excption if the given doc(dt, dn) is linked in another record.
  31. """
  32. sql = webnotes.conn.sql
  33. ll = get_link_fields(dt)
  34. for l in ll:
  35. link_dt, link_field = l
  36. issingle = sql("select issingle from tabDocType where name = '%s'" % link_dt)
  37. # no such doctype (?)
  38. if not issingle: continue
  39. if issingle[0][0]:
  40. item = sql("select doctype from `tabSingles` where field='%s' and value = '%s' and doctype = '%s' " % (link_field, dn, l[0]))
  41. if item:
  42. webnotes.msgprint("Cannot delete %s <b>%s</b> because it is linked in <b>%s</b>" % (dt, dn, item[0][0]), raise_exception=1)
  43. else:
  44. item = None
  45. try:
  46. item = sql("select name, parent, parenttype from `tab%s` where `%s`='%s' and docstatus!=2 limit 1" % (link_dt, link_field, dn))
  47. except Exception, e:
  48. if e.args[0]==1146: pass
  49. else: raise e
  50. if item:
  51. webnotes.msgprint("Cannot delete %s <b>%s</b> because it is linked in %s <b>%s</b>" % (dt, dn, item[0][2] or link_dt, item[0][1] or item[0][0]), raise_exception=1)
  52. @webnotes.whitelist()
  53. def delete_doc(doctype=None, name=None, doclist = None, force=0):
  54. """
  55. Deletes a doc(dt, dn) and validates if it is not submitted and not linked in a live record
  56. """
  57. import webnotes.model.meta
  58. sql = webnotes.conn.sql
  59. # get from form
  60. if not doctype:
  61. doctype = webnotes.form_dict.get('dt')
  62. name = webnotes.form_dict.get('dn')
  63. if not doctype:
  64. webnotes.msgprint('Nothing to delete!', raise_exception =1)
  65. # already deleted..?
  66. if not webnotes.conn.exists(doctype, name):
  67. return
  68. tablefields = webnotes.model.meta.get_table_fields(doctype)
  69. # check if submitted
  70. d = webnotes.conn.sql("select docstatus from `tab%s` where name=%s" % (doctype, '%s'), name)
  71. if d and int(d[0][0]) == 1:
  72. webnotes.msgprint("Submitted Record '%s' '%s' cannot be deleted" % (doctype, name))
  73. raise Exception
  74. # call on_trash if required
  75. from webnotes.model.code import get_obj
  76. if doclist:
  77. obj = get_obj(doclist=doclist)
  78. else:
  79. obj = get_obj(doctype, name)
  80. if hasattr(obj,'on_trash'):
  81. obj.on_trash()
  82. if doctype=='DocType':
  83. webnotes.conn.sql("delete from `tabCustom Field` where dt = %s", name)
  84. webnotes.conn.sql("delete from `tabCustom Script` where dt = %s", name)
  85. webnotes.conn.sql("delete from `tabProperty Setter` where doc_type = %s", name)
  86. webnotes.conn.sql("delete from `tabSearch Criteria` where doc_type = %s", name)
  87. # check if links exist
  88. if not force:
  89. check_if_doc_is_linked(doctype, name)
  90. # remove tags
  91. from webnotes.widgets.tags import clear_tags
  92. clear_tags(doctype, name)
  93. try:
  94. webnotes.conn.sql("delete from `tab%s` where name='%s' limit 1" % (doctype, name))
  95. for t in tablefields:
  96. webnotes.conn.sql("delete from `tab%s` where parent = %s" % (t[0], '%s'), name)
  97. except Exception, e:
  98. if e.args[0]==1451:
  99. webnotes.msgprint("Cannot delete %s '%s' as it is referenced in another record. You must delete the referred record first" % (doctype, name))
  100. raise e
  101. return 'okay'
  102. def get_search_criteria(dt):
  103. import webnotes.model.doc
  104. # load search criteria for reports (all)
  105. dl = []
  106. try: # bc
  107. sc_list = webnotes.conn.sql("select name from `tabSearch Criteria` where doc_type = '%s' or parent_doc_type = '%s' and (disabled!=1 OR disabled IS NULL)" % (dt, dt))
  108. for sc in sc_list:
  109. dl += webnotes.model.doc.get('Search Criteria', sc[0])
  110. except Exception, e:
  111. pass # no search criteria
  112. return dl
  113. # Rename Doc
  114. #=================================================================================
  115. def rename(doctype, old, new, is_doctype=0, debug=1):
  116. import webnotes.model.rename_doc
  117. webnotes.model.rename_doc.rename_doc(doctype, old, new, is_doctype, debug)
  118. def get_link_fields(dt):
  119. """
  120. Returns linked fields for dt as a tuple of (linked_doctype, linked_field)
  121. """
  122. import webnotes.model.rename_doc
  123. link_fields = webnotes.model.rename_doc.get_link_fields(dt)
  124. link_fields = [[lf['parent'], lf['fieldname']] for lf in link_fields]
  125. return link_fields
  126. #=================================================================================
  127. def clear_recycle_bin():
  128. """
  129. Clears temporary records that have been deleted
  130. """
  131. sql = webnotes.conn.sql
  132. tl = sql('show tables')
  133. total_deleted = 0
  134. for t in tl:
  135. fl = [i[0] for i in sql('desc `%s`' % t[0])]
  136. if 'name' in fl:
  137. total_deleted += sql("select count(*) from `%s` where name like '__overwritten:%%'" % t[0])[0][0]
  138. sql("delete from `%s` where name like '__overwritten:%%'" % t[0])
  139. if 'parent' in fl:
  140. total_deleted += sql("select count(*) from `%s` where parent like '__oldparent:%%'" % t[0])[0][0]
  141. sql("delete from `%s` where parent like '__oldparent:%%'" % t[0])
  142. total_deleted += sql("select count(*) from `%s` where parent like 'oldparent:%%'" % t[0])[0][0]
  143. sql("delete from `%s` where parent like 'oldparent:%%'" % t[0])
  144. total_deleted += sql("select count(*) from `%s` where parent like 'old_parent:%%'" % t[0])[0][0]
  145. sql("delete from `%s` where parent like 'old_parent:%%'" % t[0])
  146. webnotes.msgprint("%s records deleted" % str(int(total_deleted)))
  147. # Make Table Copy
  148. #=================================================================================
  149. def copytables(srctype, src, srcfield, tartype, tar, tarfield, srcfields, tarfields=[]):
  150. import webnotes.model.doc
  151. if not tarfields:
  152. tarfields = srcfields
  153. l = []
  154. data = webnotes.model.doc.getchildren(src.name, srctype, srcfield)
  155. for d in data:
  156. newrow = webnotes.model.doc.addchild(tar, tarfield, tartype, local = 1)
  157. newrow.idx = d.idx
  158. for i in range(len(srcfields)):
  159. newrow.fields[tarfields[i]] = d.fields[srcfields[i]]
  160. l.append(newrow)
  161. return l
  162. # DB Exists
  163. #=================================================================================
  164. def db_exists(dt, dn):
  165. import webnotes
  166. return webnotes.conn.exists(dt, dn)
  167. def delete_fields(args_dict, delete=0):
  168. """
  169. Delete a field.
  170. * Deletes record from `tabDocField`
  171. * If not single doctype: Drops column from table
  172. * If single, deletes record from `tabSingles`
  173. args_dict = { dt: [field names] }
  174. """
  175. import webnotes.utils
  176. for dt in args_dict.keys():
  177. fields = args_dict[dt]
  178. if not fields: continue
  179. webnotes.conn.sql("""\
  180. DELETE FROM `tabDocField`
  181. WHERE parent=%s AND fieldname IN (%s)
  182. """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt)
  183. # Delete the data / column only if delete is specified
  184. if not delete: continue
  185. is_single = webnotes.conn.sql("select issingle from tabDocType where name = '%s'" % dt)
  186. is_single = is_single and webnotes.utils.cint(is_single[0][0]) or 0
  187. if is_single:
  188. webnotes.conn.sql("""\
  189. DELETE FROM `tabSingles`
  190. WHERE doctype=%s AND field IN (%s)
  191. """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt)
  192. else:
  193. existing_fields = webnotes.conn.sql("desc `tab%s`" % dt)
  194. existing_fields = existing_fields and [e[0] for e in existing_fields] or []
  195. query = "ALTER TABLE `tab%s` " % dt + \
  196. ", ".join(["DROP COLUMN `%s`" % f for f in fields if f in existing_fields])
  197. webnotes.conn.commit()
  198. webnotes.conn.sql(query)