OpenSlides/openslides/utils/autoupdate.py

212 lines
7.1 KiB
Python
Raw Normal View History

import json
import os
import posixpath
from urllib.parse import unquote
from django.conf import settings
2014-10-12 11:11:54 +02:00
from django.core.wsgi import get_wsgi_application
from sockjs.tornado import SockJSConnection, SockJSRouter
2015-01-18 15:53:03 +01:00
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from tornado.httpserver import HTTPServer
2015-01-18 15:53:03 +01:00
from tornado.httputil import HTTPHeaders
from tornado.ioloop import IOLoop
from tornado.options import parse_command_line
from tornado.web import (
Application,
FallbackHandler,
HTTPError,
StaticFileHandler,
)
from tornado.wsgi import WSGIContainer
from .rest_api import get_collection_and_id_from_url
2015-01-18 15:53:03 +01:00
RUNNING_HOST = None
2016-01-10 01:00:54 +01:00
RUNNING_PORT = None
class DjangoStaticFileHandler(StaticFileHandler):
"""
Handels static data by using the django finders.
Only needed in the "small" version with tornado as wsgi server.
"""
def initialize(self):
"""Overwrite some attributes."""
# NOTE: root is never actually used and default_filename is not
# supported (must always be None)
self.root = ''
self.default_filename = None
@classmethod
def get_absolute_path(cls, root, path):
from django.contrib.staticfiles import finders
normalized_path = posixpath.normpath(unquote(path)).lstrip('/')
absolute_path = finders.find(normalized_path)
return absolute_path
def validate_absolute_path(self, root, absolute_path):
# differences from base implementation:
# - we ignore self.root since our files do not necessarily have
# a shared root prefix
# - we do not handle self.default_filename (we do not use it and it
# does not make much sense here anyway)
if absolute_path is None or not os.path.exists(absolute_path):
raise HTTPError(404)
if not os.path.isfile(absolute_path):
raise HTTPError(403, 'The requested resource is not a file.')
return absolute_path
class OpenSlidesSockJSConnection(SockJSConnection):
"""
SockJS connection for OpenSlides.
"""
waiters = set()
def on_open(self, info):
self.waiters.add(self)
self.connection_info = info
def on_close(self):
OpenSlidesSockJSConnection.waiters.remove(self)
def forward_rest_response(self, response):
2015-01-18 15:53:03 +01:00
"""
Sends data to the client of the connection instance.
2015-01-18 15:53:03 +01:00
This method is called after succesful response of AsyncHTTPClient().
See send_object().
2015-01-18 15:53:03 +01:00
"""
if response.code in (200, 404):
# Only send something to the client in case of one of these status
# codes. You have to change the client code (autoupdate.onMessage)
# if you want to handle some more codes.
collection, obj_id = get_collection_and_id_from_url(response.request.url)
data = {
'url': response.request.url,
'status_code': response.code,
'collection': collection,
'id': obj_id,
'data': json.loads(response.body.decode())}
self.send(data)
2015-01-18 15:53:03 +01:00
@classmethod
def send_object(cls, object_url):
"""
Sends an OpenSlides object to all connected clients (waiters).
2015-01-18 15:53:03 +01:00
First, retrieve the object from the OpenSlides REST api using the given
object_url.
2015-01-18 15:53:03 +01:00
"""
# Join network location with object URL.
if settings.OPENSLIDES_WSGI_NETWORK_LOCATION:
wsgi_network_location = settings.OPENSLIDES_WSGI_NETWORK_LOCATION
2016-01-28 16:15:07 +01:00
else:
if RUNNING_HOST == '0.0.0.0':
# Windows can not connect to 0.0.0.0, so connect to localhost instead.
wsgi_network_location = 'http://localhost:{}'.format(RUNNING_PORT)
else:
wsgi_network_location = 'http://{}:{}'.format(RUNNING_HOST, RUNNING_PORT)
url = ''.join((wsgi_network_location, object_url))
# Send out internal HTTP request to get data from the REST api.
2015-01-18 15:53:03 +01:00
for waiter in cls.waiters:
# Initiat new headers object.
2015-01-18 15:53:03 +01:00
headers = HTTPHeaders()
# Read waiter's former cookies and parse session cookie to new header object.
2015-01-30 11:58:36 +01:00
try:
session_cookie = waiter.connection_info.cookies[settings.SESSION_COOKIE_NAME]
except KeyError:
# There is no session cookie
pass
else:
headers.add('Cookie', '%s=%s' % (settings.SESSION_COOKIE_NAME, session_cookie.value))
# Read waiter's language header.
try:
languages = waiter.connection_info.headers['Accept-Language']
except KeyError:
# There is no language header
pass
else:
headers.parse_line('Accept-Language: ' + languages)
# Setup uncompressed request.
2015-01-18 15:53:03 +01:00
request = HTTPRequest(
url=url,
2015-01-18 15:53:03 +01:00
headers=headers,
decompress_response=False)
# Setup non-blocking HTTP client
http_client = AsyncHTTPClient()
# Executes the request, asynchronously returning an HTTPResponse
# and calling waiter's forward_rest_response() method.
http_client.fetch(request, waiter.forward_rest_response)
2015-01-18 15:53:03 +01:00
2015-01-16 14:18:34 +01:00
def run_tornado(addr, port, *args, **kwargs):
"""
Starts the tornado webserver as wsgi server for OpenSlides.
It runs in one thread.
"""
# Save the port and the addr in a global var
2016-01-10 01:00:54 +01:00
global RUNNING_HOST, RUNNING_PORT
RUNNING_HOST = addr
2016-01-10 01:00:54 +01:00
RUNNING_PORT = port
# Don't try to read the command line args from openslides
parse_command_line(args=[])
2013-12-09 18:03:47 +01:00
# Setup WSGIContainer
2014-10-12 11:11:54 +02:00
app = WSGIContainer(get_wsgi_application())
2013-12-09 18:03:47 +01:00
# Collect urls
sock_js_router = SockJSRouter(OpenSlidesSockJSConnection, '/sockjs')
2013-12-09 18:03:47 +01:00
other_urls = [
(r'%s(.*)' % settings.STATIC_URL, DjangoStaticFileHandler),
(r'%s(.*)' % settings.MEDIA_URL, StaticFileHandler, {'path': settings.MEDIA_ROOT}),
2013-12-09 18:03:47 +01:00
('.*', FallbackHandler, dict(fallback=app))]
2013-12-09 18:03:47 +01:00
# Start the application
debug = settings.DEBUG
tornado_app = Application(sock_js_router.urls + other_urls, autoreload=debug, debug=debug)
server = HTTPServer(tornado_app)
2013-12-09 18:03:47 +01:00
server.listen(port=port, address=addr)
IOLoop.instance().start()
2016-01-10 01:00:54 +01:00
# Reset the global vars
RUNNING_HOST = None
2016-01-10 01:00:54 +01:00
RUNNING_PORT = None
def inform_changed_data(*args):
"""
Informs all users about changed data.
The arguments are Django/OpenSlides models.
"""
rest_urls = set()
for instance in args:
try:
rest_urls.add(instance.get_root_rest_url())
except AttributeError:
# Instance has no method get_root_rest_url. Just skip it.
pass
if settings.USE_TORNADO_AS_WSGI_SERVER:
for url in rest_urls:
2015-01-18 15:53:03 +01:00
OpenSlidesSockJSConnection.send_object(url)
else:
pass
# TODO: Implement big varainte with Apache or Nginx as wsgi webserver.
def inform_changed_data_receiver(sender, instance, **kwargs):
"""
Receiver for the inform_changed_data function to use in a signal.
"""
inform_changed_data(instance)