def __new__(mcs, name, bases, attrs): new_class = super(ModelBase, mcs).__new__(mcs, name, bases, attrs) parents = [b for b in bases if isinstance(b, ModelBase)] if not parents: # If this isn't a subclass of Model, don't do anything special. return new_class # Processing Model metadata. try: meta = getattr(new_class, 'Meta') except AttributeError: meta = None options = _Options(meta) options.collection = options.collection or to_underscore(name) if options.interface: new_class._meta = None new_class.database = None new_class.collection = DummyCollection return new_class elif options.deffered and not options.configured: deffered_classes.append((mcs, new_class, options, name)) return new_class else: return mcs.configure(new_class, options, name, mcs)
def __new__(mcs, name, bases, attrs): new_class = super(ModelBase, mcs).__new__(mcs, name, bases, attrs) parents = [b for b in bases if isinstance(b, ModelBase)] if not parents: # If this isn't a subclass of Model, don't do anything special. return new_class # Processing Model metadata. try: meta = getattr(new_class, 'Meta') except AttributeError: meta = None else: delattr(new_class, 'Meta') # Won't need the original metadata # container anymore. options = _Options(meta) options.collection = options.collection or to_underscore(name) if options.interface: new_class._meta = None new_class.database = None new_class.collection = DummyCollection return new_class if not (options.host and options.port and options.database): raise Exception( 'Model %r improperly configured: %s %s %s' % ( name, options.host, options.port, options.database)) # Checking connection pool for an existing connection. hostport = options.host, options.port if hostport in mcs._connections: connection = mcs._connections[hostport] else: # _connect=False option # creates :class:`pymongo.connection.Connection` object without # establishing connection. It's required if there is no running # mongodb at this time but we want to create :class:`Model`. connection = Connection(*hostport, _connect=False) mcs._connections[hostport] = connection new_class._meta = options new_class.connection = connection new_class.database = connection[options.database] new_class.collection = options.collection_class( new_class.database, options.collection, document_class=new_class) new_class.backward_references = [] if options.auto_index: new_class.auto_index() # Generating required indices. if options.references: for reference in options.references: if len(reference) == 3: field_name, backward_reference_cls, type = reference backward_reference_cls.add_backward_reference(field_name=field_name, referent_cls=new_class, type=type) return new_class
def __new__(mcs, name, bases, attrs): new_class = super(ModelBase, mcs).__new__(mcs, name, bases, attrs) parents = [b for b in bases if isinstance(b, ModelBase)] if not parents: # If this isn't a subclass of Model, don't do anything special. return new_class # Processing Model metadata. try: meta = getattr(new_class, 'Meta') except AttributeError: meta = None else: delattr(new_class, 'Meta') # Won't need the original metadata any more options = _Options(meta) options.collection = options.collection or to_underscore(name) if options.interface: new_class._meta = None new_class.database = None new_class.collection = DummyCollection return new_class if not (options.host and options.port and options.database): raise Exception( 'Model %r improperly configured: %s %s %s' % ( name, options.host, options.port, options.database)) # Checking connection / client pool for an existing connection / client. pool_key = options.host, options.port if options.replica_set_name: pool_key = options.replica_set_name if pool_key in mcs._connections: client = mcs._connections[pool_key] else: if options.replica_set_name: client = pymongo.MongoClient(options.replica_set_uri, replicaSet=options.replica_set_name) else: client = pymongo.MongoClient(options.host, options.port) mcs._connections[pool_key] = client new_class._meta = options new_class.connection = client new_class.database = client[options.database] if options.username and options.password: new_class.database.authenticate(options.username, options.password) new_class.collection = options.collection_class( new_class.database, options.collection, document_class=new_class) if options.auto_index: new_class.auto_index() # Generating required indices. return new_class
def __new__(mcs, name, bases, attrs): new_class = super(ModelBase, mcs).__new__(mcs, name, bases, attrs) parents = [b for b in bases if isinstance(b, ModelBase)] if not parents: # If this isn't a subclass of Model, don't do anything special. return new_class # Processing Model metadata. try: meta = getattr(new_class, 'Meta') except AttributeError: meta = None else: delattr(new_class, 'Meta') # Won't need the original metadata # container anymore. options = _Options(meta) options.collection = options.collection or to_underscore(name) if options.interface: new_class._meta = None new_class.database = None new_class.collection = DummyCollection return new_class if not (options.host and options.port and options.database): raise Exception( 'Model %r improperly configured: %s %s %s' % (name, options.host, options.port, options.database)) # Checking connection pool for an existing connection. hostport = options.host, options.port if hostport in mcs._connections: connection = mcs._connections[hostport] else: # _connect=False option # creates :class:`pymongo.connection.Connection` object without # establishing connection. It's required if there is no running # mongodb at this time but we want to create :class:`Model`. connection = MongoClient(*hostport, replicaset=options.replicaset) mcs._connections[hostport] = connection new_class._meta = options new_class.connection = connection new_class.database = connection[options.database] if options.username and options.password: new_class.database.authenticate(options.username, options.password) new_class.collection = options.collection_class( new_class.database, options.collection, document_class=new_class) if options.auto_index: new_class.auto_index() # Generating required indices. return new_class
def test_options_init(): class Meta: foo = 'bar' options = _Options(Meta) assert options.foo, 'bar'
def __new__(mcs, name, bases, attrs): new_class = super(ModelBase, mcs).__new__(mcs, name, bases, attrs) parents = [b for b in bases if isinstance(b, ModelBase)] if not parents: # If this isn't a subclass of Model, don't do anything special. return new_class # Processing Model metadata. try: meta = getattr(new_class, "Meta") except AttributeError: meta = None else: delattr(new_class, "Meta") # Won't need the original metadata # container anymore. options = _Options(meta) options.collection = options.collection or to_underscore(name) if options.interface: new_class._meta = None new_class.database = None new_class.collection = DummyCollection return new_class if not (options.host and options.port and options.database): raise Exception( "Model %r improperly configured: %s %s %s" % (name, options.host, options.port, options.database) ) # Checking connection / client pool for an existing connection / client. pool_key = options.host, options.port if options.replica_set_name: logging.debug("Using replica_set_name=%s as database pool key." % options.replica_set_name) pool_key = options.replica_set_name if pool_key in mcs._connections: client = mcs._connections[pool_key] logging.debug("Got database client from pool for pool_key=%s" % (pool_key,)) else: logging.debug("Creating new database client for pool_key=%s" % (pool_key,)) if options.replica_set_name: logging.debug("Setting up a replica set client...") client = MongoReplicaSetClient(options.replica_set_uri, replicaSet=options.replica_set_name) client.read_preference = ReadPreference.SECONDARY_PREFERRED else: logging.debug("Setting up a normal client...") client = MongoClient(options.host, options.port) mcs._connections[pool_key] = client new_class._meta = options new_class.connection = client new_class.database = client[options.database] if options.username and options.password: new_class.database.authenticate(options.username, options.password) new_class.collection = options.collection_class( new_class.database, options.collection, document_class=new_class ) if options.auto_index: new_class.auto_index() # Generating required indices. return new_class