Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymongo
- from django.conf import settings
- #Checks to make
- # - If database name is valid
- # and a lot more. Its in pre-pre-pre-alpha working condition though.
- ## NEED TO FIND A BETTER WAY TO DISCONNECT
- ## A user can assign an id to an object and potentially delete data, need to find workaround for this
- class MongoConnector:
- '''Takes an optional name parameter, which is the key from the MONGO dictionary in the settings file, for the database to use.'''
- def __init__(self, name='mongo'):
- self.host = settings.MONGO[name]['HOST']
- try:
- self.port = int(settings.MONGO[name]['PORT'])
- except ValueError:
- self.port = 27017
- self.dbname = settings.MONGO[name]['NAME']
- if settings.MONGO[name]['USER']:
- self.user = settings.MONGO[name]['USER']
- else:
- self.user = None
- if settings.MONGO[name]['PASSWORD']:
- self.password = settings.MONGO[name]['PASSWORD']
- else:
- self.password = None
- def connect(self):
- '''returns a connection object to the selected database'''
- return pymongo.connection.Connection(self.host, self.port)
- class Mongo:
- '''Methods for using a mongo database. Takes a collection name to initialise and optional parameters for document and name of the key from the MONGO dictionary in the settings file to select the database to use'''
- # objects = MongoManager()
- def __init__(self, collection, document=None, name='mongo'):
- self.document = document
- self.dbname = settings.MONGO[name]['NAME']
- self.multidbname = name
- self.collection = collection
- self.id = None
- def save(self):
- conn = MongoConnector(self.multidbname).connect()
- coll = conn[self.dbname][self.collection]
- if self.id is None:
- objId = coll.insert(self.document)
- self.id = objId
- if '_id' in self.document:
- pass
- else:
- self.document['_id'] = objId
- else:
- coll.update({'_id': self.id}, self.document)
- conn.disconnect()
- return self.id
- def delete(self):
- conn = MongoConnector(self.multidbname).connect()
- coll = conn[self.dbname][self.collection]
- coll.remove({'_id': self.id})
- conn.disconnect()
- #class MongoManager:
- # def __init__(self):
- # self = Mongo()
- def get(self, query=None):
- conn = MongoConnector(self.multidbname).connect()
- coll = conn[self.dbname][self.collection]
- result = coll.find_one(query)
- obj = Mongo(self.collection, result)
- obj.id = result['_id']
- conn.disconnect()
- return obj
- def filter(self, query=None):
- conn = MongoConnector(self.multidbname).connect()
- coll = conn[self.dbname][self.collection]
- results = []
- for result in coll.find(query):
- obj = Mongo(self.collection, result)
- obj.id = result['_id']
- results.append(obj)
- conn.disconnect()
- return results
- def remove(self, query):
- if query is None:
- raise Exception('remove query cannot be None')
- else:
- conn = MongoConnector(self.multidbname).connect()
- coll = conn[self.dbname][self.collection]
- coll.remove(query)
- conn.disconnect()
- def remove_all(self):
- conn = MongoConnector(self.multidbname).connect()
- coll = conn[self.dbname][self.collection]
- coll.remove()
- conn.disconnect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement