From 1145ef59faedc624241e50652065a5eed38818dd Mon Sep 17 00:00:00 2001 From: Davide Alberani Date: Sat, 21 Mar 2015 09:29:01 +0100 Subject: [PATCH] basic backend --- backend.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++ eventman_server.py | 14 +++++++++++--- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/backend.py b/backend.py index a59610f..0fce04c 100644 --- a/backend.py +++ b/backend.py @@ -3,4 +3,52 @@ Classes and functions used to manage events and attendants. """ +import pymongo + + +class EventManDB(object): + connection = None + + def __init__(self, url=None, dbName='eventman'): + self._url = url + self._dbName = dbName + self.connect(url) + + def connect(self, url=None, dbName=None): + if self.connection is not None: + return self.connection + if url: + self._url = url + if dbName: + self._dbName = dbName + self.connection = pymongo.MongoClient(self._url) + self.db = self.connection[self._dbName] + return self.db + + def addUser(self, user): + db = self.connect() + db.users.insert(user) + + def addEvent(self, event): + db = self.connect() + db.events.insert(event) + + def getUser(self, query=None): + db = self.connect() + return db.users.find_one(query or {}) + + def getEvent(self, query): + db = self.connect() + return db.events.find_one(query or {}) + + def getUsers(self, eventID=None): + self.connect() + pass + + def getEvents(self): + self.connect() + pass + + + diff --git a/eventman_server.py b/eventman_server.py index d5220af..25475f5 100755 --- a/eventman_server.py +++ b/eventman_server.py @@ -13,6 +13,8 @@ from tornado.options import define, options import tornado.web from tornado import gen +import backend + class BaseHandler(tornado.web.RequestHandler): def initialize(self, **kwargs): @@ -79,19 +81,25 @@ class EventsHandler(BaseHandler): + def main(): define("port", default=5242, help="run on the given port", type=int) define("data", default=os.path.join(os.path.dirname(__file__), "data"), help="specify the directory used to store the data") + define("mongodb", default=None, + help="URL to MongoDB server", type=str) define("debug", default=False, help="run in debug mode") define("config", help="read configuration file", callback=lambda path: tornado.options.parse_config_file(path, final=False)) tornado.options.parse_command_line() + db_connector = backend.EventManDB(url=options.mongodb) + init_params = dict(db=db_connector) + application = tornado.web.Application([ - (r"/persons/?(?P\d+)?", PersonsHandler), - (r"/events/?(?P\d+)?", EventsHandler), - (r"/(?:index.html)?", RootHandler), + (r"/persons/?(?P\d+)?", PersonsHandler, init_params), + (r"/events/?(?P\d+)?", EventsHandler, init_params), + (r"/(?:index.html)?", RootHandler, init_params), (r'/(.*)', tornado.web.StaticFileHandler, {"path": "angular_app"}) ], template_path=os.path.join(os.path.dirname(__file__), "templates"),