def remove(self): """ Removes this object from the database. It will still remain in memory, however, and can be resaved at a later time provided that the original reference is maintained.""" storage = Storage() database = self._database collection = self._collection if database is None or collection is None: raise ValueError, "The object needs to be assigned a database and a collection." storage.getDatabase(database) storage.getCollection(collection) documents = storage.removeDocuments({"_id":self._id})
def getObjectsByKey(self, key, value, limit=None): """ This will retrieve documents from the database and collection specified by this object based on one of their keys and convert them to their proper Python object state. :param key: The key to select on. :param value: The value to search for. :param limit: The maximum amount of objects to return. Will return all results by default. :rtype: All the matching objects stored in the database. """ storage = Storage() database = self._database collection = self._collection if database is None or collection is None: raise ValueError, "The object needs to be assigned a database and a collection." storage.getDatabase(database) storage.getCollection(collection) documents = storage.getDocuments({key:value}, limit) objects = [ self.loadFromRawData( data ) for data in documents ] return objects
def save(self): """Save this object into the database with all its public attributes.""" # Can't save without a database or a table if self._database is None: raise ValueError, "No database has been selected." if self._collection is None: raise ValueError, "No collection has been selected." # Check private variables. We probably shouldn't store these. document = {} for key, value in self.__dict__.items(): key = key.replace("_"+self._type, "") if key.startswith("__"): continue document[key] = value # Let's store this object storage = Storage() storage.getDatabase(self._database) storage.getCollection(self._collection) storage.insertDocuments(document) self._id = document["_id"]