Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

247 lignes
8.0 KiB

  1. # model __init__.py
  2. import webnotes
  3. no_value_fields = ['Section Break', 'Column Break', 'HTML', 'Table', 'FlexTable', 'Button', 'Image', 'Graph']
  4. default_fields = ['doctype','name','owner','creation','modified','modified_by','parent','parentfield','parenttype','idx','docstatus']
  5. #=================================================================================
  6. def check_if_doc_is_linked(dt, dn):
  7. """
  8. Raises excption if the given doc(dt, dn) is linked in another record.
  9. """
  10. sql = webnotes.conn.sql
  11. ll = get_link_fields(dt)
  12. for l in ll:
  13. link_dt, link_field = l
  14. issingle = sql("select issingle from tabDocType where name = '%s'" % link_dt)
  15. # no such doctype (?)
  16. if not issingle: continue
  17. if issingle[0][0]:
  18. item = sql("select doctype from `tabSingles` where field='%s' and value = '%s' and doctype = '%s' " % (link_field, dn, l[0]))
  19. if item:
  20. webnotes.msgprint("Cannot delete %s <b>%s</b> because it is linked in <b>%s</b>" % (dt, dn, item[0][0]), raise_exception=1)
  21. else:
  22. item = None
  23. try:
  24. item = sql("select name from `tab%s` where `%s`='%s' and docstatus!=2 limit 1" % (link_dt, link_field, dn))
  25. except Exception, e:
  26. if e.args[0]==1146: pass
  27. else: raise e
  28. if item:
  29. webnotes.msgprint("Cannot delete %s <b>%s</b> because it is linked in %s <b>%s</b>" % (dt, dn, link_dt, item[0][0]), raise_exception=1)
  30. @webnotes.whitelist()
  31. def delete_doc(doctype=None, name=None, doclist = None, force=0):
  32. """
  33. Deletes a doc(dt, dn) and validates if it is not submitted and not linked in a live record
  34. """
  35. import webnotes.model.meta
  36. sql = webnotes.conn.sql
  37. # get from form
  38. if not doctype:
  39. doctype = webnotes.form_dict.get('dt')
  40. name = webnotes.form_dict.get('dn')
  41. if not doctype:
  42. webnotes.msgprint('Nothing to delete!', raise_exception =1)
  43. # already deleted..?
  44. if not webnotes.conn.exists(doctype, name):
  45. return
  46. tablefields = webnotes.model.meta.get_table_fields(doctype)
  47. # check if submitted
  48. d = webnotes.conn.sql("select docstatus from `tab%s` where name=%s" % (doctype, '%s'), name)
  49. if d and int(d[0][0]) == 1:
  50. webnotes.msgprint("Submitted Record '%s' '%s' cannot be deleted" % (doctype, name))
  51. raise Exception
  52. # call on_trash if required
  53. from webnotes.model.code import get_obj
  54. if doclist:
  55. obj = get_obj(doclist=doclist)
  56. else:
  57. obj = get_obj(doctype, name)
  58. if hasattr(obj,'on_trash'):
  59. obj.on_trash()
  60. # check if links exist
  61. if not force:
  62. check_if_doc_is_linked(doctype, name)
  63. # remove tags
  64. from webnotes.widgets.tags import clear_tags
  65. clear_tags(doctype, name)
  66. try:
  67. webnotes.conn.sql("delete from `tab%s` where name='%s' limit 1" % (doctype, name))
  68. for t in tablefields:
  69. webnotes.conn.sql("delete from `tab%s` where parent = %s" % (t[0], '%s'), name)
  70. except Exception, e:
  71. if e.args[0]==1451:
  72. webnotes.msgprint("Cannot delete %s '%s' as it is referenced in another record. You must delete the referred record first" % (doctype, name))
  73. raise e
  74. return 'okay'
  75. def get_search_criteria(dt):
  76. import webnotes.model.doc
  77. # load search criteria for reports (all)
  78. dl = []
  79. try: # bc
  80. 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))
  81. for sc in sc_list:
  82. dl += webnotes.model.doc.get('Search Criteria', sc[0])
  83. except Exception, e:
  84. pass # no search criteria
  85. return dl
  86. # Rename Doc
  87. #=================================================================================
  88. def get_link_fields(dt):
  89. """
  90. Returns linked fields for dt as a tuple of (linked_doctype, linked_field)
  91. """
  92. return webnotes.conn.sql("select parent, fieldname from tabDocField where parent not like 'old%%' and ((options = '%s' and fieldtype='Link') or (options = 'link:%s' and fieldtype='Select'))" % (dt, dt))
  93. def rename(dt, old, new, is_doctype = 0):
  94. """
  95. Renames a doc(dt, old) to doc(dt, new) and updates all linked fields of type "Link" or "Select" with "link:"
  96. """
  97. sql = webnotes.conn.sql
  98. # rename doc
  99. sql("update `tab%s` set name='%s' where name='%s'" % (dt, new, old))
  100. # get child docs
  101. ct = sql("select options from tabDocField where parent = '%s' and fieldtype='Table'" % dt)
  102. for c in ct:
  103. sql("update `tab%s` set parent='%s' where parent='%s'" % (c[0], new, old))
  104. # get links (link / select)
  105. ll = get_link_fields(dt)
  106. for l in ll:
  107. is_single = sql("select issingle from tabDocType where name = '%s'" % l[0])
  108. is_single = is_single and webnotes.utils.cint(is_single[0][0]) or 0
  109. if is_single:
  110. sql("update `tabSingles` set value='%s' where field='%s' and value = '%s' and doctype = '%s' " % (new, l[1], old, l[0]))
  111. else:
  112. sql("update `tab%s` set `%s`='%s' where `%s`='%s'" % (l[0], l[1], new, l[1], old))
  113. # doctype
  114. if is_doctype:
  115. sql("RENAME TABLE `tab%s` TO `tab%s`" % (old, new))
  116. # get child docs (update parenttype)
  117. ct = sql("select options from tabDocField where parent = '%s' and fieldtype='Table'" % new)
  118. for c in ct:
  119. sql("update `tab%s` set parenttype='%s' where parenttype='%s'" % (c[0], new, old))
  120. #=================================================================================
  121. def clear_recycle_bin():
  122. """
  123. Clears temporary records that have been deleted
  124. """
  125. sql = webnotes.conn.sql
  126. tl = sql('show tables')
  127. total_deleted = 0
  128. for t in tl:
  129. fl = [i[0] for i in sql('desc `%s`' % t[0])]
  130. if 'name' in fl:
  131. total_deleted += sql("select count(*) from `%s` where name like '__overwritten:%%'" % t[0])[0][0]
  132. sql("delete from `%s` where name like '__overwritten:%%'" % t[0])
  133. if 'parent' in fl:
  134. total_deleted += sql("select count(*) from `%s` where parent like '__oldparent:%%'" % t[0])[0][0]
  135. sql("delete from `%s` where parent like '__oldparent:%%'" % t[0])
  136. total_deleted += sql("select count(*) from `%s` where parent like 'oldparent:%%'" % t[0])[0][0]
  137. sql("delete from `%s` where parent like 'oldparent:%%'" % t[0])
  138. total_deleted += sql("select count(*) from `%s` where parent like 'old_parent:%%'" % t[0])[0][0]
  139. sql("delete from `%s` where parent like 'old_parent:%%'" % t[0])
  140. webnotes.msgprint("%s records deleted" % str(int(total_deleted)))
  141. # Make Table Copy
  142. #=================================================================================
  143. def copytables(srctype, src, srcfield, tartype, tar, tarfield, srcfields, tarfields=[]):
  144. import webnotes.model.doc
  145. if not tarfields:
  146. tarfields = srcfields
  147. l = []
  148. data = webnotes.model.doc.getchildren(src.name, srctype, srcfield)
  149. for d in data:
  150. newrow = webnotes.model.doc.addchild(tar, tarfield, tartype, local = 1)
  151. newrow.idx = d.idx
  152. for i in range(len(srcfields)):
  153. newrow.fields[tarfields[i]] = d.fields[srcfields[i]]
  154. l.append(newrow)
  155. return l
  156. # DB Exists
  157. #=================================================================================
  158. def db_exists(dt, dn):
  159. import webnotes
  160. return webnotes.conn.exists(dt, dn)
  161. def delete_fields(args_dict, delete=0):
  162. """
  163. Delete a field.
  164. * Deletes record from `tabDocField`
  165. * If not single doctype: Drops column from table
  166. * If single, deletes record from `tabSingles`
  167. args_dict = { dt: [field names] }
  168. """
  169. import webnotes.utils
  170. for dt in args_dict.keys():
  171. fields = args_dict[dt]
  172. if not fields: continue
  173. webnotes.conn.sql("""\
  174. DELETE FROM `tabDocField`
  175. WHERE parent=%s AND fieldname IN (%s)
  176. """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt)
  177. # Delete the data / column only if delete is specified
  178. if not delete: continue
  179. is_single = webnotes.conn.sql("select issingle from tabDocType where name = '%s'" % dt)
  180. is_single = is_single and webnotes.utils.cint(is_single[0][0]) or 0
  181. if is_single:
  182. webnotes.conn.sql("""\
  183. DELETE FROM `tabSingles`
  184. WHERE doctype=%s AND field IN (%s)
  185. """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt)
  186. else:
  187. existing_fields = webnotes.conn.sql("desc `tab%s`" % dt)
  188. existing_fields = existing_fields and [e[0] for e in existing_fields] or []
  189. query = "ALTER TABLE `tab%s` " % dt + \
  190. ", ".join(["DROP COLUMN `%s`" % f for f in fields if f in existing_fields])
  191. webnotes.conn.commit()
  192. webnotes.conn.sql(query)