ソースを参照

[cleanup] [minor] added webnotes.model.delete_doc (module)

version-14
Rushabh Mehta 11年前
committed by Anand Doshi
コミット
6c2922ddf0
8個のファイルの変更102行の追加108行の削除
  1. +1
    -1
      core/doctype/defaultvalue/defaultvalue.py
  2. +1
    -1
      core/doctype/profile/test_profile.py
  3. +7
    -4
      webnotes/__init__.py
  4. +0
    -5
      webnotes/model/__init__.py
  5. +1
    -1
      webnotes/model/bean.py
  6. +91
    -0
      webnotes/model/delete_doc.py
  7. +0
    -94
      webnotes/model/utils.py
  8. +1
    -2
      webnotes/widgets/reportview.py

+ 1
- 1
core/doctype/defaultvalue/defaultvalue.py ファイルの表示

@@ -19,4 +19,4 @@ def on_doctype_update():
where Key_name="defaultvalue_parent_parenttype_index" """):
webnotes.conn.commit()
webnotes.conn.sql("""alter table `tabDefaultValue`
add index defaultvalue_parent_parenttype_index(parent, parentttype)""")
add index defaultvalue_parent_parenttype_index(parent, parenttype)""")

+ 1
- 1
core/doctype/profile/test_profile.py ファイルの表示

@@ -3,7 +3,7 @@

import webnotes, unittest

from webnotes.model.utils import delete_doc, LinkExistsError
from webnotes.model.delete_doc import delete_doc, LinkExistsError

class TestProfile(unittest.TestCase):
def test_delete(self):


+ 7
- 4
webnotes/__init__.py ファイルの表示

@@ -431,17 +431,20 @@ def get_doctype(doctype, processed=False):
import webnotes.model.doctype
return webnotes.model.doctype.get(doctype, processed)

def delete_doc(doctype=None, name=None, doclist = None, force=0, ignore_doctypes=None, for_reload=False, ignore_permissions=False):
import webnotes.model.utils
def delete_doc(doctype=None, name=None, doclist = None, force=0, ignore_doctypes=None,
for_reload=False, ignore_permissions=False):
import webnotes.model.delete_doc

if not ignore_doctypes:
ignore_doctypes = []
if isinstance(name, list):
for n in name:
webnotes.model.utils.delete_doc(doctype, n, doclist, force, ignore_doctypes, for_reload, ignore_permissions)
webnotes.model.delete_doc.delete_doc(doctype, n, doclist, force, ignore_doctypes,
for_reload, ignore_permissions)
else:
webnotes.model.utils.delete_doc(doctype, name, doclist, force, ignore_doctypes, for_reload, ignore_permissions)
webnotes.model.delete_doc.delete_doc(doctype, name, doclist, force, ignore_doctypes,
for_reload, ignore_permissions)

def clear_perms(doctype):
conn.sql("""delete from tabDocPerm where parent=%s""", doctype)


+ 0
- 5
webnotes/model/__init__.py ファイルの表示

@@ -23,11 +23,6 @@ def insert(doclist):
return wrapper

@webnotes.whitelist()
def delete_doc(doctype=None, name=None, doclist = None, force=0):
import webnotes.model.utils
return webnotes.model.utils.delete_doc(doctype, name, doclist, force)
def rename(doctype, old, new, debug=False):
import webnotes.model.rename_doc
webnotes.model.rename_doc.rename_doc(doctype, old, new, debug)


+ 1
- 1
webnotes/model/bean.py ファイルの表示

@@ -400,7 +400,7 @@ class Bean:
_("No Permission to ") + ptype, raise_exception=BeanPermissionError)
def check_no_back_links_exist(self):
from webnotes.model.utils import check_if_doc_is_linked
from webnotes.model.delete_doc import check_if_doc_is_linked
check_if_doc_is_linked(self.doc.doctype, self.doc.name, method="Cancel")
def check_mandatory(self):


+ 91
- 0
webnotes/model/delete_doc.py ファイルの表示

@@ -0,0 +1,91 @@
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals

import webnotes
import webnotes.model.meta

def delete_doc(doctype=None, name=None, doclist = None, force=0, ignore_doctypes=[], for_reload=False, ignore_permissions=False):
"""
Deletes a doc(dt, dn) and validates if it is not submitted and not linked in a live record
"""

# get from form
if not doctype:
doctype = webnotes.form_dict.get('dt')
name = webnotes.form_dict.get('dn')
if not doctype:
webnotes.msgprint('Nothing to delete!', raise_exception =1)

# already deleted..?
if not webnotes.conn.exists(doctype, name):
return

if not for_reload:
check_permission_and_not_submitted(doctype, name, ignore_permissions)
run_on_trash(doctype, name, doclist)
# check if links exist
if not force:
check_if_doc_is_linked(doctype, name)
try:
tablefields = webnotes.model.meta.get_table_fields(doctype)
webnotes.conn.sql("delete from `tab%s` where name=%s" % (doctype, "%s"), name)
for t in tablefields:
if t[0] not in ignore_doctypes:
webnotes.conn.sql("delete from `tab%s` where parent = %s" % (t[0], '%s'), name)
except Exception, e:
if e.args[0]==1451:
webnotes.msgprint("Cannot delete %s '%s' as it is referenced in another record. You must delete the referred record first" % (doctype, name))
raise
# delete attachments
from webnotes.utils.file_manager import remove_all
remove_all(doctype, name)
return 'okay'

def check_permission_and_not_submitted(doctype, name, ignore_permissions=False):
# permission
if not ignore_permissions and webnotes.session.user!="Administrator" and not webnotes.has_permission(doctype, "cancel"):
webnotes.msgprint(_("User not allowed to delete."), raise_exception=True)

# check if submitted
if webnotes.conn.get_value(doctype, name, "docstatus") == 1:
webnotes.msgprint(_("Submitted Record cannot be deleted")+": "+name+"("+doctype+")",
raise_exception=True)

def run_on_trash(doctype, name, doclist):
# call on_trash if required
if doclist:
bean = webnotes.bean(doclist)
else:
bean = webnotes.bean(doctype, name)
bean.run_method("on_trash")

class LinkExistsError(webnotes.ValidationError): pass

def check_if_doc_is_linked(dt, dn, method="Delete"):
"""
Raises excption if the given doc(dt, dn) is linked in another record.
"""
from webnotes.model.rename_doc import get_link_fields
link_fields = get_link_fields(dt)
link_fields = [[lf['parent'], lf['fieldname'], lf['issingle']] for lf in link_fields]

for link_dt, link_field, issingle in link_fields:
if not issingle:
item = webnotes.conn.get_value(link_dt, {link_field:dn},
["name", "parent", "parenttype", "docstatus"], as_dict=True)
if item and item.parent != dn and (method=="Delete" or
(method=="Cancel" and item.docstatus==1)):
webnotes.msgprint(method + " " + _("Error") + ":"+\
("%s (%s) " % (dn, dt)) + _("is linked in") + (" %s (%s)") %
(item.parent or item.name, item.parent and item.parenttype or link_dt),
raise_exception=LinkExistsError)

+ 0
- 94
webnotes/model/utils.py ファイルの表示

@@ -113,100 +113,6 @@ def copy_doclist(doclist, no_copy = []):

return cl

def getvaluelist(doclist, fieldname):
"""
Returns a list of values of a particualr fieldname from all Document object in a doclist
"""
l = []
for d in doclist:
l.append(d.fields[fieldname])
return l

def delete_doc(doctype=None, name=None, doclist = None, force=0, ignore_doctypes=[], for_reload=False, ignore_permissions=False):
"""
Deletes a doc(dt, dn) and validates if it is not submitted and not linked in a live record
"""
import webnotes.model.meta

# get from form
if not doctype:
doctype = webnotes.form_dict.get('dt')
name = webnotes.form_dict.get('dn')
if not doctype:
webnotes.msgprint('Nothing to delete!', raise_exception =1)

# already deleted..?
if not webnotes.conn.exists(doctype, name):
return

if not for_reload:
check_permission_and_not_submitted(doctype, name, ignore_permissions)
run_on_trash(doctype, name, doclist)
# check if links exist
if not force:
check_if_doc_is_linked(doctype, name)
try:
tablefields = webnotes.model.meta.get_table_fields(doctype)
webnotes.conn.sql("delete from `tab%s` where name=%s" % (doctype, "%s"), name)
for t in tablefields:
if t[0] not in ignore_doctypes:
webnotes.conn.sql("delete from `tab%s` where parent = %s" % (t[0], '%s'), name)
except Exception, e:
if e.args[0]==1451:
webnotes.msgprint("Cannot delete %s '%s' as it is referenced in another record. You must delete the referred record first" % (doctype, name))
raise
# delete attachments
from webnotes.utils.file_manager import remove_all
remove_all(doctype, name)
return 'okay'

def check_permission_and_not_submitted(doctype, name, ignore_permissions=False):
# permission
if not ignore_permissions and webnotes.session.user!="Administrator" and not webnotes.has_permission(doctype, "cancel"):
webnotes.msgprint(_("User not allowed to delete."), raise_exception=True)

# check if submitted
if webnotes.conn.get_value(doctype, name, "docstatus") == 1:
webnotes.msgprint(_("Submitted Record cannot be deleted")+": "+name+"("+doctype+")",
raise_exception=True)

def run_on_trash(doctype, name, doclist):
# call on_trash if required
if doclist:
bean = webnotes.bean(doclist)
else:
bean = webnotes.bean(doctype, name)
bean.run_method("on_trash")

class LinkExistsError(webnotes.ValidationError): pass

def check_if_doc_is_linked(dt, dn, method="Delete"):
"""
Raises excption if the given doc(dt, dn) is linked in another record.
"""
from webnotes.model.rename_doc import get_link_fields
link_fields = get_link_fields(dt)
link_fields = [[lf['parent'], lf['fieldname'], lf['issingle']] for lf in link_fields]

for link_dt, link_field, issingle in link_fields:
if not issingle:
item = webnotes.conn.get_value(link_dt, {link_field:dn},
["name", "parent", "parenttype", "docstatus"], as_dict=True)
if item and item.parent != dn and (method=="Delete" or
(method=="Cancel" and item.docstatus==1)):
webnotes.msgprint(method + " " + _("Error") + ":"+\
("%s (%s) " % (dn, dt)) + _("is linked in") + (" %s (%s)") %
(item.parent or item.name, item.parent and item.parenttype or link_dt),
raise_exception=LinkExistsError)

def set_default(doc, key):
if not doc.is_default:
webnotes.conn.set(doc, "is_default", 1)


+ 1
- 2
webnotes/widgets/reportview.py ファイルの表示

@@ -333,7 +333,6 @@ def get_labels(columns):
def delete_items():
"""delete selected items"""
import json
from webnotes.model import delete_doc
from webnotes.model.code import get_obj

il = json.loads(webnotes.form_dict.get('items'))
@@ -344,7 +343,7 @@ def delete_items():
dt_obj = get_obj(doctype, d)
if hasattr(dt_obj, 'on_trash'):
dt_obj.on_trash()
delete_doc(doctype, d)
webnotes.delete_doc(doctype, d)
except Exception, e:
webnotes.errprint(webnotes.getTraceback())
pass


読み込み中…
キャンセル
保存