<s> """<STR_LIT>""" <EOL> from flask import request <EOL> from flask_velox . mixins . context import ContextMixin <EOL> class BaseModelMixin ( ContextMixin ) : <EOL> """<STR_LIT>""" <EOL> def set_context ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( BaseModelMixin , self ) . set_context ( ) <EOL> self . add_context ( '<STR_LIT>' , self . get_model ( ) ) <EOL> def get_session ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> return self . session <EOL> def get_model ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> return self . model <EOL> def get_pk_field ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( self , '<STR_LIT>' , '<STR_LIT:id>' ) <EOL> class SingleObjectMixin ( BaseModelMixin ) : <EOL> """<STR_LIT>""" <EOL> def get_lookup_field ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( self , '<STR_LIT>' , '<STR_LIT:id>' ) <EOL> def get_lookup_value ( self ) : <EOL> """<STR_LIT>""" <EOL> field = self . get_lookup_field ( ) <EOL> val = request . view_args . get ( field ) <EOL> if not val : <EOL> val = request . args . get ( field ) <EOL> if not val : <EOL> val = getattr ( self , field , None ) <EOL> return val <EOL> def get_object ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . _obj <EOL> model = self . get_model ( ) <EOL> val = self . get_lookup_value ( ) <EOL> if val : <EOL> filter_by = { <EOL> self . get_lookup_field ( ) : val } <EOL> obj = model . query . filter_by ( ** filter_by ) . first ( ) <EOL> else : <EOL> obj = model ( ) <EOL> self . _obj = obj <EOL> return obj </s>
