import and merge backend

This commit is contained in:
Davide Alberani 2015-03-29 23:47:59 +02:00
parent 47d305ab63
commit f636de8d9a
2 changed files with 18 additions and 3 deletions

View file

@ -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.

View file

@ -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)