From 08aef28d0d368a9091688910a36cd019b430622b Mon Sep 17 00:00:00 2001 From: Davide Alberani Date: Sun, 10 Apr 2016 17:21:46 +0200 Subject: [PATCH] fixes #86: only the changed person is returned, updating an attendee status --- README.md | 2 +- angular_app/js/controllers.js | 28 +++++++++++++++++++++++++--- angular_app/js/i18n.js | 1 - angular_app/js/services.js | 9 ++++----- eventman_server.py | 12 ++++++------ 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 42e1b82..1129044 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ By default, authentication is required; default username and password are *admin License and copyright ===================== -Copyright 2015 Davide Alberani +Copyright 2015-2016 Davide Alberani RaspiBO Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/angular_app/js/controllers.js b/angular_app/js/controllers.js index ca202e9..bf7e4a4 100644 --- a/angular_app/js/controllers.js +++ b/angular_app/js/controllers.js @@ -124,10 +124,20 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event', $scope.$watchCollection(function() { return $scope.EventUpdates.data; }, function(prev, old) { - if (!($scope.EventUpdates.data && $scope.EventUpdates.data.persons)) { + if (!($scope.EventUpdates.data && $scope.EventUpdates.data.update)) { return; } - $scope.event.persons = $scope.EventUpdates.data.persons; + var data = $scope.EventUpdates.data.update; + 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 ($scope.event.persons[person_idx] != data.person) { + $scope.event.persons[person_idx] = data.person; + } } ); } @@ -229,7 +239,19 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event', data[key] = value; Event.updatePerson(data, function(data) { - $scope.event.persons = data; + if (!(data && data.person_id && data.person)) { + 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 person_id ' + person_id); + return; + } + if ($scope.event.persons[person_idx] != data.person) { + $scope.event.persons[person_idx] = data.person; + } if (callback) { callback(data); } diff --git a/angular_app/js/i18n.js b/angular_app/js/i18n.js index f9fb7f0..ceb57ba 100644 --- a/angular_app/js/i18n.js +++ b/angular_app/js/i18n.js @@ -3,7 +3,6 @@ eventManApp.config(['$translateProvider', function ($translateProvider) { - console.log($translateProvider); $translateProvider.useStaticFilesLoader({ prefix: '/static/i18n/', suffix: '.json' diff --git a/angular_app/js/services.js b/angular_app/js/services.js index d641ea8..0a6bf58 100644 --- a/angular_app/js/services.js +++ b/angular_app/js/services.js @@ -33,10 +33,10 @@ eventManServices.factory('Event', ['$resource', updatePerson: { method: 'PUT', - isArray: true, + isArray: false, url: 'events/:id/persons/:person_id', transformResponse: function(data, headers) { - return angular.fromJson(data).event.persons; + return angular.fromJson(data); } }, @@ -156,9 +156,8 @@ eventManApp.factory('EventUpdates', ['$websocket', '$location', '$log', '/ws/' + $location.path() + '/updates'); dataStream.onMessage(function(message) { $log.debug('EventUpdates message received'); - data.persons = angular.fromJson(message.data); - }); - + data.update = angular.fromJson(message.data); + }); } }; diff --git a/eventman_server.py b/eventman_server.py index ada7c0a..fc3ba31 100755 --- a/eventman_server.py +++ b/eventman_server.py @@ -128,7 +128,7 @@ _ws_clients = {} class CollectionHandler(BaseHandler): """Base class for handlers that need to interact with the database backend. - + Introduce basic CRUD operations.""" # set of documents we're managing (a collection in MongoDB or a table in a SQL database) collection = None @@ -155,11 +155,11 @@ class CollectionHandler(BaseHandler): def _filter_results(self, results, params): """Filter a list using keys and values from a dictionary. - + :param results: the list to be filtered :type results: list :param params: a dictionary of items that must all be present in an original list item to be included in the return - + :return: list of items that have all the keys with the same values as params :rtype: list""" if not params: @@ -433,10 +433,10 @@ class EventsHandler(CollectionHandler): if new_person_data.get('attended'): self.run_triggers('attends', stdin_data=stdin_data, env=env) + ret = {'action': 'update', 'person_id': person_id, 'person': new_person_data} if old_person_data != new_person_data: - self.send_ws_message('event/%s/updates' % id_, - json.dumps(doc.get('persons') or [])) - return {'event': doc} + self.send_ws_message('event/%s/updates' % id_, json.dumps(ret)) + return ret def handle_delete_persons(self, id_, person_id): # Remove a specific person from the list of persons registered at this event.