|
@@ -16,9 +16,11 @@ See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
limitations under the License.
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
+import re
|
|
import pymongo
|
|
import pymongo
|
|
from bson.objectid import ObjectId
|
|
from bson.objectid import ObjectId
|
|
|
|
|
|
|
|
+re_objectid = re.compile(r'[0-9a-f]{24}')
|
|
|
|
|
|
class EventManDB(object):
|
|
class EventManDB(object):
|
|
"""MongoDB connector."""
|
|
"""MongoDB connector."""
|
|
@@ -54,18 +56,30 @@ class EventManDB(object):
|
|
self.db = self.connection[self._dbName]
|
|
self.db = self.connection[self._dbName]
|
|
return self.db
|
|
return self.db
|
|
|
|
|
|
- def toID(self, _id):
|
|
|
|
- """Convert a string to a MongoDB ID.
|
|
|
|
|
|
+ def convert_obj(self, obj):
|
|
|
|
+ """Convert a string to an object for MongoDB.
|
|
|
|
|
|
- :param _id: string to convert to :class:`~bson.objectid.ObjectId`
|
|
|
|
- :type _id: str
|
|
|
|
-
|
|
|
|
- :return: MongoDB ID
|
|
|
|
- :rtype: :class:`~bson.objectid.ObjectId`
|
|
|
|
|
|
+ :param obj: object to convert
|
|
"""
|
|
"""
|
|
- if not isinstance(_id, ObjectId):
|
|
|
|
- _id = ObjectId(_id)
|
|
|
|
- return _id
|
|
|
|
|
|
+ try:
|
|
|
|
+ return ObjectId(obj)
|
|
|
|
+ except:
|
|
|
|
+ pass
|
|
|
|
+ try:
|
|
|
|
+ return int(obj)
|
|
|
|
+ except:
|
|
|
|
+ pass
|
|
|
|
+ return obj
|
|
|
|
+
|
|
|
|
+ def convert(self, seq):
|
|
|
|
+ if isinstance(seq, dict):
|
|
|
|
+ d = {}
|
|
|
|
+ for key, item in seq.iteritems():
|
|
|
|
+ d[key] = self.convert_obj(item)
|
|
|
|
+ return d
|
|
|
|
+ if isinstance(seq, (list, tuple)):
|
|
|
|
+ return [self.convert_obj(x) for x in seq]
|
|
|
|
+ return self.convert_obj(seq)
|
|
|
|
|
|
def get(self, collection, _id):
|
|
def get(self, collection, _id):
|
|
"""Get a single document with the specified `_id`.
|
|
"""Get a single document with the specified `_id`.
|
|
@@ -78,8 +92,7 @@ class EventManDB(object):
|
|
:return: the document with the given `_id`
|
|
:return: the document with the given `_id`
|
|
:rtype: dict
|
|
:rtype: dict
|
|
"""
|
|
"""
|
|
- _id = self.toID(_id)
|
|
|
|
- results = self.query(collection, {'_id': _id})
|
|
|
|
|
|
+ results = self.query(collection, self.convert({'_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):
|
|
@@ -94,13 +107,8 @@ class EventManDB(object):
|
|
:rtype: list
|
|
:rtype: list
|
|
"""
|
|
"""
|
|
db = self.connect()
|
|
db = self.connect()
|
|
- query = query or {}
|
|
|
|
- if'_id' in query:
|
|
|
|
- query['_id'] = self.toID(query['_id'])
|
|
|
|
- results = list(db[collection].find(query))
|
|
|
|
- for result in results:
|
|
|
|
- result['_id'] = str(result['_id'])
|
|
|
|
- return results
|
|
|
|
|
|
+ query = self.convert(query or {})
|
|
|
|
+ return list(db[collection].find(query))
|
|
|
|
|
|
def add(self, collection, data):
|
|
def add(self, collection, data):
|
|
"""Insert a new document.
|
|
"""Insert a new document.
|
|
@@ -152,7 +160,8 @@ class EventManDB(object):
|
|
elif isinstance(_id_or_query, (list, tuple)):
|
|
elif isinstance(_id_or_query, (list, tuple)):
|
|
_id_or_query = {'$or': self.buildSearchPattern(data, _id_or_query)}
|
|
_id_or_query = {'$or': self.buildSearchPattern(data, _id_or_query)}
|
|
elif not isinstance(_id_or_query, dict):
|
|
elif not isinstance(_id_or_query, dict):
|
|
- _id_or_query = {'_id': self.toID(_id_or_query)}
|
|
|
|
|
|
+ _id_or_query = {'_id': _id_or_query}
|
|
|
|
+ _id_or_query = self.convert(_id_or_query)
|
|
if '_id' in data:
|
|
if '_id' in data:
|
|
del data['_id']
|
|
del data['_id']
|
|
res = db[collection].find_and_modify(query=_id_or_query,
|
|
res = db[collection].find_and_modify(query=_id_or_query,
|
|
@@ -212,6 +221,6 @@ class EventManDB(object):
|
|
return
|
|
return
|
|
db = self.connect()
|
|
db = self.connect()
|
|
if not isinstance(_id_or_query, dict):
|
|
if not isinstance(_id_or_query, dict):
|
|
- _id_or_query = self.toID(_id_or_query)
|
|
|
|
|
|
+ _id_or_query = {'_id': _id_or_query}
|
|
db[collection].remove(_id_or_query)
|
|
db[collection].remove(_id_or_query)
|
|
|
|
|