2015-03-14 11:12:57 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""Event Man(ager)
|
|
|
|
|
|
|
|
Your friendly manager of attendants at a conference.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
import tornado.httpserver
|
|
|
|
import tornado.ioloop
|
|
|
|
import tornado.options
|
|
|
|
from tornado.options import define, options
|
|
|
|
import tornado.web
|
2015-03-15 23:05:59 +01:00
|
|
|
from tornado import gen
|
2015-03-14 11:12:57 +01:00
|
|
|
|
2015-03-21 09:29:01 +01:00
|
|
|
import backend
|
|
|
|
|
2015-03-20 23:08:21 +01:00
|
|
|
|
|
|
|
class BaseHandler(tornado.web.RequestHandler):
|
|
|
|
def initialize(self, **kwargs):
|
|
|
|
for key, value in kwargs.iteritems():
|
|
|
|
setattr(self, key, value)
|
|
|
|
|
|
|
|
|
|
|
|
class RootHandler(BaseHandler):
|
2015-03-15 15:47:04 +01:00
|
|
|
angular_app_path = os.path.join(os.path.dirname(__file__), "angular_app")
|
2015-03-14 13:05:04 +01:00
|
|
|
@gen.coroutine
|
2015-03-14 11:12:57 +01:00
|
|
|
def get(self):
|
2015-03-15 15:47:04 +01:00
|
|
|
with open(self.angular_app_path + "/index.html", 'r') as fd:
|
|
|
|
self.write(fd.read())
|
2015-03-14 17:32:45 +01:00
|
|
|
|
2015-03-15 23:05:59 +01:00
|
|
|
MOCKUP_PERSONS = {
|
|
|
|
1: {'name': 'Silvia', 'surname': 'Castellari',
|
2015-03-14 17:32:45 +01:00
|
|
|
'email': 'hackinbo.it@gmail.com',
|
|
|
|
'id': 1},
|
2015-03-15 23:05:59 +01:00
|
|
|
2: {'name': 'Daniele', 'surname': 'Castellari',
|
2015-03-14 17:32:45 +01:00
|
|
|
'email': 'hackinbo.it@gmail.com',
|
|
|
|
'id': 2},
|
2015-03-15 23:05:59 +01:00
|
|
|
3: {'name': 'Mario', 'surname': 'Anglani',
|
2015-03-14 17:32:45 +01:00
|
|
|
'email': 'hackinbo.it@gmail.com',
|
2015-03-15 23:05:59 +01:00
|
|
|
'id': 3}
|
|
|
|
}
|
2015-03-14 17:32:45 +01:00
|
|
|
|
2015-03-15 18:00:08 +01:00
|
|
|
import datetime
|
2015-03-15 23:05:59 +01:00
|
|
|
MOCKUP_EVENTS = {
|
|
|
|
1: {'title': 'HackInBo 2015', 'begin-datetime': datetime.datetime(2015, 5, 23, 9, 0),
|
2015-03-15 18:00:08 +01:00
|
|
|
'end-datetime': datetime.datetime(2015, 5, 24, 17, 0),
|
|
|
|
'location': 'Bologna', 'id': 1},
|
2015-03-15 23:05:59 +01:00
|
|
|
2: {'title': 'La fiera del carciofo', 'begin-datetime': datetime.datetime(2015, 6, 23, 9, 0),
|
2015-03-15 18:00:08 +01:00
|
|
|
'end-datetime': datetime.datetime(2015, 6, 24, 17, 0),
|
|
|
|
'location': 'Gatteo a mare', 'id': 2},
|
2015-03-15 23:05:59 +01:00
|
|
|
}
|
2015-03-15 18:00:08 +01:00
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
class ImprovedEncoder(json.JSONEncoder):
|
|
|
|
def default(self, o):
|
|
|
|
if isinstance(o, datetime.datetime):
|
|
|
|
return str(o)
|
|
|
|
return json.JSONEncoder.default(self, o)
|
|
|
|
|
|
|
|
json._default_encoder = ImprovedEncoder()
|
|
|
|
|
2015-03-14 11:12:57 +01:00
|
|
|
|
2015-03-20 23:08:21 +01:00
|
|
|
class PersonsHandler(BaseHandler):
|
2015-03-14 17:32:45 +01:00
|
|
|
@gen.coroutine
|
|
|
|
def get(self, id_=None):
|
2015-03-15 23:05:59 +01:00
|
|
|
if id_ is not None:
|
2015-03-21 11:34:55 +01:00
|
|
|
self.write(MOCKUP_PERSONS[int(id_)])
|
2015-03-15 23:05:59 +01:00
|
|
|
return
|
|
|
|
self.write({'persons': MOCKUP_PERSONS.values()})
|
2015-03-14 11:12:57 +01:00
|
|
|
|
2015-03-15 18:00:08 +01:00
|
|
|
|
2015-03-20 23:08:21 +01:00
|
|
|
class EventsHandler(BaseHandler):
|
2015-03-15 18:00:08 +01:00
|
|
|
@gen.coroutine
|
|
|
|
def get(self, id_=None):
|
2015-03-15 23:05:59 +01:00
|
|
|
if id_ is not None:
|
2015-03-21 11:34:55 +01:00
|
|
|
self.write(MOCKUP_EVENTS[int(id_)])
|
2015-03-15 23:05:59 +01:00
|
|
|
return
|
|
|
|
self.write({'events': MOCKUP_EVENTS.values()})
|
2015-03-15 18:00:08 +01:00
|
|
|
|
2015-03-21 15:33:17 +01:00
|
|
|
@gen.coroutine
|
|
|
|
def post(self, id_=None, **kwargs):
|
|
|
|
data = self.request.body
|
|
|
|
print 'aaaaaa', id_, data
|
|
|
|
|
|
|
|
@gen.coroutine
|
|
|
|
def put(self, id_=None, **kwargs):
|
|
|
|
data = self.request.body
|
|
|
|
print 'aaaaaaa put', id_, data
|
|
|
|
|
2015-03-15 18:00:08 +01:00
|
|
|
|
2015-03-14 11:12:57 +01:00
|
|
|
def main():
|
|
|
|
define("port", default=5242, help="run on the given port", type=int)
|
2015-03-14 17:32:45 +01:00
|
|
|
define("data", default=os.path.join(os.path.dirname(__file__), "data"),
|
|
|
|
help="specify the directory used to store the data")
|
2015-03-21 09:29:01 +01:00
|
|
|
define("mongodb", default=None,
|
|
|
|
help="URL to MongoDB server", type=str)
|
2015-03-14 17:32:45 +01:00
|
|
|
define("debug", default=False, help="run in debug mode")
|
2015-03-14 11:12:57 +01:00
|
|
|
define("config", help="read configuration file",
|
|
|
|
callback=lambda path: tornado.options.parse_config_file(path, final=False))
|
|
|
|
tornado.options.parse_command_line()
|
|
|
|
|
2015-03-21 09:29:01 +01:00
|
|
|
db_connector = backend.EventManDB(url=options.mongodb)
|
|
|
|
init_params = dict(db=db_connector)
|
|
|
|
|
2015-03-14 11:12:57 +01:00
|
|
|
application = tornado.web.Application([
|
2015-03-21 09:29:01 +01:00
|
|
|
(r"/persons/?(?P<id_>\d+)?", PersonsHandler, init_params),
|
|
|
|
(r"/events/?(?P<id_>\d+)?", EventsHandler, init_params),
|
|
|
|
(r"/(?:index.html)?", RootHandler, init_params),
|
2015-03-14 17:32:45 +01:00
|
|
|
(r'/(.*)', tornado.web.StaticFileHandler, {"path": "angular_app"})
|
2015-03-14 11:12:57 +01:00
|
|
|
],
|
2015-03-14 13:05:04 +01:00
|
|
|
template_path=os.path.join(os.path.dirname(__file__), "templates"),
|
2015-03-14 11:12:57 +01:00
|
|
|
static_path=os.path.join(os.path.dirname(__file__), "static"),
|
2015-03-14 13:05:04 +01:00
|
|
|
debug=options.debug)
|
2015-03-14 11:12:57 +01:00
|
|
|
http_server = tornado.httpserver.HTTPServer(application)
|
2015-03-14 11:22:19 +01:00
|
|
|
http_server.listen(options.port)
|
2015-03-14 11:12:57 +01:00
|
|
|
tornado.ioloop.IOLoop.instance().start()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
|