Merge pull request #93 from alberanid/master

add and remove attendees via WebSocket
This commit is contained in:
Davide Alberani 2016-04-10 19:43:26 +02:00
commit d55c8c9108
5 changed files with 57 additions and 35 deletions

View file

@ -31,7 +31,7 @@
</head>
<!--
Copyright 2015 Davide Alberani <da@erlug.linux.it>
Copyright 2015-2016 Davide Alberani <da@erlug.linux.it>
RaspiBO <info@raspibo.org>
Licensed under the Apache License, Version 2.0 (the "License");
@ -71,8 +71,7 @@
<div class="main-header">
</div>
<!-- all the magic takes place here: the content inside the next div
changes accordingly to the location you're visiting -->
<!-- all the magic takes place here: the content inside the next div changes accordingly to the location you're visiting -->
<div ui-view></div>
<nav class="navbar navbar-default navbar-fixed-bottom">

View file

@ -128,15 +128,24 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
return;
}
var data = $scope.EventUpdates.data.update;
if (!$scope.event.persons) {
$scope.event.persons = [];
}
var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
return data.person_id == el.person_id;
});
if (person_idx == -1) {
$log.warn('unable to find person_id ' + person_id);
return;
if (person_idx != -1) {
$log.debug('person_id ' + data.person_id + ' found');
} else {
$log.debug('person_id ' + data.person_id + ' not found');
}
if ($scope.event.persons[person_idx] != data.person) {
if (data.action == 'update' && person_idx != -1 && $scope.event.persons[person_idx] != data.person) {
$scope.event.persons[person_idx] = data.person;
} else if (data.action == 'add' && person_idx == -1) {
$scope.event.persons.push(data.person);
} else if (data.action == 'delete' && person_idx != -1) {
$scope.event.persons.splice(person_idx, 1);
}
}
);
@ -194,19 +203,17 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
person_data.person_id = person_data._id;
person_data._id = $stateParams.id;
Event.addPerson(person_data, function() {
// This could be improved adding it only locally.
//$scope.event.persons.push(person_data);
$scope.setPersonAttribute(person_data, 'attended', true, function() {
Event.get($stateParams, function(data) {
$scope.event.persons = angular.fromJson(data).persons;
});
var idx = $scope.allPersons.indexOf(original_data);
if (idx != -1) {
$scope.allPersons.splice(idx, 1);
} else {
$scope.allPersons = Person.all();
if (!$scope.event.persons) {
$scope.event.persons = [];
}
$scope.event.persons.push(person_data);
$scope.setPersonAttribute(person_data, 'attended', true, function() {
var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
return person_data.person_id == el.person_id;
});
if (all_person_idx != -1) {
$scope.allPersons.splice(all_person_idx, 1);
}
$scope.newPerson = {};
$translate('{{person_name}} {{person_surname}} successfully added to event {{event_title}}',
{person_name: person_data.name, person_surname: person_data.surname, event_title: $scope.event.title}).then(
function (translation) {
@ -279,8 +286,18 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
person_id: person.person_id
},
function(data) {
$scope.event.persons = data;
$scope.allPersons = Person.all();
if (!(data && data.person_id && $scope.event.persons)) {
return;
}
var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
return data.person_id == el.person_id;
});
if (person_idx == -1) {
$log.warn('unable to find and delete person_id ' + data.person_id);
return;
}
$scope.event.persons.splice(person_idx, 1);
$scope.allPersons.push(person);
});
};

View file

@ -42,19 +42,19 @@ eventManServices.factory('Event', ['$resource',
addPerson: {
method: 'POST',
isArray: true,
isArray: false,
url: 'events/:id/persons/:person_id',
transformResponse: function(data, headers) {
return angular.fromJson(data).event.persons;
return angular.fromJson(data);
}
},
deletePerson: {
method: 'DELETE',
isArray: true,
isArray: false,
url: 'events/:_id/persons/:person_id',
transformResponse: function(data, headers) {
return angular.fromJson(data).event.persons;
return angular.fromJson(data);
}
}
});

View file

@ -3,7 +3,7 @@
Your friendly manager of attendees at an event.
Copyright 2015 Davide Alberani <da@erlug.linux.it>
Copyright 2015-2016 Davide Alberani <da@erlug.linux.it>
RaspiBO <info@raspibo.org>
Licensed under the Apache License, Version 2.0 (the "License");
@ -391,13 +391,15 @@ class EventsHandler(CollectionHandler):
{'_id': id_, 'persons.person_id': person_id})
if '_id' in data:
del data['_id']
ret = {'action': 'add', 'person_id': person_id, 'person': data}
if not doc:
merged, doc = self.db.update('events',
{'_id': id_},
{'persons': data},
operation='appendUnique',
create=False)
return {'event': doc}
self.send_ws_message('event/%s/updates' % id_, json.dumps(ret))
return ret
def handle_put_persons(self, id_, person_id, data):
# Update an existing entry for a person registered at this event.
@ -440,12 +442,17 @@ class EventsHandler(CollectionHandler):
def handle_delete_persons(self, id_, person_id):
# Remove a specific person from the list of persons registered at this event.
doc = self.db.query('events',
{'_id': id_, 'persons.person_id': person_id})
ret = {'action': 'delete', 'person_id': person_id}
if doc:
merged, doc = self.db.update('events',
{'_id': id_},
{'persons': {'person_id': person_id}},
operation='delete',
create=False)
return {'event': doc}
self.send_ws_message('event/%s/updates' % id_, json.dumps(ret))
return ret
class EbCSVImportPersonsHandler(BaseHandler):
@ -718,4 +725,3 @@ def run():
if __name__ == '__main__':
run()