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.
 
 
 
 
 
 

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