eventman/eventman_server.py

306 lines
12 KiB
Python
Raw Normal View History

2015-03-14 11:12:57 +01:00
#!/usr/bin/env python
"""Event Man(ager)
2015-03-22 11:08:23 +01:00
Your friendly manager of attendees at an event.
2015-03-22 09:36:32 +01:00
Copyright 2015 Davide Alberani <da@erlug.linux.it>
RaspiBO <info@raspibo.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2015-03-14 11:12:57 +01:00
"""
import os
import tornado.httpserver
import tornado.ioloop
import tornado.options
from tornado.options import define, options
import tornado.web
2015-03-21 18:29:19 +01:00
from tornado import gen, escape
2015-03-14 11:12:57 +01:00
2015-03-30 21:39:12 +02:00
import utils
2015-03-21 09:29:01 +01:00
import backend
2015-03-20 23:08:21 +01:00
class BaseHandler(tornado.web.RequestHandler):
2015-03-22 08:58:25 +01:00
"""Base class for request handlers."""
_bool_convert = {
'0': False,
'n': False,
2015-04-14 20:30:05 +02:00
'f': False,
'no': False,
'off': False,
'false': False
}
def tobool(self, obj):
if isinstance(obj, (list, tuple)):
obj = obj[0]
if isinstance(obj, (str, unicode)):
obj = obj.lower()
return bool(self._bool_convert.get(obj, obj))
2015-03-20 23:08:21 +01:00
def initialize(self, **kwargs):
2015-03-22 08:58:25 +01:00
"""Add every passed (key, value) as attributes of the instance."""
2015-03-20 23:08:21 +01:00
for key, value in kwargs.iteritems():
setattr(self, key, value)
class RootHandler(BaseHandler):
2015-03-22 08:58:25 +01:00
"""Handler for the / path."""
2015-03-15 15:47:04 +01:00
angular_app_path = os.path.join(os.path.dirname(__file__), "angular_app")
2015-03-22 08:58:25 +01:00
2015-03-14 13:05:04 +01:00
@gen.coroutine
def get(self, *args, **kwargs):
2015-03-22 08:58:25 +01:00
# serve the ./angular_app/index.html file
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 18:00:08 +01:00
2015-03-21 20:32:39 +01:00
class CollectionHandler(BaseHandler):
2015-03-22 08:58:25 +01:00
"""Base class for handlers that need to interact with the database backend.
Introduce basic CRUD operations."""
# set of documents we're managing (a collection in MongoDB or a table in a SQL database)
2015-03-21 20:32:39 +01:00
collection = None
2015-03-21 18:29:19 +01:00
2015-03-14 17:32:45 +01:00
@gen.coroutine
2015-04-05 00:55:59 +02:00
def get(self, id_=None, resource=None, resource_id=None, **kwargs):
if resource:
2015-04-13 23:25:46 +02:00
# Handle access to sub-resources.
2015-04-04 14:56:41 +02:00
method = getattr(self, 'handle_get_%s' % resource, None)
if method and callable(method):
2015-04-05 22:16:11 +02:00
self.write(method(id_, resource_id, **kwargs))
return
2015-03-15 23:05:59 +01:00
if id_ is not None:
2015-03-22 08:58:25 +01:00
# read a single document
2015-03-21 20:32:39 +01:00
self.write(self.db.get(self.collection, id_))
2015-03-21 18:29:19 +01:00
else:
2015-03-22 08:58:25 +01:00
# return an object containing the list of all objects in the collection;
# e.g.: {'events': [{'_id': 'obj1-id, ...}, {'_id': 'obj2-id, ...}, ...]}
2015-03-22 17:08:25 +01:00
# Please, never return JSON lists that are not encapsulated into an object,
2015-03-22 08:58:25 +01:00
# to avoid XSS vulnerabilities.
2015-03-21 20:32:39 +01:00
self.write({self.collection: self.db.query(self.collection)})
2015-03-21 18:29:19 +01:00
@gen.coroutine
2015-04-05 00:55:59 +02:00
def post(self, id_=None, resource=None, resource_id=None, **kwargs):
data = escape.json_decode(self.request.body or '{}')
2015-04-05 00:55:59 +02:00
if resource:
2015-04-13 23:25:46 +02:00
# Handle access to sub-resources.
2015-04-05 00:55:59 +02:00
method = getattr(self, 'handle_%s_%s' % (self.request.method.lower(), resource), None)
if method and callable(method):
2015-04-05 22:16:11 +02:00
self.write(method(id_, resource_id, data, **kwargs))
return
2015-03-21 18:29:19 +01:00
if id_ is None:
newData = self.db.add(self.collection, data)
else:
merged, newData = self.db.update(self.collection, id_, data)
2015-03-21 18:29:19 +01:00
self.write(newData)
2015-03-14 11:12:57 +01:00
2015-03-22 17:08:25 +01:00
# PUT (update an existing document) is handled by the POST (create a new document) method
2015-03-21 20:32:39 +01:00
put = post
2015-03-15 18:00:08 +01:00
2015-03-22 17:08:25 +01:00
@gen.coroutine
2015-04-05 11:20:57 +02:00
def delete(self, id_=None, resource=None, resource_id=None, **kwargs):
if resource:
2015-04-13 23:25:46 +02:00
# Handle access to sub-resources.
2015-04-05 11:20:57 +02:00
method = getattr(self, 'handle_delete_%s' % resource, None)
if method and callable(method):
2015-04-05 22:16:11 +02:00
self.write(method(id_, resource_id, **kwargs))
return
2015-04-13 23:25:46 +02:00
if id_:
self.db.delete(self.collection, id_)
self.write({'success': True})
2015-03-22 17:08:25 +01:00
2015-03-22 08:58:25 +01:00
2015-03-21 20:32:39 +01:00
class PersonsHandler(CollectionHandler):
2015-03-22 08:58:25 +01:00
"""Handle requests for Persons."""
2015-03-21 20:32:39 +01:00
collection = 'persons'
object_id = 'person_id'
2015-04-05 00:55:59 +02:00
def handle_get_events(self, id_, resource_id=None, **kwargs):
2015-04-13 23:25:46 +02:00
# Get a list of events attended by this person.
# Inside the data of each event, a 'person_data' dictionary is
# created, duplicating the entry for the current person (so that
# there's no need to parse the 'persons' list on the client).
#
# If resource_id is given, only the specified event is considered.
#
# If the 'all' parameter is given, every event (also unattended ones) is returned.
args = self.request.arguments
query = {}
if id_ and not self.tobool(args.get('all')):
query = {'persons.person_id': id_}
2015-04-05 00:55:59 +02:00
if resource_id:
query['_id'] = resource_id
events = self.db.query('events', query)
2015-04-04 14:15:52 +02:00
for event in events:
person_data = {}
2015-04-04 14:56:41 +02:00
for persons in event.get('persons') or []:
if str(persons.get('person_id')) == id_:
person_data = persons
2015-04-04 14:15:52 +02:00
break
event['person_data'] = person_data
2015-04-13 23:25:46 +02:00
if resource_id and events:
return events[0]
2015-04-04 14:15:52 +02:00
return {'events': events}
2015-03-22 08:58:25 +01:00
2015-03-21 20:32:39 +01:00
class EventsHandler(CollectionHandler):
2015-03-22 08:58:25 +01:00
"""Handle requests for Events."""
2015-03-21 20:32:39 +01:00
collection = 'events'
object_id = 'event_id'
2015-04-05 00:55:59 +02:00
def handle_get_persons(self, id_, resource_id=None):
2015-04-13 23:25:46 +02:00
# Return every person registered at this event, or the information
# about a specific person.
2015-04-05 00:55:59 +02:00
query = {'_id': id_}
event = self.db.query('events', query)[0]
if resource_id:
for person in event.get('persons', []):
if str(person.get('person_id')) == resource_id:
return {'person': person}
2015-04-14 20:40:54 +02:00
if resource_id:
return {'person': {}}
2015-04-14 20:39:09 +02:00
return {'persons': event.get('persons') or []}
2015-04-05 00:55:59 +02:00
def handle_post_persons(self, id_, person_id, data):
2015-04-13 23:25:46 +02:00
# Add a person to the list of persons registered at this event.
doc = self.db.query('events',
{'_id': id_, 'persons.person_id': person_id})
if '_id' in data:
del data['_id']
if not doc:
merged, doc = self.db.update('events',
{'_id': id_},
{'persons': data},
operation='append',
create=False)
return {'event': doc}
2015-04-05 00:55:59 +02:00
def handle_put_persons(self, id_, person_id, data):
2015-04-13 23:25:46 +02:00
# Update an existing entry for a person registered at this event.
2015-04-05 00:55:59 +02:00
merged, doc = self.db.update('events',
{'_id': id_, 'persons.person_id': person_id},
data, updateList='persons', create=False)
2015-04-05 00:55:59 +02:00
return {'event': doc}
2015-04-05 11:20:57 +02:00
def handle_delete_persons(self, id_, person_id):
2015-04-13 23:25:46 +02:00
# Remove a specific person from the list of persons registered at this event.
2015-04-05 11:20:57 +02:00
merged, doc = self.db.update('events',
{'_id': id_},
{'persons': {'person_id': person_id}},
operation='delete',
2015-04-05 11:20:57 +02:00
create=False)
return {'event': doc}
2015-03-30 21:39:12 +02:00
class EbCSVImportPersonsHandler(BaseHandler):
"""Importer for CSV files exported from eventbrite."""
2015-03-29 15:05:01 +02:00
csvRemap = {
'Nome evento': 'event_title',
'ID evento': 'event_id',
'N. codice a barre': 'ebqrcode',
'Cognome acquirente': 'surname',
'Nome acquirente': 'name',
'E-mail acquirente': 'email',
'Cognome': 'surname',
'Nome': 'name',
'E-mail': 'email',
2015-04-04 14:56:41 +02:00
'Indirizzo e-mail': 'email',
2015-03-29 15:05:01 +02:00
'Tipologia biglietto': 'ticket_kind',
'Data partecipazione': 'attending_datetime',
'Data check-in': 'checkin_datetime',
'Ordine n.': 'order_nr',
'ID ordine': 'order_nr',
'Prefisso (Sig., Sig.ra, ecc.)': 'name_title',
2015-03-29 15:05:01 +02:00
}
2015-04-14 20:28:57 +02:00
keepPersonData = ('name', 'surname', 'email', 'name_title')
2015-03-29 15:05:01 +02:00
@gen.coroutine
def post(self, **kwargs):
2015-03-29 15:50:36 +02:00
targetEvent = None
try:
targetEvent = self.get_body_argument('targetEvent')
except:
pass
reply = dict(total=0, valid=0, merged=0, new_in_event=0)
2015-03-29 15:05:01 +02:00
for fieldname, contents in self.request.files.iteritems():
for content in contents:
filename = content['filename']
2015-03-30 21:31:09 +02:00
parseStats, persons = utils.csvParse(content['body'], remap=self.csvRemap)
2015-03-29 15:05:01 +02:00
reply['total'] += parseStats['total']
reply['valid'] += parseStats['valid']
2015-03-29 23:47:59 +02:00
for person in persons:
person_data = dict([(k, person[k]) for k in self.keepPersonData
if k in person])
merged, person = self.db.update('persons',
[('email',), ('name', 'surname')],
person_data)
if merged:
2015-03-29 23:47:59 +02:00
reply['merged'] += 1
if targetEvent and person:
2015-04-04 17:26:00 +02:00
event_id = targetEvent
person_id = person['_id']
registered_data = {
'person_id': person_id,
'attended': False,
'from_file': filename}
person.update(registered_data)
if not self.db.query('events',
2015-04-04 14:56:41 +02:00
{'_id': event_id, 'persons.person_id': person_id}):
self.db.update('events', {'_id': event_id},
2015-04-04 14:56:41 +02:00
{'persons': person},
operation='appendUnique')
reply['new_in_event'] += 1
2015-03-29 15:05:01 +02:00
self.write(reply)
2015-03-22 08:58:25 +01:00
def run():
"""Run the Tornado web application."""
# command line arguments; can also be written in a configuration file,
# specified with the --config argument.
2015-03-14 11:12:57 +01:00
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 20:32:39 +01:00
define("mongodbURL", default=None,
2015-03-21 09:29:01 +01:00
help="URL to MongoDB server", type=str)
2015-03-21 20:32:39 +01:00
define("dbName", default='eventman',
help="Name of the MongoDB database to use", 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-22 08:58:25 +01:00
# database backend connector
2015-03-21 20:32:39 +01:00
db_connector = backend.EventManDB(url=options.mongodbURL, dbName=options.dbName)
2015-03-21 09:29:01 +01:00
init_params = dict(db=db_connector)
2015-03-14 11:12:57 +01:00
application = tornado.web.Application([
2015-04-05 00:55:59 +02:00
(r"/persons/?(?P<id_>\w+)?/?(?P<resource>\w+)?/?(?P<resource_id>\w+)?", PersonsHandler, init_params),
(r"/events/?(?P<id_>\w+)?/?(?P<resource>\w+)?/?(?P<resource_id>\w+)?", EventsHandler, init_params),
2015-03-21 09:29:01 +01:00
(r"/(?:index.html)?", RootHandler, init_params),
2015-03-29 15:05:01 +02:00
(r"/ebcsvpersons", EbCSVImportPersonsHandler, 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__':
2015-03-22 08:58:25 +01:00
run()
2015-03-14 11:12:57 +01:00