25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

97 lines
3.2 KiB

  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. import webnotes.model.meta
  6. import webnotes.defaults
  7. from webnotes.utils.file_manager import remove_all
  8. from webnotes import _
  9. def delete_doc(doctype=None, name=None, doclist = None, force=0, ignore_doctypes=[], for_reload=False, ignore_permissions=False):
  10. """
  11. Deletes a doc(dt, dn) and validates if it is not submitted and not linked in a live record
  12. """
  13. # get from form
  14. if not doctype:
  15. doctype = webnotes.form_dict.get('dt')
  16. name = webnotes.form_dict.get('dn')
  17. if not doctype:
  18. webnotes.msgprint('Nothing to delete!', raise_exception =1)
  19. # already deleted..?
  20. if not webnotes.conn.exists(doctype, name):
  21. return
  22. if not for_reload:
  23. check_permission_and_not_submitted(doctype, name, ignore_permissions)
  24. run_on_trash(doctype, name, doclist)
  25. # check if links exist
  26. if not force:
  27. check_if_doc_is_linked(doctype, name)
  28. try:
  29. tablefields = webnotes.model.meta.get_table_fields(doctype)
  30. webnotes.conn.sql("delete from `tab%s` where name=%s" % (doctype, "%s"), (name,))
  31. for t in tablefields:
  32. if t[0] not in ignore_doctypes:
  33. webnotes.conn.sql("delete from `tab%s` where parent = %s" % (t[0], '%s'), (name,))
  34. except Exception, e:
  35. if e.args[0]==1451:
  36. webnotes.msgprint("Cannot delete %s '%s' as it is referenced in another record. You must delete the referred record first" % (doctype, name))
  37. raise
  38. # delete attachments
  39. remove_all(doctype, name)
  40. # delete restrictions
  41. webnotes.defaults.clear_default(parenttype="Restriction", key=doctype, value=name)
  42. return 'okay'
  43. def check_permission_and_not_submitted(doctype, name, ignore_permissions=False):
  44. # permission
  45. if not ignore_permissions and webnotes.session.user!="Administrator" and not webnotes.has_permission(doctype, "cancel"):
  46. webnotes.msgprint(_("User not allowed to delete."), raise_exception=True)
  47. # check if submitted
  48. if webnotes.conn.get_value(doctype, name, "docstatus") == 1:
  49. webnotes.msgprint(_("Submitted Record cannot be deleted")+": "+name+"("+doctype+")",
  50. raise_exception=True)
  51. def run_on_trash(doctype, name, doclist):
  52. # call on_trash if required
  53. if doclist:
  54. bean = webnotes.bean(doclist)
  55. else:
  56. bean = webnotes.bean(doctype, name)
  57. bean.run_method("on_trash")
  58. class LinkExistsError(webnotes.ValidationError): pass
  59. def check_if_doc_is_linked(dt, dn, method="Delete"):
  60. """
  61. Raises excption if the given doc(dt, dn) is linked in another record.
  62. """
  63. from webnotes.model.rename_doc import get_link_fields
  64. link_fields = get_link_fields(dt)
  65. link_fields = [[lf['parent'], lf['fieldname'], lf['issingle']] for lf in link_fields]
  66. for link_dt, link_field, issingle in link_fields:
  67. if not issingle:
  68. item = webnotes.conn.get_value(link_dt, {link_field:dn},
  69. ["name", "parent", "parenttype", "docstatus"], as_dict=True)
  70. if item and item.parent != dn and (method=="Delete" or
  71. (method=="Cancel" and item.docstatus==1)):
  72. webnotes.msgprint(method + " " + _("Error") + ":"+\
  73. ("%s (%s) " % (dn, dt)) + _("is linked in") + (" %s (%s)") %
  74. (item.parent or item.name, item.parent and item.parenttype or link_dt),
  75. raise_exception=LinkExistsError)