<s> from balancer import pinning <EOL> class MasterSlaveMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( MasterSlaveMixin , self ) . __init__ ( ) <EOL> from django . conf import settings <EOL> self . master = settings . MASTER_DATABASE <EOL> def db_for_write ( self , model , ** hints ) : <EOL> """<STR_LIT>""" <EOL> return self . master <EOL> def allow_relation ( self , obj1 , obj2 , ** hints ) : <EOL> """<STR_LIT>""" <EOL> pool = self . pool + [ self . master ] <EOL> if obj1 . _state . db in pool and obj2 . _state . db in pool : <EOL> return True <EOL> return None <EOL> def allow_syncdb ( self , db , model ) : <EOL> """<STR_LIT>""" <EOL> return db == self . master <EOL> def allow_migrate ( self , db , model ) : <EOL> """<STR_LIT>""" <EOL> return db == self . master <EOL> class PinningMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def db_for_read ( self , model , ** hints ) : <EOL> from django . conf import settings <EOL> if pinning . thread_is_pinned ( ) : <EOL> return settings . MASTER_DATABASE <EOL> return super ( PinningMixin , self ) . db_for_read ( model , ** hints ) <EOL> def db_for_write ( self , model , ** hints ) : <EOL> pinning . set_db_write ( ) <EOL> pinning . pin_thread ( ) <EOL> return super ( PinningMixin , self ) . db_for_write ( model , ** hints ) </s>
