<s> from sqlalchemy import Column , Integer , Boolean , String <EOL> from sqlalchemy . orm . exc import NoResultFound <EOL> from flask import current_app <EOL> from flask . ext . featureflags import NoFeatureFlagFound , log <EOL> class SQLAlchemyFeatureFlags ( object ) : <EOL> def __init__ ( self , db , model = None ) : <EOL> if not model : <EOL> model = self . _make_model ( db ) <EOL> self . model = model <EOL> def __call__ ( self , feature = None ) : <EOL> if not current_app : <EOL> log . warn ( u"<STR_LIT>" . format ( feature = feature ) ) <EOL> return False <EOL> try : <EOL> return self . model . check ( feature ) <EOL> except NoResultFound : <EOL> raise NoFeatureFlagFound ( ) <EOL> def _make_model ( self , db ) : <EOL> class FeatureFlag ( db . Model ) : <EOL> id = Column ( Integer , primary_key = True ) <EOL> feature = Column ( String ( <NUM_LIT:255> ) , nullable = False , unique = True ) <EOL> is_active = Column ( Boolean , default = False ) <EOL> @ classmethod <EOL> def check ( cls , feature ) : <EOL> r = cls . query . filter_by ( feature = feature ) . one ( ) <EOL> return r . is_active <EOL> return FeatureFlag </s>
