update and add method for the backend

This commit is contained in:
Davide Alberani 2015-03-21 20:33:23 +01:00
parent 550d6bf443
commit bfc08033e2

View file

@ -4,6 +4,7 @@ Classes and functions used to manage events and attendants.
""" """
import pymongo import pymongo
from bson.objectid import ObjectId
class EventManDB(object): class EventManDB(object):
@ -26,14 +27,18 @@ class EventManDB(object):
self.db = self.connection[self._dbName] self.db = self.connection[self._dbName]
return self.db return self.db
def get(self, collection, id_): def get(self, collection, _id):
results = self.query(collection, {'id': id_}) if not isinstance(_id, ObjectId):
print results, id_, type(id_) _id = ObjectId(_id)
results = self.query(collection, {'_id': _id})
return results and results[0] or {} return results and results[0] or {}
def query(self, collection, query=None): def query(self, collection, query=None):
db = self.connect() db = self.connect()
results = list(db[collection].find(query or {})) query = query or {}
if'_id' in query and not isinstance(query['_id'], ObjectId):
query['_id'] = ObjectId(query['_id'])
results = list(db[collection].find(query))
for result in results: for result in results:
result['_id'] = str(result['_id']) result['_id'] = str(result['_id'])
return results return results
@ -41,36 +46,13 @@ class EventManDB(object):
def add(self, collection, data): def add(self, collection, data):
db = self.connect() db = self.connect()
_id = db[collection].insert(data) _id = db[collection].insert(data)
newData = db[collection].find_one({'_id': _id}) return self.get(collection, _id)
newData['_id'] = str(newData['_id'])
return newData
#def update(self, collection) def update(self, collection, _id, data):
def addUser(self, user):
db = self.connect() db = self.connect()
db.users.insert(user) data = data or {}
if '_id' in data:
def addEvent(self, event): del data['_id']
db = self.connect() db[collection].update({'_id': ObjectId(_id)}, {'$set': data})
db.events.insert(event) return self.get(collection, _id)
def getUser(self, query=None):
db = self.connect()
return db.users.find_one(query or {})
def getEvent(self, query):
db = self.connect()
return db.events.find_one(query or {})
def getUsers(self, eventID=None):
self.connect()
pass
def getEvents(self):
self.connect()
pass