From 51a9e41015d04d6aaf1e73ab5dbb52ee0f10c24d Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Wed, 27 Feb 2013 18:22:24 +0100 Subject: [PATCH] Set new django setting. Put tornado code in a seperate file. --- openslides/global_settings.py | 4 ++ openslides/main.py | 44 ++--------------- .../commands/{run-tornado.py => runserver.py} | 8 ++- openslides/utils/tornado_webserver.py | 49 +++++++++++++++++++ 4 files changed, 63 insertions(+), 42 deletions(-) rename openslides/utils/management/commands/{run-tornado.py => runserver.py} (60%) create mode 100644 openslides/utils/tornado_webserver.py diff --git a/openslides/global_settings.py b/openslides/global_settings.py index e152f4da5..aec335377 100644 --- a/openslides/global_settings.py +++ b/openslides/global_settings.py @@ -140,3 +140,7 @@ CACHES = { TEST_RUNNER = 'discover_runner.DiscoverRunner' TEST_DISCOVER_TOP_LEVEL = os.path.dirname(os.path.dirname(__file__)) + +# Hosts/domain names that are valid for this site; required if DEBUG is False +# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts +ALLOWED_HOSTS = ['*'] diff --git a/openslides/main.py b/openslides/main.py index 39af2bec0..1baa2c62e 100644 --- a/openslides/main.py +++ b/openslides/main.py @@ -20,20 +20,12 @@ 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.conf import ENVIRONMENT_VARIABLE from django.core.management import execute_from_command_line from openslides import get_version +from openslides.utils.tornado_webserver import run_tornado CONFIG_TEMPLATE = """#!/usr/bin/env python # -*- coding: utf-8 -*- @@ -229,7 +221,7 @@ def setup_django_environment(settings_path): sys.exit(1) settings_module_dir = os.path.dirname(settings_path) sys.path.append(settings_module_dir) - os.environ[django.conf.ENVIRONMENT_VARIABLE] = '%s' % settings_module_name + os.environ[ENVIRONMENT_VARIABLE] = '%s' % settings_module_name def detect_listen_opts(address, port): @@ -311,41 +303,13 @@ 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=[]): - 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) # 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() + run_tornado(addr, port) def start_browser(url): diff --git a/openslides/utils/management/commands/run-tornado.py b/openslides/utils/management/commands/runserver.py similarity index 60% rename from openslides/utils/management/commands/run-tornado.py rename to openslides/utils/management/commands/runserver.py index 39c5d6361..ac389cc37 100644 --- a/openslides/utils/management/commands/run-tornado.py +++ b/openslides/utils/management/commands/runserver.py @@ -1,8 +1,12 @@ +#!/usr/bin/env python # -*- coding: utf-8 -*- """ - Start script for OpenSlides. + openslides.utils.management.commands.runserver + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - :copyright: 2011, 2012 by OpenSlides team, see AUTHORS. + Overrides the Django runserver command to start the tornado webserver. + + :copyright: (c) 2011-2013 by the OpenSlides team, see AUTHORS. :license: GNU GPL, see LICENSE for more details. """ diff --git a/openslides/utils/tornado_webserver.py b/openslides/utils/tornado_webserver.py new file mode 100644 index 000000000..26db428b6 --- /dev/null +++ b/openslides/utils/tornado_webserver.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" + openslides.utils.tornado_webserver + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :copyright: (c) 2011-2013 by the OpenSlides team, see AUTHORS. + :license: GNU GPL, see LICENSE for more details. +""" + +import posixpath +from urllib import unquote + +from tornado.httpserver import HTTPServer +from tornado.ioloop import IOLoop +from tornado.web import FallbackHandler, Application, StaticFileHandler +from tornado.wsgi import WSGIContainer +from tornado.options import options, define, parse_command_line + +from django.core.handlers.wsgi import WSGIHandler as Django_WSGIHandler +from django.conf import settings + + +class StaticFileHandler(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 run_tornado(addr, port): + parse_command_line() + app = WSGIContainer(Django_WSGIHandler()) + tornado_app = Application([ + (r"%s(.*)" % settings.STATIC_URL, StaticFileHandler), + ('.*', FallbackHandler, dict(fallback=app))]) + + server = HTTPServer(tornado_app) + server.listen(port=port, + address=addr) + IOLoop.instance().start()