<s> from __future__ import unicode_literals <EOL> import frappe <EOL> from frappe . desk . notifications import clear_notifications <EOL> common_keys = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def set_user_default ( key , value , user = None , parenttype = None ) : <EOL> set_default ( key , value , user or frappe . session . user , parenttype ) <EOL> def add_user_default ( key , value , user = None , parenttype = None ) : <EOL> add_default ( key , value , user or frappe . session . user , parenttype ) <EOL> def get_user_default ( key , user = None ) : <EOL> user_defaults = get_defaults ( user or frappe . session . user ) <EOL> d = user_defaults . get ( key , None ) <EOL> if is_a_user_permission_key ( key ) : <EOL> if d and isinstance ( d , ( list , tuple ) ) and len ( d ) == <NUM_LIT:1> : <EOL> d = d [ <NUM_LIT:0> ] <EOL> else : <EOL> d = user_defaults . get ( frappe . scrub ( key ) , None ) <EOL> return isinstance ( d , ( list , tuple ) ) and d [ <NUM_LIT:0> ] or d <EOL> def get_user_default_as_list ( key , user = None ) : <EOL> user_defaults = get_defaults ( user or frappe . session . user ) <EOL> d = user_defaults . get ( key , None ) <EOL> if is_a_user_permission_key ( key ) : <EOL> if d and isinstance ( d , ( list , tuple ) ) and len ( d ) == <NUM_LIT:1> : <EOL> d = [ d [ <NUM_LIT:0> ] ] <EOL> else : <EOL> d = user_defaults . get ( frappe . scrub ( key ) , None ) <EOL> return filter ( None , ( not isinstance ( d , ( list , tuple ) ) ) and [ d ] or d ) <EOL> def is_a_user_permission_key ( key ) : <EOL> return "<STR_LIT::>" not in key and key != frappe . scrub ( key ) <EOL> def get_user_permissions ( user = None ) : <EOL> if not user : <EOL> user = frappe . session . user <EOL> return build_user_permissions ( user ) <EOL> def build_user_permissions ( user ) : <EOL> out = frappe . cache ( ) . hget ( "<STR_LIT>" , user ) <EOL> if out == None : <EOL> out = { } <EOL> for key , value in frappe . db . sql ( """<STR_LIT>""" , ( user , ) ) : <EOL> out . setdefault ( key , [ ] ) . append ( value ) <EOL> if user not in out . get ( "<STR_LIT>" , [ ] ) : <EOL> out . setdefault ( "<STR_LIT>" , [ ] ) . append ( user ) <EOL> frappe . cache ( ) . hset ( "<STR_LIT>" , user , out ) <EOL> return out <EOL> def get_defaults ( user = None ) : <EOL> globald = get_defaults_for ( ) <EOL> if not user : <EOL> user = frappe . session . user if frappe . session else "<STR_LIT>" <EOL> if user : <EOL> userd = { } <EOL> userd . update ( get_defaults_for ( user ) ) <EOL> userd . update ( { "<STR_LIT:user>" : user , "<STR_LIT>" : user } ) <EOL> globald . update ( userd ) <EOL> return globald <EOL> def clear_user_default ( key , user = None ) : <EOL> clear_default ( key , parent = user or frappe . session . user ) <EOL> def set_global_default ( key , value ) : <EOL> set_default ( key , value , "<STR_LIT>" ) <EOL> def add_global_default ( key , value ) : <EOL> add_default ( key , value , "<STR_LIT>" ) <EOL> def get_global_default ( key ) : <EOL> d = get_defaults ( ) . get ( key , None ) <EOL> return isinstance ( d , list ) and d [ <NUM_LIT:0> ] or d <EOL> def set_default ( key , value , parent , parenttype = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> frappe . db . sql ( """<STR_LIT>""" , ( key , parent ) ) <EOL> add_default ( key , value , parent ) <EOL> def add_default ( key , value , parent , parenttype = None ) : <EOL> d = frappe . get_doc ( { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : parent , <EOL> "<STR_LIT>" : parenttype or "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : key , <EOL> "<STR_LIT>" : value <EOL> } ) <EOL> d . insert ( ignore_permissions = True ) <EOL> _clear_cache ( parent ) <EOL> def clear_default ( key = None , value = None , parent = None , name = None , parenttype = None ) : <EOL> """<STR_LIT>""" <EOL> conditions = [ ] <EOL> values = [ ] <EOL> if name : <EOL> conditions . append ( "<STR_LIT>" ) <EOL> values . append ( name ) <EOL> else : <EOL> if key : <EOL> conditions . append ( "<STR_LIT>" ) <EOL> values . append ( key ) <EOL> if value : <EOL> conditions . append ( "<STR_LIT>" ) <EOL> values . append ( value ) <EOL> if parent : <EOL> conditions . append ( "<STR_LIT>" ) <EOL> values . append ( parent ) <EOL> if parenttype : <EOL> conditions . append ( "<STR_LIT>" ) <EOL> values . append ( parenttype ) <EOL> if parent : <EOL> clear_cache ( parent ) <EOL> else : <EOL> clear_cache ( "<STR_LIT>" ) <EOL> clear_cache ( "<STR_LIT>" ) <EOL> if not conditions : <EOL> raise Exception , "<STR_LIT>" <EOL> frappe . db . sql ( """<STR_LIT>""" . format ( "<STR_LIT>" . join ( conditions ) ) , <EOL> tuple ( values ) ) <EOL> _clear_cache ( parent ) <EOL> def get_defaults_for ( parent = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> defaults = frappe . cache ( ) . hget ( "<STR_LIT>" , parent ) <EOL> if defaults == None : <EOL> res = frappe . db . sql ( """<STR_LIT>""" , ( parent , ) , as_dict = <NUM_LIT:1> ) <EOL> defaults = frappe . _dict ( { } ) <EOL> for d in res : <EOL> if d . defkey in defaults : <EOL> if not isinstance ( defaults [ d . defkey ] , list ) and defaults [ d . defkey ] != d . defvalue : <EOL> defaults [ d . defkey ] = [ defaults [ d . defkey ] ] <EOL> if d . defvalue not in defaults [ d . defkey ] : <EOL> defaults [ d . defkey ] . append ( d . defvalue ) <EOL> elif d . defvalue is not None : <EOL> defaults [ d . defkey ] = d . defvalue <EOL> frappe . cache ( ) . hset ( "<STR_LIT>" , parent , defaults ) <EOL> return defaults <EOL> def _clear_cache ( parent ) : <EOL> if parent in common_keys : <EOL> frappe . clear_cache ( ) <EOL> else : <EOL> clear_notifications ( user = parent ) <EOL> frappe . clear_cache ( user = parent ) <EOL> def clear_cache ( user = None ) : <EOL> if user : <EOL> for p in ( [ user ] + common_keys ) : <EOL> frappe . cache ( ) . hdel ( "<STR_LIT>" , p ) <EOL> elif frappe . flags . in_install != "<STR_LIT>" : <EOL> frappe . cache ( ) . delete_key ( "<STR_LIT>" ) </s>
