basic backend
This commit is contained in:
parent
aa060715db
commit
1145ef59fa
2 changed files with 59 additions and 3 deletions
48
backend.py
48
backend.py
|
@ -3,4 +3,52 @@
|
||||||
Classes and functions used to manage events and attendants.
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,8 @@ from tornado.options import define, options
|
||||||
import tornado.web
|
import tornado.web
|
||||||
from tornado import gen
|
from tornado import gen
|
||||||
|
|
||||||
|
import backend
|
||||||
|
|
||||||
|
|
||||||
class BaseHandler(tornado.web.RequestHandler):
|
class BaseHandler(tornado.web.RequestHandler):
|
||||||
def initialize(self, **kwargs):
|
def initialize(self, **kwargs):
|
||||||
|
@ -79,19 +81,25 @@ class EventsHandler(BaseHandler):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
define("port", default=5242, help="run on the given port", type=int)
|
define("port", default=5242, help="run on the given port", type=int)
|
||||||
define("data", default=os.path.join(os.path.dirname(__file__), "data"),
|
define("data", default=os.path.join(os.path.dirname(__file__), "data"),
|
||||||
help="specify the directory used to store the 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("debug", default=False, help="run in debug mode")
|
||||||
define("config", help="read configuration file",
|
define("config", help="read configuration file",
|
||||||
callback=lambda path: tornado.options.parse_config_file(path, final=False))
|
callback=lambda path: tornado.options.parse_config_file(path, final=False))
|
||||||
tornado.options.parse_command_line()
|
tornado.options.parse_command_line()
|
||||||
|
|
||||||
|
db_connector = backend.EventManDB(url=options.mongodb)
|
||||||
|
init_params = dict(db=db_connector)
|
||||||
|
|
||||||
application = tornado.web.Application([
|
application = tornado.web.Application([
|
||||||
(r"/persons/?(?P<id_>\d+)?", PersonsHandler),
|
(r"/persons/?(?P<id_>\d+)?", PersonsHandler, init_params),
|
||||||
(r"/events/?(?P<id_>\d+)?", EventsHandler),
|
(r"/events/?(?P<id_>\d+)?", EventsHandler, init_params),
|
||||||
(r"/(?:index.html)?", RootHandler),
|
(r"/(?:index.html)?", RootHandler, init_params),
|
||||||
(r'/(.*)', tornado.web.StaticFileHandler, {"path": "angular_app"})
|
(r'/(.*)', tornado.web.StaticFileHandler, {"path": "angular_app"})
|
||||||
],
|
],
|
||||||
template_path=os.path.join(os.path.dirname(__file__), "templates"),
|
template_path=os.path.join(os.path.dirname(__file__), "templates"),
|
||||||
|
|
Loading…
Reference in a new issue