From f636de8d9ade4a22f0b2e3864f1429930632a081 Mon Sep 17 00:00:00 2001 From: Davide Alberani Date: Sun, 29 Mar 2015 23:47:59 +0200 Subject: [PATCH] import and merge backend --- backend.py | 13 +++++++++++++ eventman_server.py | 8 +++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/backend.py b/backend.py index e46814f..289007d 100644 --- a/backend.py +++ b/backend.py @@ -125,6 +125,19 @@ class EventManDB(object): db[collection].update({'_id': ObjectId(_id)}, {'$set': data}) return self.get(collection, _id) + def merge(self, collection, data, searchBy): + db = self.connect() + _or = [] + for searchPattern in searchBy: + try: + _or.append(dict([(k, data[k]) for k in searchPattern])) + except KeyError: + continue + if not _or: + return {} + r = db[collection].update({'$or': _or}, {'$set': data}, upsert=True) + return r['updatedExisting'] + def delete(self, collection, _id_or_query=None, force=False): """Remove one or more documents from a collection. diff --git a/eventman_server.py b/eventman_server.py index 7869c5f..54ff3a7 100755 --- a/eventman_server.py +++ b/eventman_server.py @@ -178,11 +178,13 @@ class EbCSVImportPersonsHandler(ImportPersonsHandler): for fieldname, contents in self.request.files.iteritems(): for content in contents: filename = content['filename'] - parseStats, users = csvParse(content['body'], remap=self.csvRemap) + parseStats, persons = csvParse(content['body'], remap=self.csvRemap) reply['total'] += parseStats['total'] reply['valid'] += parseStats['valid'] - for user in users: - print user + for person in persons: + if self.db.merge('persons', person, + searchBy=[('email',), ('name', 'surname')]): + reply['merged'] += 1 self.write(reply)