Merge pull request #536 from ostcar/tornado

Tornado
This commit is contained in:
Oskar Hahn 2013-02-18 11:00:23 -08:00
commit c54a7d06f6
6 changed files with 75 additions and 9 deletions

59
openslides/main.py Executable file → Normal file
View File

@ -20,7 +20,16 @@ import tempfile
import threading
import time
import webbrowser
import posixpath
from urllib import unquote
from tornado.options import options, define, parse_command_line
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.wsgi
import django.core.handlers.wsgi
import django.conf
from django.core.management import execute_from_command_line
@ -71,12 +80,12 @@ KEY_LENGTH = 30
_portable_db_path = object()
def process_options(argv=None):
def process_options(argv=None, check_args=True):
if argv is None:
argv = sys.argv[1:]
parser = optparse.OptionParser(
description="Run openslides using django's builtin webserver")
description="Run openslides using the tornado webserver")
parser.add_option("-a", "--address", help="IP Address to listen on.")
parser.add_option("-p", "--port", type="int", help="Port to listen on.")
parser.add_option(
@ -102,7 +111,11 @@ def process_options(argv=None):
if opts.version:
print get_version()
exit(0)
if args:
# Don't check for further args if we come from our custom management
# command, that always sets them
if args and check_args:
sys.stderr.write("This command does not take arguments!\n\n")
parser.print_help()
sys.exit(1)
@ -110,8 +123,8 @@ def process_options(argv=None):
return opts
def main(argv=None):
opts = process_options(argv)
def main(argv=None, check_args=True):
opts = process_options(argv, check_args)
_main(opts)
@ -298,14 +311,42 @@ def create_or_reset_admin_user():
admin.save()
class StaticFileHandler(tornado.web.StaticFileHandler):
"""Handels static data by using the django finders."""
def initialize(self):
"""Overwrite some attributes."""
self.root = ''
self.default_filename = None
def get(self, path, include_body=True):
from django.contrib.staticfiles import finders
normalized_path = posixpath.normpath(unquote(path)).lstrip('/')
absolute_path = finders.find(normalized_path)
return super(StaticFileHandler, self).get(absolute_path, include_body)
def start_openslides(addr, port, start_browser_url=None, extra_args=[]):
argv = ["", "runserver", '--noreload'] + extra_args
argv.append("%s:%d" % (addr, port))
from django.conf import settings
# Set the options
define('port', type=int, default=port)
define('address', default=addr)
# Open the browser
if start_browser_url:
start_browser(start_browser_url)
execute_from_command_line(argv)
# Start the server
app = tornado.wsgi.WSGIContainer(django.core.handlers.wsgi.WSGIHandler())
tornado_app = tornado.web.Application([
(r"%s(.*)" % settings.STATIC_URL, StaticFileHandler),
('.*', tornado.web.FallbackHandler, dict(fallback=app))])
server = tornado.httpserver.HTTPServer(tornado_app)
server.listen(port=options.port,
address=options.address)
tornado.ioloop.IOLoop.instance().start()
def start_browser(url):

View File

View File

@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
"""
Start script for OpenSlides.
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
:license: GNU GPL, see LICENSE for more details.
"""
from django.core.management.base import BaseCommand
from openslides.main import main
class Command(BaseCommand):
'''
Start the application using the tornado webserver
'''
help = 'Start the application using the tornado webserver'
def handle(self, *args, **options):
main(check_args=False)

View File

@ -5,3 +5,4 @@ PIL==1.1.7
coverage==3.6
django-discover-runner==0.2.2
pep8==1.4
tornado==2.4.1

View File

@ -44,6 +44,7 @@ setup(
'django-mptt',
'reportlab',
'pil',
'tornado',
],
entry_points={
'console_scripts': [