Merge pull request #13 from alberanid/master

draft of resource-to-resource resolution
This commit is contained in:
Davide Alberani 2015-03-31 23:36:50 +02:00
commit b9c29d85c3
5 changed files with 62 additions and 7 deletions

View file

@ -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() {

View file

@ -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) {

View file

@ -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>

View file

@ -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"})

View file

@ -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)