瀏覽代碼

decouple the backend calls from MongoDB operators

Davide Alberani 9 年之前
父節點
當前提交
4f630a5ba2
共有 2 個文件被更改,包括 15 次插入6 次删除
  1. 12 3
      backend.py
  2. 3 3
      eventman_server.py

+ 12 - 3
backend.py

@@ -27,6 +27,14 @@ class EventManDB(object):
     db = None
     connection = None
 
+    # map operations on lists of items.
+    _operations = {
+            'update': '$set',
+            'append': '$push',
+            'appendUnique': '$addToSet',
+            'delete': '$pull'
+    }
+
     def __init__(self, url=None, dbName='eventman'):
         """Initialize the instance, connecting to the database.
 
@@ -163,7 +171,7 @@ class EventManDB(object):
                 continue
         return _or
 
-    def update(self, collection, _id_or_query, data, operator='$set', create=True):
+    def update(self, collection, _id_or_query, data, operation='update', create=True):
         """Update an existing document or create it, if requested.
         _id_or_query can be an ID, a dict representing a query or a list of tuples.
         In the latter case, the tuples are put in OR; a tuple match if all of its
@@ -175,8 +183,8 @@ class EventManDB(object):
         :type _id_or_query: str or :class:`~bson.objectid.ObjectId` or iterable
         :param data: the updated information to store
         :type data: dict
-        :param operator: operator used to update the document
-        :type operator: str
+        :param operation: operation used to update the document or a portion of it, like a list (update, append, appendUnique, delete)
+        :type operation: str
         :param create: if True, the document is created if no document matches
         :type create: bool
 
@@ -192,6 +200,7 @@ class EventManDB(object):
             _id_or_query = {'_id': _id_or_query}
         if '_id' in data:
             del data['_id']
+        operator = self._operations.get(operation)
         res = db[collection].find_and_modify(query=_id_or_query,
                 update={operator: data}, full_response=True, new=True, upsert=create)
         lastErrorObject = res.get('lastErrorObject') or {}

+ 3 - 3
eventman_server.py

@@ -180,7 +180,7 @@ class EventsHandler(CollectionHandler):
             merged, doc = self.db.update('events',
                     {'_id': id_},
                     {'persons': data},
-                    operator='$push',
+                    operation='append',
                     create=False)
         return {'event': doc}
 
@@ -196,7 +196,7 @@ class EventsHandler(CollectionHandler):
         merged, doc = self.db.update('events',
                 {'_id': id_},
                 {'persons': {'person_id': person_id}},
-                operator='$pull',
+                operation='delete',
                 create=False)
         return {'event': doc}
 
@@ -257,7 +257,7 @@ class EbCSVImportPersonsHandler(BaseHandler):
                                 {'_id': event_id, 'persons.person_id': person_id}):
                             self.db.update('events', {'_id': event_id},
                                     {'persons': person},
-                                    operator='$addToSet')
+                                    operation='appendUnique')
                             reply['new_in_event'] += 1
         self.write(reply)