close #35: the PUT method of EventsHandler can be used to set arbitrary values for registered persons

This commit is contained in:
Davide Alberani 2015-04-14 22:49:16 +02:00
parent e91874a64a
commit dcce705bbc
3 changed files with 13 additions and 5 deletions

View file

@ -128,7 +128,7 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', 'Event', 'Person',
Event.personAttended({ Event.personAttended({
_id: $stateParams.id, _id: $stateParams.id,
person_id: person.person_id, person_id: person.person_id,
'persons.$.attended': attended 'attended': attended
}, },
function(data) { function(data) {
$log.debug('EventDetailsCtrl.personAttended.data'); $log.debug('EventDetailsCtrl.personAttended.data');
@ -212,10 +212,10 @@ eventManControllers.controller('PersonDetailsCtrl', ['$scope', '$stateParams', '
Event.personAttended({ Event.personAttended({
_id: event._id, _id: event._id,
person_id: $stateParams.id, person_id: $stateParams.id,
'persons.$.attended': attended 'attended': attended
}, },
function(data) { function(data) {
$scope.events = data = Person.getEvents({_id: $stateParams.id, all: true}); $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
} }
); );
}; };

View file

@ -171,7 +171,8 @@ class EventManDB(object):
continue continue
return _or return _or
def update(self, collection, _id_or_query, data, operation='update', create=True): def update(self, collection, _id_or_query, data, operation='update',
updateList=None, create=True):
"""Update an existing document or create it, if requested. """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. _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 In the latter case, the tuples are put in OR; a tuple match if all of its
@ -185,6 +186,8 @@ class EventManDB(object):
:type data: dict :type data: dict
:param operation: operation used to update the document or a portion of it, like a list (update, append, appendUnique, delete) :param operation: operation used to update the document or a portion of it, like a list (update, append, appendUnique, delete)
:type operation: str :type operation: str
:param updateList: if set, it's considered the name of a list (the first matching element will be updated)
:type updateList: str
:param create: if True, the document is created if no document matches :param create: if True, the document is created if no document matches
:type create: bool :type create: bool
@ -201,6 +204,11 @@ class EventManDB(object):
if '_id' in data: if '_id' in data:
del data['_id'] del data['_id']
operator = self._operations.get(operation) operator = self._operations.get(operation)
if updateList:
newData = {}
for key, value in data.iteritems():
newData['%s.$.%s' % (updateList, key)] = value
data = newData
res = db[collection].find_and_modify(query=_id_or_query, res = db[collection].find_and_modify(query=_id_or_query,
update={operator: data}, full_response=True, new=True, upsert=create) update={operator: data}, full_response=True, new=True, upsert=create)
lastErrorObject = res.get('lastErrorObject') or {} lastErrorObject = res.get('lastErrorObject') or {}

View file

@ -191,7 +191,7 @@ class EventsHandler(CollectionHandler):
# Update an existing entry for a person registered at this event. # Update an existing entry for a person registered at this event.
merged, doc = self.db.update('events', merged, doc = self.db.update('events',
{'_id': id_, 'persons.person_id': person_id}, {'_id': id_, 'persons.person_id': person_id},
data, create=False) data, updateList='persons', create=False)
return {'event': doc} return {'event': doc}
def handle_delete_persons(self, id_, person_id): def handle_delete_persons(self, id_, person_id):