2016-03-02 00:46:19 +01:00
|
|
|
import json
|
2013-11-12 23:01:37 +01:00
|
|
|
import os
|
2013-02-27 18:22:24 +01:00
|
|
|
import posixpath
|
2016-02-11 22:58:32 +01:00
|
|
|
from importlib import import_module
|
2014-08-16 09:25:18 +02:00
|
|
|
from urllib.parse import unquote
|
2016-02-11 22:58:32 +01:00
|
|
|
|
2013-09-25 10:01:01 +02:00
|
|
|
from django.conf import settings
|
2014-10-12 11:11:54 +02:00
|
|
|
from django.core.wsgi import get_wsgi_application
|
2015-06-16 10:37:23 +02:00
|
|
|
from sockjs.tornado import SockJSConnection, SockJSRouter
|
|
|
|
from tornado.httpserver import HTTPServer
|
2013-02-27 18:22:24 +01:00
|
|
|
from tornado.ioloop import IOLoop
|
2013-09-25 10:01:01 +02:00
|
|
|
from tornado.options import parse_command_line
|
2013-11-12 23:01:37 +01:00
|
|
|
from tornado.web import (
|
|
|
|
Application,
|
|
|
|
FallbackHandler,
|
2015-06-16 10:37:23 +02:00
|
|
|
HTTPError,
|
2013-11-12 23:01:37 +01:00
|
|
|
StaticFileHandler,
|
|
|
|
)
|
2013-02-27 18:22:24 +01:00
|
|
|
from tornado.wsgi import WSGIContainer
|
2016-02-11 22:58:32 +01:00
|
|
|
|
2016-03-02 00:46:19 +01:00
|
|
|
from ..users.auth import AnonymousUser, get_user
|
|
|
|
from .access_permissions import BaseAccessPermissions
|
2015-01-18 15:53:03 +01:00
|
|
|
|
2016-01-10 00:17:00 +01:00
|
|
|
RUNNING_HOST = None
|
2016-01-10 01:00:54 +01:00
|
|
|
RUNNING_PORT = None
|
2016-01-10 00:17:00 +01:00
|
|
|
|
2013-02-27 18:22:24 +01:00
|
|
|
|
2013-02-16 16:19:20 +01:00
|
|
|
class DjangoStaticFileHandler(StaticFileHandler):
|
2015-01-17 14:01:44 +01:00
|
|
|
"""
|
|
|
|
Handels static data by using the django finders.
|
|
|
|
|
|
|
|
Only needed in the "small" version with tornado as wsgi server.
|
|
|
|
"""
|
2013-02-27 18:22:24 +01:00
|
|
|
|
|
|
|
def initialize(self):
|
|
|
|
"""Overwrite some attributes."""
|
2013-11-12 23:01:37 +01:00
|
|
|
# NOTE: root is never actually used and default_filename is not
|
|
|
|
# supported (must always be None)
|
2014-08-16 09:25:18 +02:00
|
|
|
self.root = ''
|
2013-02-27 18:22:24 +01:00
|
|
|
self.default_filename = None
|
|
|
|
|
2013-11-12 23:01:37 +01:00
|
|
|
@classmethod
|
|
|
|
def get_absolute_path(cls, root, path):
|
2013-02-27 18:22:24 +01:00
|
|
|
from django.contrib.staticfiles import finders
|
|
|
|
normalized_path = posixpath.normpath(unquote(path)).lstrip('/')
|
|
|
|
absolute_path = finders.find(normalized_path)
|
2013-11-12 23:01:37 +01:00
|
|
|
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)
|
2013-12-23 17:42:11 +01:00
|
|
|
if absolute_path is None or not os.path.exists(absolute_path):
|
2013-11-12 23:01:37 +01:00
|
|
|
raise HTTPError(404)
|
|
|
|
if not os.path.isfile(absolute_path):
|
2013-12-23 17:42:11 +01:00
|
|
|
raise HTTPError(403, 'The requested resource is not a file.')
|
2013-11-12 23:01:37 +01:00
|
|
|
return absolute_path
|
2013-02-27 18:22:24 +01:00
|
|
|
|
|
|
|
|
2015-01-17 14:01:44 +01:00
|
|
|
class OpenSlidesSockJSConnection(SockJSConnection):
|
2013-10-03 21:49:51 +02:00
|
|
|
"""
|
2015-02-04 00:08:38 +01:00
|
|
|
SockJS connection for OpenSlides.
|
2013-10-03 21:49:51 +02:00
|
|
|
"""
|
2013-08-04 12:59:11 +02:00
|
|
|
waiters = set()
|
|
|
|
|
2015-02-04 00:08:38 +01:00
|
|
|
def on_open(self, info):
|
|
|
|
self.waiters.add(self)
|
|
|
|
self.connection_info = info
|
2013-08-04 12:59:11 +02:00
|
|
|
|
|
|
|
def on_close(self):
|
2015-01-17 14:01:44 +01:00
|
|
|
OpenSlidesSockJSConnection.waiters.remove(self)
|
2013-08-04 12:59:11 +02:00
|
|
|
|
2015-01-18 15:53:03 +01:00
|
|
|
@classmethod
|
2016-03-02 00:46:19 +01:00
|
|
|
def send_object(cls, json_container):
|
2015-01-18 15:53:03 +01:00
|
|
|
"""
|
2015-02-04 00:08:38 +01:00
|
|
|
Sends an OpenSlides object to all connected clients (waiters).
|
2015-01-18 15:53:03 +01:00
|
|
|
"""
|
2016-03-02 00:46:19 +01:00
|
|
|
# Load JSON
|
|
|
|
container = json.loads(json_container)
|
|
|
|
|
|
|
|
# Search our AccessPermission class.
|
|
|
|
for access_permissions in BaseAccessPermissions.get_all():
|
|
|
|
if access_permissions.get_dispatch_uid() == container.get('dispatch_uid'):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise ValueError('Invalid container. A valid dispatch_uid is missing.')
|
|
|
|
|
|
|
|
# Loop over all waiters
|
2015-01-18 15:53:03 +01:00
|
|
|
for waiter in cls.waiters:
|
2016-02-11 22:58:32 +01:00
|
|
|
# Read waiter's former cookies and parse session cookie to get user instance.
|
2015-01-30 11:58:36 +01:00
|
|
|
try:
|
|
|
|
session_cookie = waiter.connection_info.cookies[settings.SESSION_COOKIE_NAME]
|
2016-02-11 22:58:32 +01:00
|
|
|
except KeyError:
|
|
|
|
# There is no session cookie so use anonymous user here.
|
|
|
|
user = AnonymousUser()
|
|
|
|
else:
|
|
|
|
# Get session from session store and use it to retrieve the user.
|
2016-02-11 11:29:19 +01:00
|
|
|
engine = import_module(settings.SESSION_ENGINE)
|
2016-02-11 22:58:32 +01:00
|
|
|
session = engine.SessionStore(session_cookie.value)
|
|
|
|
fake_request = type('FakeRequest', (), {})()
|
|
|
|
fake_request.session = session
|
|
|
|
user = get_user(fake_request)
|
2016-03-02 00:46:19 +01:00
|
|
|
|
|
|
|
# Two cases: models instance was changed or deleted
|
|
|
|
if container.get('action') == 'changed':
|
|
|
|
data = access_permissions.get_restricted_data(container.get('full_data'), user)
|
|
|
|
if data is None:
|
|
|
|
# There are no data for the user so he can't see the object. Skip him.
|
|
|
|
break
|
|
|
|
output = {
|
|
|
|
'status_code': 200, # TODO: Refactor this. Use strings like 'change' or 'delete'.
|
|
|
|
'collection': container['collection_string'],
|
|
|
|
'id': container['rest_pk'],
|
|
|
|
'data': data}
|
|
|
|
elif container.get('action') == 'deleted':
|
|
|
|
output = {
|
|
|
|
'status_code': 404, # TODO: Refactor this. Use strings like 'change' or 'delete'.
|
|
|
|
'collection': container['collection_string'],
|
|
|
|
'id': container['rest_pk']}
|
|
|
|
else:
|
|
|
|
raise ValueError('Invalid container. A valid action is missing.')
|
|
|
|
|
|
|
|
# Send output to the waiter (client).
|
|
|
|
waiter.send(output)
|
2015-01-18 15:53:03 +01:00
|
|
|
|
2013-08-04 12:59:11 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
def run_tornado(addr, port, *args, **kwargs):
|
2015-01-17 14:01:44 +01:00
|
|
|
"""
|
|
|
|
Starts the tornado webserver as wsgi server for OpenSlides.
|
|
|
|
|
|
|
|
It runs in one thread.
|
|
|
|
"""
|
2016-01-10 00:17:00 +01:00
|
|
|
# Save the port and the addr in a global var
|
2016-01-10 01:00:54 +01:00
|
|
|
global RUNNING_HOST, RUNNING_PORT
|
2016-01-10 00:17:00 +01:00
|
|
|
RUNNING_HOST = addr
|
2016-01-10 01:00:54 +01:00
|
|
|
RUNNING_PORT = port
|
2016-01-10 00:17:00 +01:00
|
|
|
|
2013-03-27 15:53:31 +01:00
|
|
|
# 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
|
2015-01-17 14:01:44 +01:00
|
|
|
sock_js_router = SockJSRouter(OpenSlidesSockJSConnection, '/sockjs')
|
2013-12-09 18:03:47 +01:00
|
|
|
other_urls = [
|
2015-10-21 22:44:07 +02:00
|
|
|
(r'%s(.*)' % settings.STATIC_URL, DjangoStaticFileHandler),
|
2013-02-16 16:19:20 +01:00
|
|
|
(r'%s(.*)' % settings.MEDIA_URL, StaticFileHandler, {'path': settings.MEDIA_ROOT}),
|
2013-12-09 18:03:47 +01:00
|
|
|
('.*', FallbackHandler, dict(fallback=app))]
|
2013-02-27 18:22:24 +01:00
|
|
|
|
2013-12-09 18:03:47 +01:00
|
|
|
# Start the application
|
2014-05-19 22:22:56 +02:00
|
|
|
debug = settings.DEBUG
|
2015-09-07 16:46:04 +02:00
|
|
|
tornado_app = Application(sock_js_router.urls + other_urls, autoreload=debug, debug=debug)
|
2013-02-27 18:22:24 +01:00
|
|
|
server = HTTPServer(tornado_app)
|
2013-12-09 18:03:47 +01:00
|
|
|
server.listen(port=port, address=addr)
|
2013-02-27 18:22:24 +01:00
|
|
|
IOLoop.instance().start()
|
2015-01-17 14:01:44 +01:00
|
|
|
|
2016-01-10 01:00:54 +01:00
|
|
|
# Reset the global vars
|
2016-01-10 00:17:00 +01:00
|
|
|
RUNNING_HOST = None
|
2016-01-10 01:00:54 +01:00
|
|
|
RUNNING_PORT = None
|
2016-01-10 00:17:00 +01:00
|
|
|
|
2015-01-17 14:01:44 +01:00
|
|
|
|
2016-02-11 11:29:19 +01:00
|
|
|
def inform_changed_data(is_delete, *args):
|
2015-01-17 14:01:44 +01:00
|
|
|
"""
|
|
|
|
Informs all users about changed data.
|
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
The first argument is whether the object or the objects are deleted.
|
|
|
|
The other arguments are the changed or deleted Django/OpenSlides model
|
|
|
|
instances.
|
2015-01-17 14:01:44 +01:00
|
|
|
"""
|
|
|
|
if settings.USE_TORNADO_AS_WSGI_SERVER:
|
2016-02-11 22:58:32 +01:00
|
|
|
for instance in args:
|
|
|
|
try:
|
|
|
|
root_instance = instance.get_root_rest_element()
|
|
|
|
except AttributeError:
|
|
|
|
# Instance has no method get_root_rest_element. Just skip it.
|
|
|
|
pass
|
|
|
|
else:
|
2016-03-02 00:46:19 +01:00
|
|
|
access_permissions = root_instance.get_access_permissions()
|
|
|
|
container = {
|
|
|
|
'dispatch_uid': access_permissions.get_dispatch_uid(),
|
|
|
|
'collection_string': root_instance.get_collection_string(),
|
|
|
|
'rest_pk': root_instance.get_rest_pk()}
|
2016-02-11 22:58:32 +01:00
|
|
|
if is_delete and instance == root_instance:
|
|
|
|
# A root instance is deleted.
|
2016-03-02 00:46:19 +01:00
|
|
|
container['action'] = 'deleted'
|
2016-02-11 22:58:32 +01:00
|
|
|
else:
|
|
|
|
# A non root instance is deleted or any instance is just changed.
|
2016-03-02 00:46:19 +01:00
|
|
|
container['action'] = 'changed'
|
2016-02-11 22:58:32 +01:00
|
|
|
root_instance.refresh_from_db()
|
2016-03-02 00:46:19 +01:00
|
|
|
container['full_data'] = access_permissions.get_full_data(root_instance)
|
|
|
|
OpenSlidesSockJSConnection.send_object(json.dumps(container))
|
2015-01-17 14:01:44 +01:00
|
|
|
else:
|
|
|
|
pass
|
2016-02-11 22:58:32 +01:00
|
|
|
# TODO: Implement big variant with Apache or Nginx as WSGI webserver.
|
2015-01-17 14:01:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
def inform_changed_data_receiver(sender, instance, **kwargs):
|
|
|
|
"""
|
|
|
|
Receiver for the inform_changed_data function to use in a signal.
|
|
|
|
"""
|
2016-02-11 11:29:19 +01:00
|
|
|
inform_changed_data(False, instance)
|
|
|
|
|
|
|
|
|
|
|
|
def inform_deleted_data_receiver(sender, instance, **kwargs):
|
|
|
|
"""
|
|
|
|
Receiver for the inform_changed_data function to use in a signal.
|
|
|
|
"""
|
|
|
|
inform_changed_data(True, instance)
|