Set new django setting. Put tornado code in a seperate file.

This commit is contained in:
Oskar Hahn 2013-02-27 18:22:24 +01:00
parent 14f062f59f
commit 51a9e41015
4 changed files with 63 additions and 42 deletions

View File

@ -140,3 +140,7 @@ CACHES = {
TEST_RUNNER = 'discover_runner.DiscoverRunner' TEST_RUNNER = 'discover_runner.DiscoverRunner'
TEST_DISCOVER_TOP_LEVEL = os.path.dirname(os.path.dirname(__file__)) 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 = ['*']

View File

@ -20,20 +20,12 @@ import tempfile
import threading import threading
import time import time
import webbrowser import webbrowser
import posixpath
from urllib import unquote
from tornado.options import options, define, parse_command_line from django.conf import ENVIRONMENT_VARIABLE
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 from django.core.management import execute_from_command_line
from openslides import get_version from openslides import get_version
from openslides.utils.tornado_webserver import run_tornado
CONFIG_TEMPLATE = """#!/usr/bin/env python CONFIG_TEMPLATE = """#!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
@ -229,7 +221,7 @@ def setup_django_environment(settings_path):
sys.exit(1) sys.exit(1)
settings_module_dir = os.path.dirname(settings_path) settings_module_dir = os.path.dirname(settings_path)
sys.path.append(settings_module_dir) 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): def detect_listen_opts(address, port):
@ -311,41 +303,13 @@ def create_or_reset_admin_user():
admin.save() 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=[]): 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 # Open the browser
if start_browser_url: if start_browser_url:
start_browser(start_browser_url) start_browser(start_browser_url)
# Start the server # Start the server
app = tornado.wsgi.WSGIContainer(django.core.handlers.wsgi.WSGIHandler()) run_tornado(addr, port)
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): def start_browser(url):

View File

@ -1,8 +1,12 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- 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. :license: GNU GPL, see LICENSE for more details.
""" """

View File

@ -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()