<s> """<STR_LIT>""" <EOL> from decimal import Decimal <EOL> from flask import current_app <EOL> from flask . ext . admin . contrib . sqla import ModelView <EOL> from sandman import app , db <EOL> class Model ( object ) : <EOL> """<STR_LIT>""" <EOL> __endpoint__ = None <EOL> """<STR_LIT>""" <EOL> __tablename__ = None <EOL> """<STR_LIT>""" <EOL> __top_level_json_name__ = None <EOL> """<STR_LIT>""" <EOL> __methods__ = ( '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> __table__ = None <EOL> """<STR_LIT>""" <EOL> __from_class__ = None <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def endpoint ( cls ) : <EOL> """<STR_LIT>""" <EOL> endpoint = '<STR_LIT>' <EOL> if cls . __endpoint__ is not None : <EOL> return cls . __endpoint__ <EOL> elif cls . __from_class__ is not None : <EOL> endpoint = cls . __from_class__ . __name__ . lower ( ) <EOL> else : <EOL> endpoint = cls . __tablename__ . lower ( ) <EOL> if not endpoint . endswith ( '<STR_LIT:s>' ) : <EOL> endpoint += '<STR_LIT:s>' <EOL> return endpoint <EOL> def resource_uri ( self ) : <EOL> """<STR_LIT>""" <EOL> primary_key_value = getattr ( self , self . primary_key ( ) , None ) <EOL> return '<STR_LIT>' . format ( self . endpoint ( ) , primary_key_value ) <EOL> @ classmethod <EOL> def primary_key ( cls ) : <EOL> """<STR_LIT>""" <EOL> if cls . __from_class__ : <EOL> cls = cls . __from_class__ <EOL> return cls . __table__ . primary_key . columns . values ( ) [ <NUM_LIT:0> ] . name <EOL> def links ( self ) : <EOL> """<STR_LIT>""" <EOL> links = [ ] <EOL> for foreign_key in self . __table__ . foreign_keys : <EOL> column = foreign_key . column . name <EOL> column_value = getattr ( self , column , None ) <EOL> if column_value : <EOL> table = foreign_key . column . table . name <EOL> with app . app_context ( ) : <EOL> endpoint = current_app . class_references [ table ] <EOL> links . append ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' . format ( <EOL> endpoint . __name__ , column_value ) } ) <EOL> links . append ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : self . resource_uri ( ) } ) <EOL> return links <EOL> def as_dict ( self , depth = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> result_dict = { } <EOL> for column in self . __table__ . columns . keys ( ) : <EOL> result_dict [ column ] = getattr ( self , column , None ) <EOL> if isinstance ( result_dict [ column ] , Decimal ) : <EOL> result_dict [ column ] = str ( result_dict [ column ] ) <EOL> result_dict [ '<STR_LIT>' ] = self . links ( ) <EOL> for foreign_key in self . __table__ . foreign_keys : <EOL> column_name = foreign_key . column . name <EOL> column_value = getattr ( self , column_name , None ) <EOL> if column_value : <EOL> table = foreign_key . column . table . name <EOL> with app . app_context ( ) : <EOL> endpoint = current_app . class_references [ table ] <EOL> session = db . session ( ) <EOL> resource = session . query ( endpoint ) . get ( column_value ) <EOL> if depth > <NUM_LIT:0> : <EOL> result_dict . update ( { <EOL> '<STR_LIT>' : endpoint . __name__ , <EOL> endpoint . __name__ . lower ( ) : resource . as_dict ( depth - <NUM_LIT:1> ) <EOL> } ) <EOL> else : <EOL> result_dict [ <EOL> endpoint . __name__ . lower ( ) + '<STR_LIT>' ] = '<STR_LIT>' . format ( <EOL> endpoint . __name__ , column_value ) <EOL> result_dict [ '<STR_LIT>' ] = self . resource_uri ( ) <EOL> return result_dict <EOL> def from_dict ( self , dictionary ) : <EOL> """<STR_LIT>""" <EOL> for column in self . __table__ . columns . keys ( ) : <EOL> value = dictionary . get ( column , None ) <EOL> if value : <EOL> setattr ( self , column , value ) <EOL> def replace ( self , dictionary ) : <EOL> """<STR_LIT>""" <EOL> for column in self . __table__ . columns . keys ( ) : <EOL> setattr ( self , column , None ) <EOL> self . from_dict ( dictionary ) <EOL> @ classmethod <EOL> def meta ( cls ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( cls , '<STR_LIT>' , None ) is not None : <EOL> cls = cls . __from_class__ <EOL> attribute_info = { } <EOL> for name , value in cls . __table__ . columns . items ( ) : <EOL> attribute_info [ name ] = str ( value . type ) . lower ( ) <EOL> return { cls . __name__ : attribute_info } <EOL> def __str__ ( self ) : <EOL> return str ( getattr ( self , self . primary_key ( ) ) ) <EOL> class AdminModelViewWithPK ( ModelView ) : <EOL> """<STR_LIT>""" <EOL> column_display_pk = True </s>
