<s> from kokoropy import load_view , base_url , request , publish_methods , load_template , application_path , file_get_contents <EOL> from sqlalchemy . ext . declarative import declared_attr <EOL> import math , random , os , json , sys <EOL> from model import DB_Model <EOL> from operator import and_ , or_ <EOL> from kokoropy import var_dump <EOL> class Multi_Language_Controller ( object ) : <EOL> def preprocess_content ( self , content ) : <EOL> return content <EOL> def load_view ( self , application_name , view_name , * args , ** kwargs ) : <EOL> content = load_view ( application_name , view_name , * args , ** kwargs ) <EOL> return self . preprocess_content ( content ) <EOL> class Crud_Controller ( Multi_Language_Controller ) : <EOL> __model__ = None <EOL> __base_view__ = None <EOL> def search_input ( self ) : <EOL> return self . _load_view ( '<STR_LIT>' ) <EOL> def default_criterion ( self ) : <EOL> return self . __model__ . _real_id > <NUM_LIT:0> <EOL> def search_criterion ( self ) : <EOL> q = request . GET [ '<STR_LIT>' ] if '<STR_LIT>' in request . GET else None <EOL> criterion = self . __model__ . _real_id > <NUM_LIT:0> <EOL> if self . __model__ is not None and q is not None : <EOL> q_parts = q . replace ( '<STR_LIT:->' , '<STR_LIT:U+0020>' ) . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) . replace ( '<STR_LIT:/>' , '<STR_LIT:U+0020>' ) . split ( '<STR_LIT:U+0020>' ) <EOL> model_obj = self . __model__ ( ) <EOL> column_names = model_obj . _column_list <EOL> for q in q_parts : <EOL> tmp_criterion = self . __model__ . _real_id < <NUM_LIT:0> <EOL> for column_name in column_names : <EOL> prop = getattr ( self . __model__ , column_name ) <EOL> if column_name in model_obj . _get_relation_names ( ) : <EOL> ref_metadata = model_obj . _get_relation_metadata ( column_name ) <EOL> ref_class = model_obj . _get_relation_class ( column_name ) <EOL> ref_obj = ref_class ( ) <EOL> sub_column_names = ref_obj . _column_list <EOL> for sub_column_name in sub_column_names : <EOL> if sub_column_name in ref_obj . _get_actual_column_names ( ) : <EOL> sub_column = getattr ( ref_class , sub_column_name ) <EOL> if ref_metadata . uselist : <EOL> tmp_criterion = or_ ( tmp_criterion , prop . any ( sub_column . ilike ( q + '<STR_LIT:%>' ) ) ) <EOL> tmp_criterion = or_ ( tmp_criterion , prop . any ( sub_column . ilike ( '<STR_LIT>' + q + '<STR_LIT:%>' ) ) ) <EOL> tmp_criterion = or_ ( tmp_criterion , prop . any ( sub_column . ilike ( '<STR_LIT>' + q + '<STR_LIT:%>' ) ) ) <EOL> tmp_criterion = or_ ( tmp_criterion , prop . any ( sub_column . ilike ( '<STR_LIT>' + q + '<STR_LIT:%>' ) ) ) <EOL> tmp_criterion = or_ ( tmp_criterion , prop . any ( sub_column . ilike ( '<STR_LIT>' + q + '<STR_LIT:%>' , escape = '<STR_LIT:|>' ) ) ) <EOL> else : <EOL> tmp_criterion = or_ ( tmp_criterion , prop . has ( sub_column . ilike ( q + '<STR_LIT:%>' ) ) ) <EOL> tmp_criterion = or_ ( tmp_criterion , prop . has ( sub_column . ilike ( '<STR_LIT>' + q + '<STR_LIT:%>' ) ) ) <EOL> tmp_criterion = or_ ( tmp_criterion , prop . has ( sub_column . ilike ( '<STR_LIT>' + q + '<STR_LIT:%>' ) ) ) <EOL> tmp_criterion = or_ ( tmp_criterion , prop . has ( sub_column . ilike ( '<STR_LIT>' + q + '<STR_LIT:%>' ) ) ) <EOL> tmp_criterion = or_ ( tmp_criterion , prop . has ( sub_column . ilike ( '<STR_LIT>' + q + '<STR_LIT:%>' , escape = '<STR_LIT:|>' ) ) ) <EOL> elif column_name in model_obj . _get_actual_column_names ( ) : <EOL> tmp_criterion = or_ ( tmp_criterion , prop . ilike ( q + '<STR_LIT:%>' ) ) <EOL> tmp_criterion = or_ ( tmp_criterion , prop . ilike ( '<STR_LIT>' + q + '<STR_LIT:%>' ) ) <EOL> tmp_criterion = or_ ( tmp_criterion , prop . ilike ( '<STR_LIT>' + q + '<STR_LIT:%>' ) ) <EOL> tmp_criterion = or_ ( tmp_criterion , prop . ilike ( '<STR_LIT>' + q + '<STR_LIT:%>' ) ) <EOL> tmp_criterion = or_ ( tmp_criterion , prop . ilike ( '<STR_LIT>' + q + '<STR_LIT:%>' , escape = '<STR_LIT:|>' ) ) <EOL> criterion = and_ ( criterion , tmp_criterion ) <EOL> return criterion <EOL> @ declared_attr <EOL> def __application_name__ ( self ) : <EOL> path = sys . modules [ self . __module__ ] . __file__ <EOL> return os . path . dirname ( os . path . dirname ( path ) ) . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> @ declared_attr <EOL> def __url_list__ ( self ) : <EOL> view_list = [ '<STR_LIT:list>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> url = base_url ( self . __application_name__ + '<STR_LIT:/>' + self . __model_name__ ) + '<STR_LIT:/>' <EOL> url_list = { '<STR_LIT:index>' : url } <EOL> for view in view_list : <EOL> url_list [ view ] = url + view <EOL> return url_list <EOL> @ declared_attr <EOL> def __view_directory__ ( self ) : <EOL> if self . __model__ is not None : <EOL> return self . __model__ . __name__ . lower ( ) <EOL> return '<STR_LIT>' <EOL> @ declared_attr <EOL> def __model_name__ ( self ) : <EOL> if self . __model__ is not None : <EOL> return self . __model__ . __name__ . lower ( ) <EOL> return '<STR_LIT>' <EOL> @ declared_attr <EOL> def __caption__ ( self ) : <EOL> if hasattr ( self . __model__ , '<STR_LIT>' ) : <EOL> return self . __model__ . __caption__ <EOL> return '<STR_LIT>' <EOL> @ declared_attr <EOL> def __row_per_page__ ( self ) : <EOL> return <NUM_LIT:5> ; <EOL> @ classmethod <EOL> def publish_route ( cls ) : <EOL> obj = cls ( ) <EOL> obj . _fill_url_list ( ) <EOL> method_names = cls . __url_list__ . keys ( ) <EOL> methods = [ ] <EOL> for method_name in method_names : <EOL> methods . append ( ( method_name , getattr ( obj , method_name ) ) ) <EOL> publish_methods ( cls . __application_name__ , cls . __model_name__ , methods ) <EOL> def _fill_url_list ( self ) : <EOL> view_list = [ '<STR_LIT:list>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> url = base_url ( self . __application_name__ + '<STR_LIT:/>' + self . __model_name__ ) + '<STR_LIT:/>' <EOL> for view in view_list : <EOL> if view not in self . __url_list__ : <EOL> self . __url_list__ [ view ] = url + view <EOL> def _setup_view_parameter ( self ) : <EOL> self . _fill_url_list ( ) <EOL> self . _view_parameter = { <EOL> '<STR_LIT>' : self . __url_list__ , <EOL> '<STR_LIT>' : self . __caption__ , <EOL> '<STR_LIT>' : self . __base_view__ <EOL> } <EOL> def _set_view_parameter ( self , key , value ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _setup_view_parameter ( ) <EOL> self . _view_parameter [ key ] = value <EOL> def _get_view_parameter ( self , key = None ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _setup_view_parameter ( ) <EOL> if key is None : <EOL> return self . _view_parameter <EOL> elif key in self . _view_parameter : <EOL> return self . _view_parameter [ key ] <EOL> def _get_view_parameter_as_json ( self , key = None ) : <EOL> dct = { } <EOL> for key in self . _get_view_parameter ( key ) : <EOL> value = self . _get_view_parameter ( key ) <EOL> if isinstance ( value , DB_Model ) : <EOL> value = value . to_dict ( isoformat = True ) <EOL> elif isinstance ( value , list ) : <EOL> new_value = [ ] <EOL> for item in value : <EOL> if isinstance ( item , DB_Model ) : <EOL> item = item . to_dict ( isoformat = True ) <EOL> elif hasattr ( value , '<STR_LIT>' ) : <EOL> item = item . isoformat ( ) <EOL> new_value . append ( item ) <EOL> value = new_value <EOL> elif hasattr ( value , '<STR_LIT>' ) : <EOL> value = value . isoformat ( ) <EOL> dct [ key ] = value <EOL> if '<STR_LIT:message>' in dct : <EOL> dct [ '<STR_LIT:message>' ] = self . preprocess_content ( dct [ '<STR_LIT:message>' ] ) <EOL> return json . dumps ( dct ) <EOL> def _load_view ( self , view ) : <EOL> if os . path . exists ( application_path ( os . path . join ( self . __application_name__ , '<STR_LIT>' , self . __model_name__ , view ) ) ) : <EOL> content = load_view ( self . __application_name__ , self . __model_name__ + '<STR_LIT:/>' + view , ** self . _get_view_parameter ( ) ) <EOL> else : <EOL> content = '<STR_LIT>' <EOL> content = file_get_contents ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , view + '<STR_LIT>' ) ) <EOL> content = content . replace ( '<STR_LIT>' , self . __model_name__ . title ( ) ) <EOL> content = content . replace ( '<STR_LIT>' , self . __model_name__ ) <EOL> content = content . replace ( '<STR_LIT>' , self . __application_name__ ) <EOL> content = load_template ( content , ** self . _get_view_parameter ( ) ) <EOL> return self . preprocess_content ( content ) <EOL> def load_view ( self , application_name , view_name , * args , ** kwargs ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _setup_view_parameter ( ) <EOL> for key in self . _view_parameter : <EOL> if key not in kwargs : <EOL> kwargs [ key ] = self . _get_view_parameter ( key ) <EOL> content = Multi_Language_Controller . load_view ( self , application_name , view_name , * args , ** kwargs ) <EOL> return content <EOL> def _token_key ( self ) : <EOL> return '<STR_LIT>' + self . __application_name__ + '<STR_LIT:_>' + self . __model_name__ <EOL> def _set_token ( self ) : <EOL> new_token = str . zfill ( str ( random . randrange ( <NUM_LIT:0> , <NUM_LIT> ) ) , <NUM_LIT:5> ) <EOL> tokens = self . _get_token ( ) <EOL> tokens . append ( new_token ) <EOL> request . SESSION [ self . _token_key ( ) ] = '<STR_LIT:U+002C>' . join ( tokens ) <EOL> return new_token <EOL> def _get_token ( self ) : <EOL> if self . _token_key ( ) not in request . SESSION : <EOL> request . SESSION [ self . _token_key ( ) ] = '<STR_LIT>' <EOL> return request . SESSION [ self . _token_key ( ) ] . split ( '<STR_LIT:U+002C>' ) <EOL> def _is_token_match ( self , token ) : <EOL> tokens = self . _get_token ( ) <EOL> match = token in tokens <EOL> if match : <EOL> tokens . remove ( token ) <EOL> request . SESSION [ self . _token_key ( ) ] = '<STR_LIT:U+002C>' . join ( tokens ) <EOL> return match <EOL> def index ( self ) : <EOL> return self . list ( ) <EOL> def list ( self ) : <EOL> '''<STR_LIT>''' <EOL> current_page = int ( request . GET [ '<STR_LIT>' ] if '<STR_LIT>' in request . GET else <NUM_LIT:1> ) <EOL> only_trashed = True if '<STR_LIT>' in request . GET and request . GET [ '<STR_LIT>' ] == '<STR_LIT:1>' else False <EOL> limit = self . __row_per_page__ <EOL> offset = ( current_page - <NUM_LIT:1> ) * limit <EOL> data_list = self . __model__ . get ( and_ ( self . default_criterion ( ) , self . search_criterion ( ) ) , limit = limit , offset = offset , only_trashed = only_trashed ) <EOL> for data in data_list : <EOL> data . set_state_list ( ) <EOL> page_count = int ( math . ceil ( float ( self . __model__ . count ( and_ ( self . default_criterion ( ) , self . search_criterion ( ) ) ) / float ( limit ) ) ) ) <EOL> get_pair = [ ] <EOL> for key in request . GET : <EOL> if key == '<STR_LIT>' or request . GET [ key ] == '<STR_LIT>' : <EOL> continue <EOL> get_pair . append ( key + '<STR_LIT:=>' + str ( request . GET [ key ] ) ) <EOL> get_pair = '<STR_LIT:&>' . join ( get_pair ) <EOL> self . _setup_view_parameter ( ) <EOL> self . _set_view_parameter ( self . __model_name__ + '<STR_LIT>' , data_list ) <EOL> self . _set_view_parameter ( self . __model_name__ . title ( ) , self . __model__ ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , self . _set_token ( ) ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , current_page ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , page_count ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , self . search_input ( ) ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , get_pair ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , only_trashed ) <EOL> return self . _load_view ( '<STR_LIT:list>' ) <EOL> def show ( self , id = None ) : <EOL> '''<STR_LIT>''' <EOL> data = self . __model__ . find ( id , include_trashed = True ) <EOL> if data is not None : <EOL> data . set_state_show ( ) <EOL> self . _setup_view_parameter ( ) <EOL> self . _set_view_parameter ( self . __model_name__ , data ) <EOL> return self . _load_view ( '<STR_LIT>' ) <EOL> def new ( self ) : <EOL> '''<STR_LIT>''' <EOL> data = self . __model__ ( ) <EOL> data . set_state_insert ( ) <EOL> self . _setup_view_parameter ( ) <EOL> self . _set_view_parameter ( self . __model_name__ , data ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , self . _set_token ( ) ) <EOL> return self . _load_view ( '<STR_LIT>' ) <EOL> def create ( self ) : <EOL> '''<STR_LIT>''' <EOL> token = request . POST . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not self . _is_token_match ( token ) : <EOL> success = False <EOL> error_message = '<STR_LIT>' <EOL> data = None <EOL> else : <EOL> data = self . __model__ ( ) <EOL> data . set_state_insert ( ) <EOL> data . assign_from_dict ( request . POST ) <EOL> data . save ( ) <EOL> success = data . success <EOL> error_message = data . error_message <EOL> self . _setup_view_parameter ( ) <EOL> self . _set_view_parameter ( self . __model_name__ , data ) <EOL> self . _set_view_parameter ( '<STR_LIT:success>' , success ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , error_message ) <EOL> if request . is_xhr or request . POST . pop ( '<STR_LIT>' , False ) : <EOL> if success : <EOL> token = self . _set_token ( ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , token ) <EOL> return self . _get_view_parameter_as_json ( ) <EOL> return self . _load_view ( '<STR_LIT>' ) <EOL> def edit ( self , id = None ) : <EOL> '''<STR_LIT>''' <EOL> data = self . __model__ . find ( id ) <EOL> if data is not None : <EOL> data . set_state_update ( ) <EOL> self . _setup_view_parameter ( ) <EOL> self . _set_view_parameter ( self . __model_name__ , data ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , self . _set_token ( ) ) <EOL> return self . _load_view ( '<STR_LIT>' ) <EOL> def update ( self , id = None ) : <EOL> '''<STR_LIT>''' <EOL> token = request . POST . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not self . _is_token_match ( token ) : <EOL> success = False <EOL> error_message = '<STR_LIT>' <EOL> data = None <EOL> else : <EOL> data = self . __model__ . find ( id ) <EOL> if data is not None : <EOL> data . set_state_update ( ) <EOL> data . assign_from_dict ( request . POST ) <EOL> data . save ( ) <EOL> success = data . success <EOL> error_message = data . error_message <EOL> else : <EOL> success = False <EOL> error_message = '<STR_LIT>' <EOL> self . _setup_view_parameter ( ) <EOL> self . _set_view_parameter ( self . __model_name__ , data ) <EOL> self . _set_view_parameter ( '<STR_LIT:success>' , success ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , error_message ) <EOL> if request . is_xhr or request . POST . pop ( '<STR_LIT>' , False ) : <EOL> if success : <EOL> token = self . _set_token ( ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , token ) <EOL> return self . _get_view_parameter_as_json ( ) <EOL> return self . _load_view ( '<STR_LIT>' ) <EOL> def trash ( self , id = None ) : <EOL> '''<STR_LIT>''' <EOL> data = self . __model__ . find ( id ) <EOL> self . _setup_view_parameter ( ) <EOL> self . _set_view_parameter ( self . __model_name__ , data ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , self . _set_token ( ) ) <EOL> return self . _load_view ( '<STR_LIT>' ) <EOL> def remove ( self , id = None ) : <EOL> '''<STR_LIT>''' <EOL> token = request . POST . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not self . _is_token_match ( token ) : <EOL> success = False <EOL> error_message = '<STR_LIT>' <EOL> data = None <EOL> else : <EOL> data = self . __model__ . find ( id ) <EOL> if data is not None : <EOL> data . trash ( ) <EOL> success = data . success <EOL> error_message = data . error_message <EOL> else : <EOL> success = False <EOL> error_message = '<STR_LIT>' <EOL> self . _setup_view_parameter ( ) <EOL> self . _set_view_parameter ( self . __model_name__ , data ) <EOL> self . _set_view_parameter ( '<STR_LIT:success>' , success ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , error_message ) <EOL> if request . is_xhr or request . POST . pop ( '<STR_LIT>' , False ) : <EOL> if success : <EOL> token = self . _set_token ( ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , token ) <EOL> return self . _get_view_parameter_as_json ( ) <EOL> return self . _load_view ( '<STR_LIT>' ) <EOL> def untrash ( self , id = None ) : <EOL> token = request . POST . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not self . _is_token_match ( token ) : <EOL> success = False <EOL> error_message = '<STR_LIT>' <EOL> data = None <EOL> else : <EOL> data = self . __model__ . find ( id , True ) <EOL> if data is not None : <EOL> data . untrash ( ) <EOL> success = data . success <EOL> error_message = data . error_message <EOL> else : <EOL> success = False <EOL> error_message = '<STR_LIT>' <EOL> self . _setup_view_parameter ( ) <EOL> self . _set_view_parameter ( self . __model_name__ , data ) <EOL> self . _set_view_parameter ( '<STR_LIT:success>' , success ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , error_message ) <EOL> if request . is_xhr or request . POST . pop ( '<STR_LIT>' , False ) : <EOL> if success : <EOL> token = self . _set_token ( ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , token ) <EOL> return self . _get_view_parameter_as_json ( ) <EOL> return self . _load_view ( '<STR_LIT>' ) <EOL> def delete ( self , id = None ) : <EOL> '''<STR_LIT>''' <EOL> data = self . __model__ . find ( id , include_trashed = True ) <EOL> self . _setup_view_parameter ( ) <EOL> self . _set_view_parameter ( self . __model_name__ , data ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , self . _set_token ( ) ) <EOL> return self . _load_view ( '<STR_LIT>' ) <EOL> def destroy ( self , id = None ) : <EOL> '''<STR_LIT>''' <EOL> token = request . POST . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not self . _is_token_match ( token ) : <EOL> success = False <EOL> error_message = '<STR_LIT>' <EOL> data = None <EOL> else : <EOL> data = self . __model__ . find ( id , include_trashed = True ) <EOL> if data is not None : <EOL> data . delete ( ) <EOL> success = data . success <EOL> error_message = data . error_message <EOL> else : <EOL> success = False <EOL> error_message = '<STR_LIT>' <EOL> self . _setup_view_parameter ( ) <EOL> self . _set_view_parameter ( self . __model_name__ , data ) <EOL> self . _set_view_parameter ( '<STR_LIT:success>' , success ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , error_message ) <EOL> if request . is_xhr or request . POST . pop ( '<STR_LIT>' , False ) : <EOL> if success : <EOL> token = self . _set_token ( ) <EOL> self . _set_view_parameter ( '<STR_LIT>' , token ) <EOL> return self . _get_view_parameter_as_json ( ) <EOL> return self . _load_view ( '<STR_LIT>' ) </s>
