Browse Source

insert imported persons into the "registered" collection

Davide Alberani 9 years ago
parent
commit
bc32466ff7
3 changed files with 44 additions and 10 deletions
  1. 1 1
      angular_app/event-detail.html
  2. 34 7
      backend.py
  3. 9 2
      eventman_server.py

+ 1 - 1
angular_app/event-detail.html

@@ -8,7 +8,7 @@
 
         <div class="input-group input-group-lg">
             <span class="input-group-addon">Title</span>
-            <input type="text" class="form-control" placeholder="Title" value="{{event.title}}" ng-model="event.title" ng-required="1">
+            <input type="text" class="form-control" placeholder="Title" ng-model="event.title" ng-required="1">
         </div>
 
         <div class="input-group top5 well form-horizontal" ng-controller="DatetimePickerCtrl">

+ 34 - 7
backend.py

@@ -54,6 +54,19 @@ class EventManDB(object):
         self.db = self.connection[self._dbName]
         return self.db
 
+    def toID(self, _id):
+        """Convert a string to a MongoDB ID.
+
+        :param _id: string to convert to :class:`~bson.objectid.ObjectId`
+        :type _id: str
+
+        :return: MongoDB ID
+        :rtype: :class:`~bson.objectid.ObjectId`
+        """
+        if not isinstance(_id, ObjectId):
+            _id = ObjectId(_id)
+        return _id
+
     def get(self, collection, _id):
         """Get a single document with the specified `_id`.
 
@@ -65,8 +78,7 @@ class EventManDB(object):
         :return: the document with the given `_id`
         :rtype: dict
         """
-        if not isinstance(_id, ObjectId):
-            _id = ObjectId(_id)
+        _id = self.toID(_id)
         results = self.query(collection, {'_id': _id})
         return results and results[0] or {}
 
@@ -83,8 +95,8 @@ class EventManDB(object):
         """
         db = self.connect()
         query = query or {}
-        if'_id' in query and not isinstance(query['_id'], ObjectId):
-            query['_id'] = ObjectId(query['_id'])
+        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'])
@@ -105,6 +117,21 @@ class EventManDB(object):
         _id = db[collection].insert(data)
         return self.get(collection, _id)
 
+    def insertOne(self, collection, data):
+        """Insert a document, avoiding duplicates.
+
+        :param collection: update a document in this collection
+        :type collection: str
+        :param data: the document information to store
+        :type data: dict
+
+        :return: True if the document was already present
+        :rtype: bool
+        """
+        db = self.connect()
+        ret = db[collection].update(data, {'$set': data}, upsert=True)
+        return ret['updatedExisting']
+
     def update(self, collection, _id, data):
         """Update an existing document.
 
@@ -122,7 +149,7 @@ class EventManDB(object):
         data = data or {}
         if '_id' in data:
             del data['_id']
-        db[collection].update({'_id': ObjectId(_id)}, {'$set': data})
+        db[collection].update({'_id': self.toID(_id)}, {'$set': data})
         return self.get(collection, _id)
 
     def merge(self, collection, data, searchBy):
@@ -166,7 +193,7 @@ class EventManDB(object):
         if not _id_or_query and not force:
             return
         db = self.connect()
-        if not isinstance(_id_or_query, (ObjectId, dict)):
-            _id_or_query = ObjectId(_id_or_query)
+        if not isinstance(_id_or_query, dict):
+            _id_or_query = self.toID(_id_or_query)
         db[collection].remove(_id_or_query)
 

+ 9 - 2
eventman_server.py

@@ -129,9 +129,16 @@ class EbCSVImportPersonsHandler(BaseHandler):
                 reply['total'] += parseStats['total']
                 reply['valid'] += parseStats['valid']
                 for person in persons:
-                    if self.db.merge('persons', person,
-                            searchBy=[('email',), ('name', 'surname')]):
+                    merged, _id = self.db.merge('persons', person,
+                            searchBy=[('email',), ('name', 'surname')])
+                    if merged:
                         reply['merged'] += 1
+                    if targetEvent and _id:
+                        registered_data = {
+                                'event_id': self.db.toID(targetEvent),
+                                'person_id': self.db.toID(_id),
+                                'from_file': filename}
+                        self.db.insertOne('registered', registered_data)
         self.write(reply)