import inspect
class DatabaseProxy :
__writeConn = None
__readConn = None
__readClass = None
def __init__(self, writeConnection, readConnection, readClass) :
self.__writeConn = writeConnection
self.__readConn = readConnection
self.__readClass = readClass
def getConnection(self):
returnConnection = None
# get the previos frame from the database
frame_dict = inspect.currentframe().f_back.f_locals
try:
if frame_dict.has_key('self') and self.is_write_class(frame_dict['self'], self.__readClass):
# this is the read event
returnConnection = self.__readConn
else:
print 'write'
# this is the write event
returnConnection = self.__writeconn
finally:
del frame_dict
return returnConnection
def is_write_class(self, instance, class_type):
# get the instance's class
instance_class = instance.__class__
# test the instance_class to be the same type as class_type
# or to be a class that extends class_type
if len([object for object in inspect.getmro(instance_class) if (clas is class_type)]) != 0 :
return True
return False
The easy part was that all the read access to the database was done throw one class(QuerySet) or throw a class that extends this QuerySet class.