Browse Source

draft of resource-to-resource resolution

Davide Alberani 9 years ago
parent
commit
ae5619ec73
5 changed files with 62 additions and 7 deletions
  1. 5 2
      angular_app/js/controllers.js
  2. 5 0
      angular_app/js/services.js
  3. 19 0
      angular_app/person-detail.html
  4. 31 4
      eventman_server.py
  5. 2 1
      utils.py

+ 5 - 2
angular_app/js/controllers.js

@@ -74,10 +74,13 @@ eventManControllers.controller('PersonsListCtrl', ['$scope', 'Person',
 );
 
 
-eventManControllers.controller('PersonDetailsCtrl', ['$scope', 'Person', '$routeParams',
-    function ($scope, Person, $routeParams) {
+eventManControllers.controller('PersonDetailsCtrl', ['$scope', '$routeParams', 'Person', 'Action',
+    function ($scope, $routeParams, Person, Action) {
         if ($routeParams.id) {
             $scope.person = Person.get($routeParams);
+            Action.get({person_id: $routeParams.id}, function(data) {
+                $scope.actions = angular.fromJson(data).actions;
+            });
         }
         // store a new Person or update an existing one
         $scope.save = function() {

+ 5 - 0
angular_app/js/services.js

@@ -3,6 +3,11 @@
 /* Services that are used to interact with the backend. */
 var eventManServices = angular.module('eventManServices', ['ngResource']);
 
+eventManServices.factory('Action', ['$resource',
+    function($resource) {
+        return $resource('actions');
+    }]
+);
 
 eventManServices.factory('Event', ['$resource',
     function($resource) {

+ 19 - 0
angular_app/person-detail.html

@@ -18,4 +18,23 @@
 
         <input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;"/>
     </form>
+
+    <div class="panel panel-primary table-striped top5">
+        <div class="panel-heading">Actions</div>
+        <table class="table">
+            <thead>
+                <tr>
+                    <th>Action</th>
+                    <th>Event ID</th>
+                </tr>
+            </thead>
+            <tbody>
+                <tr ng-repeat="action in actions">
+                    <td>{{action.action}}</td>
+                    <td>{{action.event_id}}</td>
+                </tr>
+            </tbody>
+        </table>
+    </div>
 </div>
+

+ 31 - 4
eventman_server.py

@@ -43,7 +43,7 @@ class RootHandler(BaseHandler):
     angular_app_path = os.path.join(os.path.dirname(__file__), "angular_app")
 
     @gen.coroutine
-    def get(self):
+    def get(self, *args, **kwargs):
         # serve the ./angular_app/index.html file
         with open(self.angular_app_path + "/index.html", 'r') as fd:
             self.write(fd.read())
@@ -57,7 +57,15 @@ class CollectionHandler(BaseHandler):
     collection = None
 
     @gen.coroutine
-    def get(self, id_=None):
+    def get(self, id_=None, resource=None, **kwargs):
+        if resource:
+            method = getattr(self, 'handle_%s' % resource, None)
+            if method and callable(method):
+                try:
+                    self.write(method(id_, **kwargs))
+                    return
+                except:
+                    pass
         if id_ is not None:
             # read a single document
             self.write(self.db.get(self.collection, id_))
@@ -91,12 +99,29 @@ class PersonsHandler(CollectionHandler):
     """Handle requests for Persons."""
     collection = 'persons'
 
+    def handle_events(self, _id, **kwds):
+        return {'events': []}
+
 
 class EventsHandler(CollectionHandler):
     """Handle requests for Events."""
     collection = 'events'
 
 
+class ActionsHandler(CollectionHandler):
+    """Handle requests for Actions."""
+    collection = 'actions'
+
+    def get(self, *args, **kwargs):
+        params = self.request.arguments or {}
+        if 'event_id' in params:
+            params['event_id'] = self.db.toID(params['event_id'][0])
+        if 'person_id' in params:
+            params['person_id'] = self.db.toID(params['person_id'][0])
+        data = self.db.query(self.collection, params)
+        self.write({'actions': data})
+
+
 class EbCSVImportPersonsHandler(BaseHandler):
     """Importer for CSV files exported from eventbrite."""
     csvRemap = {
@@ -137,8 +162,9 @@ class EbCSVImportPersonsHandler(BaseHandler):
                         registered_data = {
                                 'event_id': self.db.toID(targetEvent),
                                 'person_id': self.db.toID(_id),
+                                'action': 'registered',
                                 'from_file': filename}
-                        self.db.insertOne('registered', registered_data)
+                        self.db.insertOne('actions', registered_data)
         self.write(reply)
 
 
@@ -163,8 +189,9 @@ def run():
     init_params = dict(db=db_connector)
 
     application = tornado.web.Application([
-            (r"/persons/?(?P<id_>\w+)?", PersonsHandler, init_params),
+            (r"/persons/?(?P<id_>\w+)?/?(?P<resource>\w+)?", PersonsHandler, init_params),
             (r"/events/?(?P<id_>\w+)?", EventsHandler, init_params),
+            (r"/actions/?.*", ActionsHandler, init_params),
             (r"/(?:index.html)?", RootHandler, init_params),
             (r"/ebcsvpersons", EbCSVImportPersonsHandler, init_params),
             (r'/(.*)', tornado.web.StaticFileHandler, {"path": "angular_app"})

+ 2 - 1
utils.py

@@ -20,6 +20,7 @@ import csv
 import json
 import datetime
 import StringIO
+from bson.objectid import ObjectId
 
 
 def csvParse(csvStr, remap=None, merge=None):
@@ -74,7 +75,7 @@ class ImprovedEncoder(json.JSONEncoder):
     """Enhance the default JSON encoder to serialize datetime objects."""
     def default(self, o):
         if isinstance(o, (datetime.datetime, datetime.date,
-                datetime.time, datetime.timedelta)):
+                datetime.time, datetime.timedelta, ObjectId)):
             return str(o)
         return json.JSONEncoder.default(self, o)