2016-03-02 00:46:19 +01:00
|
|
|
import json
|
2017-01-15 11:55:14 +01:00
|
|
|
import time
|
|
|
|
import warnings
|
2016-10-01 15:26:00 +02:00
|
|
|
from collections import Iterable
|
2015-01-18 15:53:03 +01:00
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
from channels import Channel, Group
|
2017-01-15 11:55:14 +01:00
|
|
|
from channels.asgi import get_channel_layer
|
2016-05-29 08:29:14 +02:00
|
|
|
from channels.auth import channel_session_user, channel_session_user_from_http
|
2017-01-14 12:29:42 +01:00
|
|
|
from django.apps import apps
|
2016-08-08 07:48:11 +02:00
|
|
|
from django.db import transaction
|
2016-01-10 00:17:00 +01:00
|
|
|
|
2016-09-17 22:26:23 +02:00
|
|
|
from ..core.config import config
|
|
|
|
from ..core.models import Projector
|
2017-01-15 13:33:54 +01:00
|
|
|
from .auth import AnonymousUser, anonymous_is_enabled
|
2016-12-17 09:30:20 +01:00
|
|
|
from .cache import websocket_user_cache
|
2016-10-01 15:26:00 +02:00
|
|
|
from .collection import Collection, CollectionElement, CollectionElementList
|
2013-02-27 18:22:24 +01:00
|
|
|
|
2015-01-17 14:01:44 +01:00
|
|
|
|
2017-01-15 11:55:14 +01:00
|
|
|
def send_or_wait(send_func, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Wrapper for channels' send() method.
|
|
|
|
|
|
|
|
If the method send() raises ChannelFull exception the worker waits for 20
|
|
|
|
milliseconds and tries again. After 5 secondes it gives up, drops the
|
|
|
|
channel message and writes a warning to stderr.
|
|
|
|
|
|
|
|
Django channels' consumer atomicity feature is disabled.
|
|
|
|
"""
|
|
|
|
kwargs['immediately'] = True
|
|
|
|
for i in range(250):
|
|
|
|
try:
|
|
|
|
send_func(*args, **kwargs)
|
|
|
|
except get_channel_layer().ChannelFull:
|
|
|
|
time.sleep(0.02)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
warnings.warn(
|
|
|
|
'Channel layer is full. Channel message dropped.',
|
|
|
|
RuntimeWarning
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
@channel_session_user_from_http
|
2016-09-17 22:26:23 +02:00
|
|
|
def ws_add_site(message):
|
2016-05-29 08:29:14 +02:00
|
|
|
"""
|
|
|
|
Adds the websocket connection to a group specific to the connecting user.
|
2013-12-09 18:03:47 +01:00
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
The group with the name 'user-None' stands for all anonymous users.
|
2017-01-14 12:29:42 +01:00
|
|
|
|
2017-01-14 13:02:26 +01:00
|
|
|
Send all "startup-data" through the connection.
|
2016-05-29 08:29:14 +02:00
|
|
|
"""
|
2016-12-17 09:30:20 +01:00
|
|
|
Group('site').add(message.reply_channel)
|
|
|
|
message.channel_session['user_id'] = message.user.id
|
|
|
|
# Saves the reply channel to the user. Uses 0 for anonymous users.
|
|
|
|
websocket_user_cache.add(message.user.id or 0, message.reply_channel.name)
|
2017-01-14 12:29:42 +01:00
|
|
|
|
|
|
|
# Collect all elements that shoud be send to the client when the websocket
|
|
|
|
# connection is established
|
|
|
|
output = []
|
|
|
|
for app in apps.get_app_configs():
|
|
|
|
try:
|
|
|
|
# Get the method get_startup_elements() from an app.
|
|
|
|
# This method has to return an iterable of Collection objects.
|
|
|
|
get_startup_elements = app.get_startup_elements
|
|
|
|
except AttributeError:
|
|
|
|
# Skip apps that do not implement get_startup_elements
|
|
|
|
continue
|
|
|
|
for collection in get_startup_elements():
|
2017-01-15 13:33:54 +01:00
|
|
|
output.extend(collection.as_autoupdate_for_user(message.user.id))
|
2017-01-14 12:29:42 +01:00
|
|
|
|
2017-01-14 13:02:26 +01:00
|
|
|
# Send all data. If there is no data, then only accept the connection
|
2017-01-14 12:29:42 +01:00
|
|
|
if output:
|
2017-01-15 11:55:14 +01:00
|
|
|
send_or_wait(message.reply_channel.send, {'text': json.dumps(output)})
|
2017-01-14 12:29:42 +01:00
|
|
|
else:
|
2017-01-15 11:55:14 +01:00
|
|
|
send_or_wait(message.reply_channel.send, {'accept': True})
|
2013-02-27 18:22:24 +01:00
|
|
|
|
2015-01-17 14:01:44 +01:00
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
@channel_session_user
|
2016-09-17 22:26:23 +02:00
|
|
|
def ws_disconnect_site(message):
|
|
|
|
"""
|
|
|
|
This function is called, when a client on the site disconnects.
|
|
|
|
"""
|
2016-12-17 09:30:20 +01:00
|
|
|
Group('site').discard(message.reply_channel)
|
|
|
|
websocket_user_cache.remove(message.user.id or 0, message.reply_channel.name)
|
2016-01-10 00:17:00 +01:00
|
|
|
|
2015-01-17 14:01:44 +01:00
|
|
|
|
2016-09-17 22:26:23 +02:00
|
|
|
@channel_session_user_from_http
|
|
|
|
def ws_add_projector(message, projector_id):
|
2015-01-17 14:01:44 +01:00
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
Adds the websocket connection to a group specific to the projector with the given id.
|
|
|
|
Also sends all data that are shown on the projector.
|
2015-01-17 14:01:44 +01:00
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
user = message.user
|
|
|
|
# user is the django anonymous user. We have our own.
|
2017-01-15 13:33:54 +01:00
|
|
|
if user.is_anonymous and anonymous_is_enabled():
|
2016-09-17 22:26:23 +02:00
|
|
|
user = AnonymousUser()
|
|
|
|
|
|
|
|
if not user.has_perm('core.can_see_projector'):
|
2017-01-15 11:55:14 +01:00
|
|
|
send_or_wait(message.reply_channel.send, {'text': 'No permissions to see this projector.'})
|
2015-01-17 14:01:44 +01:00
|
|
|
else:
|
2016-09-17 22:26:23 +02:00
|
|
|
try:
|
|
|
|
projector = Projector.objects.get(pk=projector_id)
|
|
|
|
except Projector.DoesNotExist:
|
2017-01-15 11:55:14 +01:00
|
|
|
send_or_wait(message.reply_channel.send, {'text': 'The projector {} does not exist.'.format(projector_id)})
|
2016-09-17 22:26:23 +02:00
|
|
|
else:
|
|
|
|
# At first, the client is added to the projector group, so it is
|
|
|
|
# informed if the data change.
|
|
|
|
Group('projector-{}'.format(projector_id)).add(message.reply_channel)
|
|
|
|
|
2016-10-01 12:58:49 +02:00
|
|
|
# Then it is also added to the global projector group which is
|
|
|
|
# used for broadcasting data.
|
|
|
|
Group('projector-all').add(message.reply_channel)
|
|
|
|
|
|
|
|
# Now check whether broadcast is active at the moment. If yes,
|
|
|
|
# change the local projector variable.
|
|
|
|
if config['projector_broadcast'] > 0:
|
|
|
|
projector = Projector.objects.get(pk=config['projector_broadcast'])
|
|
|
|
|
|
|
|
# Collect all elements that are on the projector.
|
2016-09-30 23:39:42 +02:00
|
|
|
output = []
|
|
|
|
for requirement in projector.get_all_requirements():
|
|
|
|
required_collection_element = CollectionElement.from_instance(requirement)
|
|
|
|
output.append(required_collection_element.as_autoupdate_for_projector())
|
2016-05-29 08:29:14 +02:00
|
|
|
|
2016-10-01 12:58:49 +02:00
|
|
|
# Collect all config elements.
|
2016-09-18 16:00:31 +02:00
|
|
|
collection = Collection(config.get_collection_string())
|
|
|
|
output.extend(collection.as_autoupdate_for_projector())
|
2016-09-17 22:26:23 +02:00
|
|
|
|
2016-10-01 12:58:49 +02:00
|
|
|
# Collect the projector instance.
|
2016-09-17 22:26:23 +02:00
|
|
|
collection_element = CollectionElement.from_instance(projector)
|
|
|
|
output.append(collection_element.as_autoupdate_for_projector())
|
|
|
|
|
2016-10-01 12:58:49 +02:00
|
|
|
# Send all the data that were only collected before.
|
2017-01-15 11:55:14 +01:00
|
|
|
send_or_wait(message.reply_channel.send, {'text': json.dumps(output)})
|
2016-09-17 22:26:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
def ws_disconnect_projector(message, projector_id):
|
|
|
|
"""
|
|
|
|
This function is called, when a client on the projector disconnects.
|
|
|
|
"""
|
|
|
|
Group('projector-{}'.format(projector_id)).discard(message.reply_channel)
|
|
|
|
|
|
|
|
|
2016-09-30 23:39:42 +02:00
|
|
|
def send_data(message):
|
2016-09-17 22:26:23 +02:00
|
|
|
"""
|
2016-09-30 23:39:42 +02:00
|
|
|
Informs all site users and projector clients about changed data.
|
2016-09-17 22:26:23 +02:00
|
|
|
"""
|
2016-10-01 15:26:00 +02:00
|
|
|
collection_elements = CollectionElementList.from_channels_message(message)
|
2016-12-17 09:30:20 +01:00
|
|
|
for user_id, channel_names in websocket_user_cache.get_all().items():
|
|
|
|
if not user_id:
|
|
|
|
# Anonymous user
|
|
|
|
user = AnonymousUser()
|
|
|
|
else:
|
|
|
|
user = CollectionElement.from_values('users/user', user_id)
|
2016-10-01 15:26:00 +02:00
|
|
|
output = collection_elements.as_autoupdate_for_user(user)
|
2016-12-17 09:30:20 +01:00
|
|
|
for channel_name in channel_names:
|
2017-01-15 11:55:14 +01:00
|
|
|
send_or_wait(Channel(channel_name).send, {'text': json.dumps(output)})
|
2016-09-18 16:00:31 +02:00
|
|
|
|
2016-10-01 12:58:49 +02:00
|
|
|
# Check whether broadcast is active at the moment and set the local
|
|
|
|
# projector queryset.
|
|
|
|
if config['projector_broadcast'] > 0:
|
|
|
|
queryset = Projector.objects.filter(pk=config['projector_broadcast'])
|
|
|
|
else:
|
|
|
|
queryset = Projector.objects.all()
|
|
|
|
|
2016-09-30 23:39:42 +02:00
|
|
|
# Loop over all projectors and send data that they need.
|
2016-10-01 12:58:49 +02:00
|
|
|
for projector in queryset:
|
2016-10-01 15:26:00 +02:00
|
|
|
output = []
|
|
|
|
for collection_element in collection_elements:
|
|
|
|
if collection_element.is_deleted():
|
|
|
|
output.append(collection_element.as_autoupdate_for_projector())
|
|
|
|
else:
|
|
|
|
for element in projector.get_collection_elements_required_for_this(collection_element):
|
2016-10-07 17:02:55 +02:00
|
|
|
output.append(element.as_autoupdate_for_projector())
|
2016-09-17 22:26:23 +02:00
|
|
|
if output:
|
2016-10-01 12:58:49 +02:00
|
|
|
if config['projector_broadcast'] > 0:
|
2017-01-15 11:55:14 +01:00
|
|
|
send_or_wait(
|
|
|
|
Group('projector-all').send,
|
2016-10-01 12:58:49 +02:00
|
|
|
{'text': json.dumps(output)})
|
|
|
|
else:
|
2017-01-15 11:55:14 +01:00
|
|
|
send_or_wait(
|
|
|
|
Group('projector-{}'.format(projector.pk)).send,
|
2016-10-01 12:58:49 +02:00
|
|
|
{'text': json.dumps(output)})
|
2016-05-29 08:29:14 +02:00
|
|
|
|
|
|
|
|
2016-10-01 15:26:00 +02:00
|
|
|
def inform_changed_data(instances, information=None):
|
2016-09-18 16:00:31 +02:00
|
|
|
"""
|
|
|
|
Informs the autoupdate system and the caching system about the creation or
|
|
|
|
update of an element.
|
2016-10-01 15:26:00 +02:00
|
|
|
|
|
|
|
The argument instances can be one instance or an interable over instances.
|
2016-09-18 16:00:31 +02:00
|
|
|
"""
|
2016-10-01 15:26:00 +02:00
|
|
|
root_instances = set()
|
|
|
|
if not isinstance(instances, Iterable):
|
2016-10-14 21:48:02 +02:00
|
|
|
# Make sure instances is an iterable
|
2016-10-01 15:26:00 +02:00
|
|
|
instances = (instances, )
|
|
|
|
for instance in instances:
|
|
|
|
try:
|
|
|
|
root_instances.add(instance.get_root_rest_element())
|
|
|
|
except AttributeError:
|
|
|
|
# Instance has no method get_root_rest_element. Just ignore it.
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Generates an collection element list for the root_instances.
|
|
|
|
collection_elements = CollectionElementList()
|
|
|
|
for root_instance in root_instances:
|
|
|
|
collection_elements.append(
|
|
|
|
CollectionElement.from_instance(
|
|
|
|
root_instance,
|
|
|
|
information=information))
|
|
|
|
# If currently there is an open database transaction, then the
|
|
|
|
# send_autoupdate function is only called, when the transaction is
|
|
|
|
# commited. If there is currently no transaction, then the function
|
|
|
|
# is called immediately.
|
|
|
|
transaction.on_commit(lambda: send_autoupdate(collection_elements))
|
2015-01-17 14:01:44 +01:00
|
|
|
|
2016-09-30 20:42:58 +02:00
|
|
|
|
2016-10-01 15:26:00 +02:00
|
|
|
def inform_deleted_data(*args, information=None):
|
2015-01-17 14:01:44 +01:00
|
|
|
"""
|
2016-09-18 16:00:31 +02:00
|
|
|
Informs the autoupdate system and the caching system about the deletion of
|
2016-10-01 15:26:00 +02:00
|
|
|
elements.
|
|
|
|
|
|
|
|
The function has to be called with the attributes collection_string and id.
|
|
|
|
Multible elements can be used. For example:
|
|
|
|
|
|
|
|
inform_deleted_data('motions/motion', 1, 'assignments/assignment', 5)
|
|
|
|
|
|
|
|
The argument information is added to each collection element.
|
|
|
|
"""
|
|
|
|
if len(args) % 2 or not args:
|
|
|
|
raise ValueError(
|
|
|
|
"inform_deleted_data has to be called with the same number of "
|
|
|
|
"collection strings and ids. It has to be at least one collection "
|
|
|
|
"string and one id.")
|
|
|
|
|
|
|
|
# Go through each pair of collection_string and id and generate a collection
|
|
|
|
# element from it.
|
|
|
|
collection_elements = CollectionElementList()
|
|
|
|
for index in range(0, len(args), 2):
|
|
|
|
collection_elements.append(CollectionElement.from_values(
|
|
|
|
collection_string=args[index],
|
|
|
|
id=args[index + 1],
|
|
|
|
deleted=True,
|
|
|
|
information=information))
|
2016-09-30 20:42:58 +02:00
|
|
|
# If currently there is an open database transaction, then the
|
|
|
|
# send_autoupdate function is only called, when the transaction is
|
|
|
|
# commited. If there is currently no transaction, then the function
|
|
|
|
# is called immediately.
|
2016-10-01 15:26:00 +02:00
|
|
|
transaction.on_commit(lambda: send_autoupdate(collection_elements))
|
2016-02-11 11:29:19 +01:00
|
|
|
|
|
|
|
|
2016-10-01 15:26:00 +02:00
|
|
|
def send_autoupdate(collection_elements):
|
2016-02-11 11:29:19 +01:00
|
|
|
"""
|
2016-10-01 15:26:00 +02:00
|
|
|
Helper function, that sends collection_elements through a channel to the
|
2016-09-18 16:00:31 +02:00
|
|
|
autoupdate system.
|
2016-10-01 15:26:00 +02:00
|
|
|
|
|
|
|
Does nothing if collection_elements is empty.
|
2016-02-11 11:29:19 +01:00
|
|
|
"""
|
2016-10-01 15:26:00 +02:00
|
|
|
if collection_elements:
|
2017-01-15 11:55:14 +01:00
|
|
|
send_or_wait(
|
|
|
|
Channel('autoupdate.send_data').send,
|
|
|
|
collection_elements.as_channels_message())
|