basic backend

This commit is contained in:
Davide Alberani 2015-03-21 09:29:01 +01:00
parent aa060715db
commit 1145ef59fa
2 changed files with 59 additions and 3 deletions

View file

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

View file

@ -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<id_>\d+)?", PersonsHandler),
(r"/events/?(?P<id_>\d+)?", EventsHandler),
(r"/(?:index.html)?", RootHandler),
(r"/persons/?(?P<id_>\d+)?", PersonsHandler, init_params),
(r"/events/?(?P<id_>\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"),