2016-05-29 08:29:14 +02:00
|
|
|
import itertools
|
2016-03-02 00:46:19 +01:00
|
|
|
import json
|
2015-01-18 15:53:03 +01:00
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
from asgiref.inmemory import ChannelLayer
|
|
|
|
from channels import Channel, Group
|
|
|
|
from channels.auth import channel_session_user, channel_session_user_from_http
|
|
|
|
from django.apps import apps
|
2016-08-08 07:48:11 +02:00
|
|
|
from django.db import transaction
|
2016-05-29 08:29:14 +02:00
|
|
|
from django.utils import timezone
|
2016-01-10 00:17:00 +01:00
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
from ..users.auth import AnonymousUser
|
|
|
|
from ..users.models import User
|
|
|
|
from .access_permissions import BaseAccessPermissions
|
2013-02-27 18:22:24 +01:00
|
|
|
|
2015-01-17 14:01:44 +01:00
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
def get_logged_in_users():
|
2015-01-17 14:01:44 +01:00
|
|
|
"""
|
2016-05-29 08:29:14 +02:00
|
|
|
Helper to get all logged in users.
|
2013-02-27 18:22:24 +01:00
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
Only works with the OpenSlides session backend.
|
2013-10-03 21:49:51 +02:00
|
|
|
"""
|
2016-05-29 08:29:14 +02:00
|
|
|
return User.objects.exclude(session=None).filter(session__expire_date__gte=timezone.now()).distinct()
|
2013-08-04 12:59:11 +02:00
|
|
|
|
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
def get_model_from_collection_string(collection_string):
|
|
|
|
"""
|
|
|
|
Returns a model class which belongs to the argument collection_string.
|
|
|
|
"""
|
|
|
|
def model_generator():
|
2015-01-18 15:53:03 +01:00
|
|
|
"""
|
2016-05-29 08:29:14 +02:00
|
|
|
Yields all models of all apps.
|
2015-01-18 15:53:03 +01:00
|
|
|
"""
|
2016-05-29 08:29:14 +02:00
|
|
|
for app_config in apps.get_app_configs():
|
|
|
|
for model in app_config.get_models():
|
|
|
|
yield model
|
|
|
|
|
|
|
|
for model in model_generator():
|
|
|
|
try:
|
|
|
|
model_collection_string = model.get_collection_string()
|
|
|
|
except AttributeError:
|
|
|
|
# Skip models which do not have the method get_collection_string.
|
|
|
|
pass
|
2016-03-02 00:46:19 +01:00
|
|
|
else:
|
2016-05-29 08:29:14 +02:00
|
|
|
if model_collection_string == collection_string:
|
|
|
|
# The model was found.
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# No model was found in all apps.
|
|
|
|
raise ValueError('Invalid message. A valid collection_string is missing.')
|
|
|
|
return model
|
2016-01-10 00:17:00 +01:00
|
|
|
|
2013-03-27 15:53:31 +01:00
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
# Connected to websocket.connect
|
|
|
|
@channel_session_user_from_http
|
|
|
|
def ws_add(message):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
Group('user-{}'.format(message.user.id)).add(message.reply_channel)
|
2013-02-27 18:22:24 +01:00
|
|
|
|
2015-01-17 14:01:44 +01:00
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
# Connected to websocket.disconnect
|
|
|
|
@channel_session_user
|
|
|
|
def ws_disconnect(message):
|
|
|
|
Group('user-{}'.format(message.user.id)).discard(message.reply_channel)
|
2016-01-10 00:17:00 +01:00
|
|
|
|
2015-01-17 14:01:44 +01:00
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
def send_data(message):
|
2015-01-17 14:01:44 +01:00
|
|
|
"""
|
|
|
|
Informs all users about changed data.
|
|
|
|
|
2016-05-29 08:29:14 +02:00
|
|
|
The argument message has to be a dict with the keywords collection_string
|
|
|
|
(string), pk (positive integer), id_deleted (boolean) and dispatch_uid
|
|
|
|
(string).
|
2015-01-17 14:01:44 +01:00
|
|
|
"""
|
2016-05-29 08:29:14 +02:00
|
|
|
for access_permissions in BaseAccessPermissions.get_all():
|
|
|
|
if access_permissions.get_dispatch_uid() == message['dispatch_uid']:
|
|
|
|
break
|
2015-01-17 14:01:44 +01:00
|
|
|
else:
|
2016-05-29 08:29:14 +02:00
|
|
|
raise ValueError('Invalid message. A valid dispatch_uid is missing.')
|
|
|
|
|
|
|
|
if not message['is_deleted']:
|
|
|
|
Model = get_model_from_collection_string(message['collection_string'])
|
|
|
|
instance = Model.objects.get(pk=message['pk'])
|
|
|
|
full_data = access_permissions.get_full_data(instance)
|
|
|
|
|
|
|
|
# Loop over all logged in users and the anonymous user.
|
|
|
|
for user in itertools.chain(get_logged_in_users(), [AnonymousUser()]):
|
|
|
|
channel = Group('user-{}'.format(user.id))
|
|
|
|
output = {
|
|
|
|
'collection': message['collection_string'],
|
2016-08-03 16:12:52 +02:00
|
|
|
'id': message['pk'], # == instance.get_rest_pk()
|
2016-05-29 08:29:14 +02:00
|
|
|
'action': 'deleted' if message['is_deleted'] else 'changed'}
|
|
|
|
if not message['is_deleted']:
|
|
|
|
data = access_permissions.get_restricted_data(full_data, user)
|
|
|
|
if data is None:
|
|
|
|
# There are no data for the user so he can't see the object. Skip him.
|
|
|
|
continue
|
|
|
|
output['data'] = data
|
|
|
|
channel.send({'text': json.dumps(output)})
|
|
|
|
|
|
|
|
|
|
|
|
def inform_changed_data(instance, is_deleted=False):
|
|
|
|
try:
|
|
|
|
root_instance = instance.get_root_rest_element()
|
|
|
|
except AttributeError:
|
|
|
|
# Instance has no method get_root_rest_element. Just ignore it.
|
2015-01-17 14:01:44 +01:00
|
|
|
pass
|
2016-05-29 08:29:14 +02:00
|
|
|
else:
|
2016-08-08 07:48:11 +02:00
|
|
|
# If currently there is an open database transaction, then the following
|
|
|
|
# function is only called, when the transaction is commited. If there
|
|
|
|
# is currently no transaction, then the function is called immediately.
|
|
|
|
def send_autoupdate():
|
|
|
|
try:
|
|
|
|
Channel('autoupdate.send_data').send({
|
|
|
|
'collection_string': root_instance.get_collection_string(),
|
|
|
|
'pk': root_instance.pk,
|
|
|
|
'is_deleted': is_deleted and instance == root_instance,
|
|
|
|
'dispatch_uid': root_instance.get_access_permissions().get_dispatch_uid()})
|
|
|
|
except ChannelLayer.ChannelFull:
|
|
|
|
pass
|
|
|
|
transaction.on_commit(send_autoupdate)
|
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-05-29 08:29:14 +02:00
|
|
|
inform_changed_data(instance)
|
2016-02-11 11:29:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
def inform_deleted_data_receiver(sender, instance, **kwargs):
|
|
|
|
"""
|
|
|
|
Receiver for the inform_changed_data function to use in a signal.
|
|
|
|
"""
|
2016-05-29 08:29:14 +02:00
|
|
|
inform_changed_data(instance, is_deleted=True)
|