Browse Source

#91: also transmit via WebSocket added attendees

Davide Alberani 8 years ago
parent
commit
4c8a13887f
5 changed files with 22 additions and 22 deletions
  1. 1 1
      README.md
  2. 3 4
      angular_app/index.html
  3. 11 12
      angular_app/js/controllers.js
  4. 2 2
      angular_app/js/services.js
  5. 5 3
      eventman_server.py

+ 1 - 1
README.md

@@ -63,7 +63,7 @@ License and copyright
 =====================
 
 Copyright 2015-2016 Davide Alberani <da@erlug.linux.it>  
-               RaspiBO <info@raspibo.org>
+                    RaspiBO <info@raspibo.org>
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.

+ 3 - 4
angular_app/index.html

@@ -31,8 +31,8 @@
     </head>
     
     <!--
-        Copyright 2015 Davide Alberani <da@erlug.linux.it>
-                       RaspiBO <info@raspibo.org>
+        Copyright 2015-2016 Davide Alberani <da@erlug.linux.it>
+                            RaspiBO <info@raspibo.org>
 
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with 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">

+ 11 - 12
angular_app/js/controllers.js

@@ -131,12 +131,16 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
                         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 (person_idx != -1) {
+			    $log.debug('person_id ' + data.person_id + ' found');
+			} else {
+			    $log.debug('person_id ' + data.person_id + ' not found');
+			}
+
+                        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);
                         }
                     }
                 );
@@ -194,19 +198,14 @@ 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.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();
                     }
-                    $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) {

+ 2 - 2
angular_app/js/services.js

@@ -42,10 +42,10 @@ 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);
                 }
             },
 

+ 5 - 3
eventman_server.py

@@ -3,8 +3,8 @@
 
 Your friendly manager of attendees at an event.
 
-Copyright 2015 Davide Alberani <da@erlug.linux.it>
-               RaspiBO <info@raspibo.org>
+Copyright 2015-2016 Davide Alberani <da@erlug.linux.it>
+                    RaspiBO <info@raspibo.org>
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with 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.