ソースを参照

backend draft

Davide Alberani 9 年 前
コミット
8a1834e509
3 ファイル変更49 行追加10 行削除
  1. 3 3
      angular_app/js/controllers.js
  2. 24 2
      backend.py
  3. 22 5
      eventman_server.py

+ 3 - 3
angular_app/js/controllers.js

@@ -50,10 +50,10 @@ eventManControllers.controller('PersonDetailsCtrl', ['$scope', 'Person', '$route
             $scope.person = Person.get($routeParams);
         }
         $scope.save = function() {
-                if ($scope.event.id === undefined) {
-                    Event.save($scope.event);
+                if ($scope.person.id === undefined) {
+                    Person.save($scope.person);
                 } else {
-                    Event.update($scope.event);
+                    Person.update($scope.person);
                 }
         };
     }]

+ 24 - 2
backend.py

@@ -7,6 +7,7 @@ import pymongo
 
 
 class EventManDB(object):
+    db = None
     connection = None
 
     def __init__(self, url=None, dbName='eventman'):
@@ -15,8 +16,8 @@ class EventManDB(object):
         self.connect(url)
 
     def connect(self, url=None, dbName=None):
-        if self.connection is not None:
-            return self.connection
+        if self.db is not None:
+            return self.db
         if url:
             self._url = url
         if dbName:
@@ -25,6 +26,27 @@ class EventManDB(object):
         self.db = self.connection[self._dbName]
         return self.db
 
+    def get(self, collection, id_):
+        results = self.query(collection, {'id': id_})
+        print results, id_, type(id_)
+        return results and results[0] or {}
+
+    def query(self, collection, query=None):
+        db = self.connect()
+        results = list(db[collection].find(query or {}))
+        for result in results:
+            result['_id'] = str(result['_id'])
+        return results
+
+    def add(self, collection, data):
+        db = self.connect()
+        _id = db[collection].insert(data)
+        newData = db[collection].find_one({'_id': _id})
+        newData['_id'] = str(newData['_id'])
+        return newData
+
+    #def update(self, collection)
+
     def addUser(self, user):
         db = self.connect()
         db.users.insert(user)

+ 22 - 5
eventman_server.py

@@ -11,7 +11,7 @@ import tornado.ioloop
 import tornado.options
 from tornado.options import define, options
 import tornado.web
-from tornado import gen
+from tornado import gen, escape
 
 import backend
 
@@ -63,12 +63,23 @@ json._default_encoder = ImprovedEncoder()
 
 
 class PersonsHandler(BaseHandler):
+    collection = 'persons'
+
     @gen.coroutine
     def get(self, id_=None):
         if id_ is not None:
-            self.write(MOCKUP_PERSONS[int(id_)])
-            return
-        self.write({'persons': MOCKUP_PERSONS.values()})
+            self.write(self.db.get(self.collection, int(id_)))
+        else:
+            self.write({'persons': self.db.query(self.collection)})
+
+    @gen.coroutine
+    def post(self, id_=None, **kwargs):
+        data = escape.json_decode(self.request.body or {})
+        if id_ is None:
+            newData = self.db.add(self.collection, data)
+        else:
+            newData = self.db.update(self.connection, data)
+        self.write(newData)
 
 
 class EventsHandler(BaseHandler):
@@ -81,7 +92,13 @@ class EventsHandler(BaseHandler):
 
     @gen.coroutine
     def post(self, id_=None, **kwargs):
-        data = self.request.body
+        event = self.request.body
+        if id_ is None:
+            newEvent = self.db.addEvent(event)
+            print newEvent
+        else:
+            dbEvent = self.db.findEvent({'id': event['id']})
+
         print 'aaaaaa', id_, data
 
     @gen.coroutine