<s> from __future__ import unicode_literals <EOL> import frappe <EOL> import frappe . model . meta <EOL> from frappe . model . dynamic_links import get_dynamic_link_map <EOL> import frappe . defaults <EOL> from frappe . utils . file_manager import remove_all <EOL> from frappe import _ <EOL> from frappe . model . naming import revert_series_if_last <EOL> def delete_doc ( doctype = None , name = None , force = <NUM_LIT:0> , ignore_doctypes = None , for_reload = False , <EOL> ignore_permissions = False , flags = None , ignore_on_trash = False ) : <EOL> """<STR_LIT>""" <EOL> if not ignore_doctypes : ignore_doctypes = [ ] <EOL> if not doctype : <EOL> doctype = frappe . form_dict . get ( '<STR_LIT>' ) <EOL> name = frappe . form_dict . get ( '<STR_LIT>' ) <EOL> names = name <EOL> if isinstance ( name , basestring ) : <EOL> names = [ name ] <EOL> for name in names or [ ] : <EOL> if not frappe . db . exists ( doctype , name ) : <EOL> return <EOL> remove_all ( doctype , name ) <EOL> doc = None <EOL> if doctype == "<STR_LIT>" : <EOL> if for_reload : <EOL> try : <EOL> doc = frappe . get_doc ( doctype , name ) <EOL> except frappe . DoesNotExistError : <EOL> pass <EOL> else : <EOL> doc . run_method ( "<STR_LIT>" ) <EOL> else : <EOL> frappe . db . sql ( "<STR_LIT>" , name ) <EOL> frappe . db . sql ( "<STR_LIT>" , name ) <EOL> frappe . db . sql ( "<STR_LIT>" , name ) <EOL> frappe . db . sql ( "<STR_LIT>" , name ) <EOL> delete_from_table ( doctype , name , ignore_doctypes , None ) <EOL> else : <EOL> doc = frappe . get_doc ( doctype , name ) <EOL> if not for_reload : <EOL> if ignore_permissions : <EOL> if not flags : flags = { } <EOL> flags [ "<STR_LIT>" ] = ignore_permissions <EOL> if flags : <EOL> doc . flags . update ( flags ) <EOL> check_permission_and_not_submitted ( doc ) <EOL> if not ignore_on_trash : <EOL> doc . run_method ( "<STR_LIT>" ) <EOL> doc . run_method ( '<STR_LIT>' ) <EOL> dynamic_linked_doctypes = [ df . parent for df in get_dynamic_link_map ( ) . get ( doc . doctype , [ ] ) ] <EOL> if "<STR_LIT>" in dynamic_linked_doctypes : <EOL> delete_linked_todos ( doc ) <EOL> if "<STR_LIT>" in dynamic_linked_doctypes : <EOL> delete_linked_communications ( doc ) <EOL> if "<STR_LIT>" in dynamic_linked_doctypes : <EOL> delete_shared ( doc ) <EOL> if "<STR_LIT>" in dynamic_linked_doctypes : <EOL> delete_email_subscribe ( doc ) <EOL> if not force : <EOL> check_if_doc_is_linked ( doc ) <EOL> check_if_doc_is_dynamically_linked ( doc ) <EOL> update_naming_series ( doc ) <EOL> delete_from_table ( doctype , name , ignore_doctypes , doc ) <EOL> doc . run_method ( "<STR_LIT>" ) <EOL> if doc : <EOL> try : <EOL> doc . notify_update ( ) <EOL> insert_feed ( doc ) <EOL> except ImportError : <EOL> pass <EOL> frappe . defaults . clear_default ( parenttype = "<STR_LIT>" , key = doctype , value = name ) <EOL> def update_naming_series ( doc ) : <EOL> if doc . meta . autoname : <EOL> if doc . meta . autoname . startswith ( "<STR_LIT>" ) and getattr ( doc , "<STR_LIT>" , None ) : <EOL> revert_series_if_last ( doc . naming_series , doc . name ) <EOL> elif doc . meta . autoname . split ( "<STR_LIT::>" ) [ <NUM_LIT:0> ] not in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> revert_series_if_last ( doc . meta . autoname , doc . name ) <EOL> def delete_from_table ( doctype , name , ignore_doctypes , doc ) : <EOL> if doctype != "<STR_LIT>" and doctype == name : <EOL> frappe . db . sql ( "<STR_LIT>" , name ) <EOL> else : <EOL> frappe . db . sql ( "<STR_LIT>" % ( frappe . db . escape ( doctype ) , "<STR_LIT:%s>" ) , ( name , ) ) <EOL> if doc : <EOL> tables = [ d . options for d in doc . meta . get_table_fields ( ) ] <EOL> else : <EOL> def get_table_fields ( field_doctype ) : <EOL> return frappe . db . sql_list ( """<STR_LIT>""" . format ( field_doctype ) , doctype ) <EOL> tables = get_table_fields ( "<STR_LIT>" ) <EOL> if not frappe . flags . in_install == "<STR_LIT>" : <EOL> tables += get_table_fields ( "<STR_LIT>" ) <EOL> for t in list ( set ( tables ) ) : <EOL> if t not in ignore_doctypes : <EOL> frappe . db . sql ( "<STR_LIT>" % ( t , '<STR_LIT:%s>' , '<STR_LIT:%s>' ) , ( doctype , name ) ) <EOL> def check_permission_and_not_submitted ( doc ) : <EOL> if not doc . flags . ignore_permissions and frappe . session . user != "<STR_LIT>" and not doc . has_permission ( "<STR_LIT>" ) : <EOL> frappe . msgprint ( _ ( "<STR_LIT>" ) . format ( doc . doctype , doc . name ) , raise_exception = True ) <EOL> if doc . docstatus == <NUM_LIT:1> : <EOL> frappe . msgprint ( _ ( "<STR_LIT>" ) . format ( doc . doctype , doc . name ) , <EOL> raise_exception = True ) <EOL> def check_if_doc_is_linked ( doc , method = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> from frappe . model . rename_doc import get_link_fields <EOL> link_fields = get_link_fields ( doc . doctype ) <EOL> link_fields = [ [ lf [ '<STR_LIT>' ] , lf [ '<STR_LIT>' ] , lf [ '<STR_LIT>' ] ] for lf in link_fields ] <EOL> for link_dt , link_field , issingle in link_fields : <EOL> if not issingle : <EOL> item = frappe . db . get_value ( link_dt , { link_field : doc . name } , <EOL> [ "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , as_dict = True ) <EOL> if item and ( ( item . parent or item . name ) != doc . name ) and ( ( method == "<STR_LIT>" and item . docstatus < <NUM_LIT:2> ) or ( method == "<STR_LIT>" and item . docstatus == <NUM_LIT:1> ) ) : <EOL> frappe . throw ( _ ( "<STR_LIT>" ) <EOL> . format ( doc . doctype , doc . name , item . parenttype if item . parent else link_dt , <EOL> item . parent or item . name ) , frappe . LinkExistsError ) <EOL> def check_if_doc_is_dynamically_linked ( doc , method = "<STR_LIT>" ) : <EOL> '''<STR_LIT>''' <EOL> for df in get_dynamic_link_map ( ) . get ( doc . doctype , [ ] ) : <EOL> if df . parent in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> continue <EOL> meta = frappe . get_meta ( df . parent ) <EOL> if meta . issingle : <EOL> refdoc = frappe . db . get_singles_dict ( df . parent ) <EOL> if ( refdoc . get ( df . options ) == doc . doctype <EOL> and refdoc . get ( df . fieldname ) == doc . name <EOL> and ( ( method == "<STR_LIT>" and refdoc . docstatus < <NUM_LIT:2> ) <EOL> or ( method == "<STR_LIT>" and refdoc . docstatus == <NUM_LIT:1> ) ) <EOL> ) : <EOL> frappe . throw ( _ ( "<STR_LIT>" ) . format ( doc . doctype , <EOL> doc . name , df . parent , "<STR_LIT>" ) , frappe . LinkExistsError ) <EOL> else : <EOL> for refdoc in frappe . db . sql ( """<STR_LIT>""" . format ( ** df ) , ( doc . doctype , doc . name ) , as_dict = True ) : <EOL> if ( ( method == "<STR_LIT>" and refdoc . docstatus < <NUM_LIT:2> ) or ( method == "<STR_LIT>" and refdoc . docstatus == <NUM_LIT:1> ) ) : <EOL> frappe . throw ( _ ( "<STR_LIT>" ) . format ( doc . doctype , doc . name , df . parent , refdoc . name ) , frappe . LinkExistsError ) <EOL> def delete_linked_todos ( doc ) : <EOL> delete_doc ( "<STR_LIT>" , frappe . db . sql_list ( """<STR_LIT>""" , ( doc . doctype , doc . name ) ) , <EOL> ignore_permissions = True ) <EOL> def delete_email_subscribe ( doc ) : <EOL> frappe . db . sql ( '''<STR_LIT>''' , ( doc . doctype , doc . name ) ) <EOL> def delete_linked_communications ( doc ) : <EOL> frappe . db . sql ( """<STR_LIT>""" , ( doc . doctype , doc . name ) ) <EOL> frappe . db . sql ( """<STR_LIT>""" , ( doc . doctype , doc . name ) ) <EOL> frappe . db . sql ( """<STR_LIT>""" , ( doc . doctype , doc . name ) ) <EOL> frappe . db . sql ( """<STR_LIT>""" , ( doc . doctype , doc . name ) ) <EOL> def insert_feed ( doc ) : <EOL> from frappe . utils import get_fullname <EOL> if frappe . flags . in_install or frappe . flags . in_import or getattr ( doc , "<STR_LIT>" , False ) : <EOL> return <EOL> frappe . get_doc ( { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : doc . doctype , <EOL> "<STR_LIT>" : "<STR_LIT>" . format ( _ ( doc . doctype ) , doc . name ) , <EOL> "<STR_LIT>" : get_fullname ( doc . owner ) <EOL> } ) . insert ( ignore_permissions = True ) <EOL> def delete_shared ( doc ) : <EOL> delete_doc ( "<STR_LIT>" , frappe . db . sql_list ( """<STR_LIT>""" , ( doc . doctype , doc . name ) ) , ignore_on_trash = True ) </s>
