<s> from __future__ import unicode_literals <EOL> import frappe <EOL> from frappe import _ <EOL> from frappe . utils import now_datetime , cint <EOL> import re <EOL> def set_new_name ( doc ) : <EOL> """<STR_LIT>""" <EOL> doc . run_method ( "<STR_LIT>" ) <EOL> autoname = frappe . get_meta ( doc . doctype ) . autoname <EOL> if getattr ( doc , "<STR_LIT>" , None ) : <EOL> _set_amended_name ( doc ) <EOL> return <EOL> elif getattr ( doc . meta , "<STR_LIT>" , False ) : <EOL> doc . name = doc . doctype <EOL> elif hasattr ( doc , "<STR_LIT>" ) : <EOL> doc . run_method ( "<STR_LIT>" ) <EOL> elif autoname : <EOL> if autoname . startswith ( '<STR_LIT>' ) : <EOL> fieldname = autoname [ <NUM_LIT:6> : ] <EOL> doc . name = ( doc . get ( fieldname ) or "<STR_LIT>" ) . strip ( ) <EOL> if not doc . name : <EOL> frappe . throw ( _ ( "<STR_LIT>" ) . format ( doc . meta . get_label ( fieldname ) ) ) <EOL> raise Exception , '<STR_LIT>' <EOL> if autoname . startswith ( "<STR_LIT>" ) : <EOL> set_name_by_naming_series ( doc ) <EOL> elif "<STR_LIT:#>" in autoname : <EOL> doc . name = make_autoname ( autoname ) <EOL> elif autoname == '<STR_LIT>' : <EOL> if not doc . name : <EOL> frappe . throw ( _ ( "<STR_LIT>" ) ) <EOL> if not doc . name or autoname == '<STR_LIT>' : <EOL> doc . name = make_autoname ( '<STR_LIT>' , doc . doctype ) <EOL> doc . name = validate_name ( doc . doctype , doc . name ) <EOL> def set_name_by_naming_series ( doc ) : <EOL> """<STR_LIT>""" <EOL> if not doc . naming_series : <EOL> doc . naming_series = get_default_naming_series ( doc . doctype ) <EOL> if not doc . naming_series : <EOL> frappe . throw ( frappe . _ ( "<STR_LIT>" ) ) <EOL> doc . name = make_autoname ( doc . naming_series + '<STR_LIT>' , '<STR_LIT>' , doc ) <EOL> def make_autoname ( key = '<STR_LIT>' , doctype = '<STR_LIT>' , doc = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if key == "<STR_LIT>" : <EOL> return frappe . generate_hash ( doctype , <NUM_LIT:10> ) <EOL> if not "<STR_LIT:#>" in key : <EOL> key = key + "<STR_LIT>" <EOL> elif not "<STR_LIT:.>" in key : <EOL> frappe . throw ( _ ( "<STR_LIT>" ) + ( _ ( "<STR_LIT>" ) . format ( doctype ) if doctype else "<STR_LIT>" ) ) <EOL> n = '<STR_LIT>' <EOL> l = key . split ( '<STR_LIT:.>' ) <EOL> series_set = False <EOL> today = now_datetime ( ) <EOL> for e in l : <EOL> part = '<STR_LIT>' <EOL> if e . startswith ( '<STR_LIT:#>' ) : <EOL> if not series_set : <EOL> digits = len ( e ) <EOL> part = getseries ( n , digits , doctype ) <EOL> series_set = True <EOL> elif e == '<STR_LIT>' : <EOL> part = today . strftime ( '<STR_LIT>' ) <EOL> elif e == '<STR_LIT>' : <EOL> part = today . strftime ( '<STR_LIT>' ) <EOL> elif e == '<STR_LIT>' : <EOL> part = today . strftime ( "<STR_LIT>" ) <EOL> elif e == '<STR_LIT>' : <EOL> part = today . strftime ( '<STR_LIT>' ) <EOL> elif doc and doc . get ( e ) : <EOL> part = doc . get ( e ) <EOL> else : part = e <EOL> if isinstance ( part , basestring ) : <EOL> n += part <EOL> return n <EOL> def getseries ( key , digits , doctype = '<STR_LIT>' ) : <EOL> current = frappe . db . sql ( "<STR_LIT>" , ( key , ) ) <EOL> if current and current [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] is not None : <EOL> current = current [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> frappe . db . sql ( "<STR_LIT>" , ( key , ) ) <EOL> current = cint ( current ) + <NUM_LIT:1> <EOL> else : <EOL> frappe . db . sql ( "<STR_LIT>" , ( key , ) ) <EOL> current = <NUM_LIT:1> <EOL> return ( '<STR_LIT>' + str ( digits ) + '<STR_LIT:d>' ) % current <EOL> def revert_series_if_last ( key , name ) : <EOL> if "<STR_LIT>" in key : <EOL> prefix , hashes = key . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> if "<STR_LIT:#>" not in hashes : <EOL> return <EOL> else : <EOL> prefix = key <EOL> count = cint ( name . replace ( prefix , "<STR_LIT>" ) ) <EOL> current = frappe . db . sql ( "<STR_LIT>" , ( prefix , ) ) <EOL> if current and current [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == count : <EOL> frappe . db . sql ( "<STR_LIT>" , prefix ) <EOL> def get_default_naming_series ( doctype ) : <EOL> """<STR_LIT>""" <EOL> naming_series = frappe . get_meta ( doctype ) . get_field ( "<STR_LIT>" ) . options or "<STR_LIT>" <EOL> if naming_series : <EOL> naming_series = naming_series . split ( "<STR_LIT:\n>" ) <EOL> return naming_series [ <NUM_LIT:0> ] or naming_series [ <NUM_LIT:1> ] <EOL> else : <EOL> return None <EOL> def validate_name ( doctype , name , case = None , merge = False ) : <EOL> if not name : return '<STR_LIT>' % doctype <EOL> if name . startswith ( '<STR_LIT>' + doctype ) : <EOL> frappe . throw ( _ ( '<STR_LIT>' ) , frappe . NameError ) <EOL> if case == '<STR_LIT>' : name = name . title ( ) <EOL> if case == '<STR_LIT>' : name = name . upper ( ) <EOL> name = name . strip ( ) <EOL> if not frappe . get_meta ( doctype ) . get ( "<STR_LIT>" ) and ( doctype == name ) and ( name != "<STR_LIT>" ) : <EOL> frappe . throw ( _ ( "<STR_LIT>" ) . format ( doctype , name ) , frappe . NameError ) <EOL> special_characters = "<STR_LIT>" <EOL> if re . findall ( "<STR_LIT>" . format ( special_characters ) , name ) : <EOL> message = "<STR_LIT:U+002CU+0020>" . join ( "<STR_LIT>" . format ( c ) for c in special_characters ) <EOL> frappe . throw ( _ ( "<STR_LIT>" ) . format ( message ) , frappe . NameError ) <EOL> return name <EOL> def _set_amended_name ( doc ) : <EOL> am_id = <NUM_LIT:1> <EOL> am_prefix = doc . amended_from <EOL> if frappe . db . get_value ( doc . doctype , doc . amended_from , "<STR_LIT>" ) : <EOL> am_id = cint ( doc . amended_from . split ( '<STR_LIT:->' ) [ - <NUM_LIT:1> ] ) + <NUM_LIT:1> <EOL> am_prefix = '<STR_LIT:->' . join ( doc . amended_from . split ( '<STR_LIT:->' ) [ : - <NUM_LIT:1> ] ) <EOL> doc . name = am_prefix + '<STR_LIT:->' + str ( am_id ) <EOL> return doc . name <EOL> def append_number_if_name_exists ( doc ) : <EOL> if frappe . db . exists ( doc . doctype , doc . name ) : <EOL> last = frappe . db . sql ( """<STR_LIT>""" . format ( doc . doctype , doc . name ) ) <EOL> if last : <EOL> count = str ( cint ( last [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . rsplit ( "<STR_LIT:->" , <NUM_LIT:1> ) [ <NUM_LIT:1> ] ) + <NUM_LIT:1> ) <EOL> else : <EOL> count = "<STR_LIT:1>" <EOL> doc . name = "<STR_LIT>" . format ( doc . name , count ) <EOL> def de_duplicate ( doctype , name ) : <EOL> original_name = name <EOL> count = <NUM_LIT:0> <EOL> while True : <EOL> if frappe . db . exists ( doctype , name ) : <EOL> count += <NUM_LIT:1> <EOL> name = "<STR_LIT>" . format ( original_name , count ) <EOL> else : <EOL> break <EOL> return name </s>
