2018-11-04 14:02:30 +01:00
|
|
|
from asgiref.sync import async_to_sync
|
2015-09-07 16:46:04 +02:00
|
|
|
from django.conf import settings
|
2018-11-04 14:02:30 +01:00
|
|
|
from django.db import models, transaction
|
2016-10-21 11:05:24 +02:00
|
|
|
from django.utils.timezone import now
|
2015-02-18 01:45:39 +01:00
|
|
|
from jsonfield import JSONField
|
2014-01-28 08:32:26 +01:00
|
|
|
|
2018-11-04 14:02:30 +01:00
|
|
|
from ..utils.autoupdate import Element
|
|
|
|
from ..utils.cache import element_cache, get_element_id
|
2016-10-01 12:58:49 +02:00
|
|
|
from ..utils.models import RESTModelMixin
|
2017-08-30 00:07:54 +02:00
|
|
|
from ..utils.projector import get_all_projector_elements
|
2016-02-11 22:58:32 +01:00
|
|
|
from .access_permissions import (
|
|
|
|
ChatMessageAccessPermissions,
|
|
|
|
ConfigAccessPermissions,
|
2016-10-21 11:05:24 +02:00
|
|
|
CountdownAccessPermissions,
|
2018-11-04 14:02:30 +01:00
|
|
|
HistoryAccessPermissions,
|
2016-02-11 22:58:32 +01:00
|
|
|
ProjectorAccessPermissions,
|
2016-10-21 11:05:24 +02:00
|
|
|
ProjectorMessageAccessPermissions,
|
2016-02-11 22:58:32 +01:00
|
|
|
TagAccessPermissions,
|
|
|
|
)
|
2015-02-18 01:45:39 +01:00
|
|
|
from .exceptions import ProjectorException
|
2014-10-11 14:34:49 +02:00
|
|
|
|
2014-01-28 08:32:26 +01:00
|
|
|
|
2016-10-01 01:30:55 +02:00
|
|
|
class ProjectorManager(models.Manager):
|
|
|
|
"""
|
|
|
|
Customized model manager to support our get_full_queryset method.
|
|
|
|
"""
|
|
|
|
def get_full_queryset(self):
|
|
|
|
"""
|
|
|
|
Returns the normal queryset with all projectors. In the background
|
|
|
|
projector defaults are prefetched from the database.
|
|
|
|
"""
|
|
|
|
return self.get_queryset().prefetch_related(
|
|
|
|
'projectiondefaults')
|
|
|
|
|
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
class Projector(RESTModelMixin, models.Model):
|
2014-01-28 08:32:26 +01:00
|
|
|
"""
|
2016-10-05 18:25:50 +02:00
|
|
|
Model for all projectors.
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2015-09-06 13:28:25 +02:00
|
|
|
The config field contains a dictionary which uses UUIDs as keys. Every
|
|
|
|
element must have at least the property "name". The property "stable"
|
|
|
|
is to set whether this element should disappear on prune or clear
|
|
|
|
requests.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
{
|
|
|
|
"881d875cf01741718ca926279ac9c99c": {
|
2016-09-18 22:14:24 +02:00
|
|
|
"name": "topics/topic",
|
2015-09-14 23:16:31 +02:00
|
|
|
"id": 1
|
|
|
|
},
|
2015-09-06 13:28:25 +02:00
|
|
|
"191c0878cdc04abfbd64f3177a21891a": {
|
|
|
|
"name": "core/countdown",
|
|
|
|
"stable": true,
|
2015-09-14 23:16:31 +02:00
|
|
|
"status": "stop",
|
2015-09-06 13:28:25 +02:00
|
|
|
"countdown_time": 20,
|
2015-09-14 23:16:31 +02:00
|
|
|
"visable": true,
|
|
|
|
"default": 42
|
|
|
|
},
|
2015-09-06 13:28:25 +02:00
|
|
|
"db670aa8d3ed4aabb348e752c75aeaaf": {
|
|
|
|
"name": "core/clock",
|
2015-09-14 23:16:31 +02:00
|
|
|
"stable": true
|
|
|
|
}
|
2015-09-06 13:28:25 +02:00
|
|
|
}
|
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
If the config field is empty or invalid the projector shows a default
|
2015-09-06 13:28:25 +02:00
|
|
|
slide.
|
|
|
|
|
2015-09-14 23:16:31 +02:00
|
|
|
There are two additional fields to control the behavior of the projector
|
|
|
|
view itself: scale and scroll.
|
|
|
|
|
2015-09-06 13:28:25 +02:00
|
|
|
The projector can be controlled using the REST API with POST requests
|
|
|
|
on e. g. the URL /rest/core/projector/1/activate_elements/.
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
access_permissions = ProjectorAccessPermissions()
|
|
|
|
|
2016-10-01 01:30:55 +02:00
|
|
|
objects = ProjectorManager()
|
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
config = JSONField()
|
2014-01-28 08:32:26 +01:00
|
|
|
|
2015-09-14 23:16:31 +02:00
|
|
|
scale = models.IntegerField(default=0)
|
|
|
|
|
|
|
|
scroll = models.IntegerField(default=0)
|
|
|
|
|
2016-08-25 16:40:34 +02:00
|
|
|
width = models.PositiveIntegerField(default=1024)
|
|
|
|
|
|
|
|
height = models.PositiveIntegerField(default=768)
|
|
|
|
|
2016-09-12 11:05:34 +02:00
|
|
|
name = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
unique=True,
|
|
|
|
blank=True)
|
|
|
|
|
|
|
|
blank = models.BooleanField(
|
|
|
|
blank=False,
|
|
|
|
default=False)
|
|
|
|
|
2014-01-28 08:32:26 +01:00
|
|
|
class Meta:
|
|
|
|
"""
|
2015-02-18 01:45:39 +01:00
|
|
|
Contains general permissions that can not be placed in a specific app.
|
2014-01-28 08:32:26 +01:00
|
|
|
"""
|
2015-12-10 00:20:59 +01:00
|
|
|
default_permissions = ()
|
2014-01-28 08:32:26 +01:00
|
|
|
permissions = (
|
2016-01-27 13:41:19 +01:00
|
|
|
('can_see_projector', 'Can see the projector'),
|
|
|
|
('can_manage_projector', 'Can manage the projector'),
|
|
|
|
('can_see_frontpage', 'Can see the front page'),)
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
@property
|
2015-06-17 09:45:00 +02:00
|
|
|
def elements(self):
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2016-02-27 20:25:06 +01:00
|
|
|
Retrieve all projector elements given in the config field. For
|
|
|
|
every element the method check_and_update_data() is called and its
|
2015-09-06 13:28:25 +02:00
|
|
|
result is also used.
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2015-09-06 13:28:25 +02:00
|
|
|
# Get all elements from all apps.
|
2017-08-30 00:07:54 +02:00
|
|
|
elements = get_all_projector_elements()
|
2015-09-06 13:28:25 +02:00
|
|
|
|
|
|
|
# Parse result
|
|
|
|
result = {}
|
|
|
|
for key, value in self.config.items():
|
2015-09-08 14:14:11 +02:00
|
|
|
# Use a copy here not to change the origin value in the config field.
|
|
|
|
result[key] = value.copy()
|
2015-09-05 23:32:10 +02:00
|
|
|
result[key]['uuid'] = key
|
2015-09-06 13:28:25 +02:00
|
|
|
element = elements.get(value['name'])
|
2015-02-18 01:45:39 +01:00
|
|
|
if element is None:
|
2016-01-09 13:32:56 +01:00
|
|
|
result[key]['error'] = 'Projector element does not exist.'
|
2015-02-18 01:45:39 +01:00
|
|
|
else:
|
|
|
|
try:
|
2016-02-27 20:25:06 +01:00
|
|
|
result[key].update(element.check_and_update_data(
|
2015-02-18 01:45:39 +01:00
|
|
|
projector_object=self,
|
2015-09-06 13:28:25 +02:00
|
|
|
config_entry=value))
|
2015-02-18 01:45:39 +01:00
|
|
|
except ProjectorException as e:
|
2015-09-06 13:28:25 +02:00
|
|
|
result[key]['error'] = str(e)
|
|
|
|
return result
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2017-07-28 15:42:58 +02:00
|
|
|
@classmethod
|
|
|
|
def remove_any(cls, skip_autoupdate=False, **kwargs):
|
|
|
|
"""
|
|
|
|
Removes all projector elements from all projectors with matching kwargs.
|
|
|
|
Additional properties of active projector elements are ignored:
|
|
|
|
|
|
|
|
Example: Sending {'name': 'assignments/assignment', 'id': 1} will remove
|
|
|
|
also projector elements with {'name': 'assignments/assignment', 'id': 1, 'poll': 2}.
|
|
|
|
"""
|
|
|
|
# Loop over all projectors.
|
|
|
|
for projector in cls.objects.all():
|
|
|
|
change_projector_config = False
|
|
|
|
projector_config = {}
|
|
|
|
# Loop over all projector elements of this projector.
|
|
|
|
for key, value in projector.config.items():
|
|
|
|
# Check if the kwargs match this element.
|
|
|
|
for kwarg_key, kwarg_value in kwargs.items():
|
|
|
|
if not value.get(kwarg_key) == kwarg_value:
|
|
|
|
# No match so the element should stay. Write it into
|
|
|
|
# new config field and break the loop.
|
|
|
|
projector_config[key] = value
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# All kwargs match this projector element. So mark this
|
|
|
|
# projector to be changed. Do not write it into new config field.
|
|
|
|
change_projector_config = True
|
|
|
|
if change_projector_config:
|
|
|
|
projector.config = projector_config
|
|
|
|
projector.save(skip_autoupdate=skip_autoupdate)
|
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2016-09-12 11:05:34 +02:00
|
|
|
class ProjectionDefault(RESTModelMixin, models.Model):
|
|
|
|
"""
|
2016-09-29 15:32:58 +02:00
|
|
|
Model for the projection defaults like motions, agenda, list of
|
|
|
|
speakers and thelike. The name is the technical name like 'topics' or
|
|
|
|
'motions'. For apps the name should be the app name to get keep the
|
|
|
|
ProjectionDefault for apps generic. But it is possible to give some
|
|
|
|
special name like 'list_of_speakers'. The display_name is the shown
|
|
|
|
name on the front end for the user.
|
2016-09-12 11:05:34 +02:00
|
|
|
"""
|
|
|
|
name = models.CharField(max_length=256)
|
|
|
|
|
|
|
|
display_name = models.CharField(max_length=256)
|
|
|
|
|
|
|
|
projector = models.ForeignKey(
|
2016-09-29 15:32:58 +02:00
|
|
|
Projector,
|
2016-09-12 11:05:34 +02:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='projectiondefaults')
|
|
|
|
|
|
|
|
def get_root_rest_element(self):
|
|
|
|
return self.projector
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_permissions = ()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.display_name
|
|
|
|
|
|
|
|
|
2015-06-16 10:37:23 +02:00
|
|
|
class Tag(RESTModelMixin, models.Model):
|
2014-12-26 13:45:13 +01:00
|
|
|
"""
|
2015-02-18 01:45:39 +01:00
|
|
|
Model for tags. This tags can be used for other models like agenda items,
|
|
|
|
motions or assignments.
|
2014-12-26 13:45:13 +01:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
access_permissions = TagAccessPermissions()
|
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
name = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
unique=True)
|
2014-12-26 13:45:13 +01:00
|
|
|
|
|
|
|
class Meta:
|
2015-02-18 01:45:39 +01:00
|
|
|
ordering = ('name',)
|
2015-12-10 00:20:59 +01:00
|
|
|
default_permissions = ()
|
2014-12-26 13:45:13 +01:00
|
|
|
permissions = (
|
2016-01-27 13:41:19 +01:00
|
|
|
('can_manage_tags', 'Can manage tags'),)
|
2014-12-26 13:45:13 +01:00
|
|
|
|
2015-01-05 17:14:29 +01:00
|
|
|
def __str__(self):
|
2014-12-26 13:45:13 +01:00
|
|
|
return self.name
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
class ConfigStore(RESTModelMixin, models.Model):
|
2015-06-29 12:08:15 +02:00
|
|
|
"""
|
|
|
|
A model class to store all config variables in the database.
|
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
access_permissions = ConfigAccessPermissions()
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
key = models.CharField(max_length=255, unique=True, db_index=True)
|
|
|
|
"""A string, the key of the config variable."""
|
|
|
|
|
|
|
|
value = JSONField()
|
|
|
|
"""The value of the config variable. """
|
|
|
|
|
|
|
|
class Meta:
|
2015-12-10 00:20:59 +01:00
|
|
|
default_permissions = ()
|
|
|
|
permissions = (
|
2017-03-31 13:48:41 +02:00
|
|
|
('can_manage_config', 'Can manage configuration'),
|
2018-01-30 16:12:02 +01:00
|
|
|
('can_manage_logos_and_fonts', 'Can manage logos and fonts'))
|
2015-09-07 16:46:04 +02:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
@classmethod
|
|
|
|
def get_collection_string(cls):
|
|
|
|
return 'core/config'
|
|
|
|
|
2015-09-07 16:46:04 +02:00
|
|
|
|
|
|
|
class ChatMessage(RESTModelMixin, models.Model):
|
|
|
|
"""
|
|
|
|
Model for chat messages.
|
|
|
|
|
|
|
|
At the moment we only have one global chat room for managers.
|
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
access_permissions = ChatMessageAccessPermissions()
|
2018-11-01 17:30:18 +01:00
|
|
|
can_see_permission = 'core.can_use_chat'
|
2016-02-11 22:58:32 +01:00
|
|
|
|
2016-01-09 13:32:56 +01:00
|
|
|
message = models.TextField()
|
2015-09-07 16:46:04 +02:00
|
|
|
|
|
|
|
timestamp = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
|
|
user = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL,
|
2016-01-09 13:32:56 +01:00
|
|
|
on_delete=models.CASCADE)
|
2015-09-07 16:46:04 +02:00
|
|
|
|
|
|
|
class Meta:
|
2015-12-10 00:20:59 +01:00
|
|
|
default_permissions = ()
|
2015-09-07 16:46:04 +02:00
|
|
|
permissions = (
|
2016-10-17 12:00:18 +02:00
|
|
|
('can_use_chat', 'Can use the chat'),
|
|
|
|
('can_manage_chat', 'Can manage the chat'),)
|
2015-09-07 16:46:04 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'Message {}'.format(self.timestamp)
|
2016-05-29 08:29:14 +02:00
|
|
|
|
|
|
|
|
2016-10-21 11:05:24 +02:00
|
|
|
class ProjectorMessage(RESTModelMixin, models.Model):
|
|
|
|
"""
|
|
|
|
Model for ProjectorMessages.
|
|
|
|
"""
|
|
|
|
access_permissions = ProjectorMessageAccessPermissions()
|
|
|
|
|
|
|
|
message = models.TextField(blank=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_permissions = ()
|
|
|
|
|
2017-07-28 15:42:58 +02:00
|
|
|
def delete(self, skip_autoupdate=False, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Customized method to delete a projector message. Ensures that a respective
|
|
|
|
projector message projector element is disabled.
|
|
|
|
"""
|
|
|
|
Projector.remove_any(
|
|
|
|
skip_autoupdate=skip_autoupdate,
|
|
|
|
name='core/projector-message',
|
|
|
|
id=self.pk)
|
2017-08-24 12:26:55 +02:00
|
|
|
return super().delete(skip_autoupdate=skip_autoupdate, *args, **kwargs) # type: ignore
|
2017-07-28 15:42:58 +02:00
|
|
|
|
2016-10-21 11:05:24 +02:00
|
|
|
|
|
|
|
class Countdown(RESTModelMixin, models.Model):
|
|
|
|
"""
|
|
|
|
Model for countdowns.
|
|
|
|
"""
|
|
|
|
access_permissions = CountdownAccessPermissions()
|
|
|
|
|
|
|
|
description = models.CharField(max_length=256, blank=True)
|
|
|
|
|
|
|
|
running = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
default_time = models.PositiveIntegerField(default=60)
|
|
|
|
|
|
|
|
countdown_time = models.FloatField(default=60)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_permissions = ()
|
|
|
|
|
2017-07-28 15:42:58 +02:00
|
|
|
def delete(self, skip_autoupdate=False, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Customized method to delete a countdown. Ensures that a respective
|
|
|
|
countdown projector element is disabled.
|
|
|
|
"""
|
|
|
|
Projector.remove_any(
|
|
|
|
skip_autoupdate=skip_autoupdate,
|
|
|
|
name='core/countdown',
|
|
|
|
id=self.pk)
|
2017-08-24 12:26:55 +02:00
|
|
|
return super().delete(skip_autoupdate=skip_autoupdate, *args, **kwargs) # type: ignore
|
2017-07-28 15:42:58 +02:00
|
|
|
|
2017-11-03 09:32:43 +01:00
|
|
|
def control(self, action, skip_autoupdate=False):
|
2016-10-21 11:05:24 +02:00
|
|
|
if action not in ('start', 'stop', 'reset'):
|
|
|
|
raise ValueError("Action must be 'start', 'stop' or 'reset', not {}.".format(action))
|
|
|
|
|
|
|
|
if action == 'start':
|
|
|
|
self.running = True
|
|
|
|
self.countdown_time = now().timestamp() + self.default_time
|
|
|
|
elif action == 'stop' and self.running:
|
|
|
|
self.running = False
|
|
|
|
self.countdown_time = self.countdown_time - now().timestamp()
|
|
|
|
else: # reset
|
|
|
|
self.running = False
|
|
|
|
self.countdown_time = self.default_time
|
2017-11-03 09:32:43 +01:00
|
|
|
self.save(skip_autoupdate=skip_autoupdate)
|
2018-11-04 14:02:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HistoryData(models.Model):
|
|
|
|
"""
|
|
|
|
Django model to save the history of OpenSlides.
|
|
|
|
|
|
|
|
This is not a RESTModel. It is not cachable and can only be reached by a
|
|
|
|
special viewset.
|
|
|
|
"""
|
|
|
|
full_data = JSONField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_permissions = ()
|
|
|
|
|
|
|
|
|
|
|
|
class HistoryManager(models.Manager):
|
|
|
|
"""
|
|
|
|
Customized model manager for the history model.
|
|
|
|
"""
|
|
|
|
def add_elements(self, elements):
|
|
|
|
"""
|
|
|
|
Method to add elements to the history. This does not trigger autoupdate.
|
|
|
|
"""
|
|
|
|
with transaction.atomic():
|
|
|
|
instances = []
|
|
|
|
for element in elements:
|
|
|
|
if element['disable_history'] or element['collection_string'] == self.model.get_collection_string():
|
|
|
|
# Do not update history for history elements itself or if history is disabled.
|
|
|
|
continue
|
|
|
|
# HistoryData is not a root rest element so there is no autoupdate and not history saving here.
|
|
|
|
data = HistoryData.objects.create(full_data=element['full_data'])
|
|
|
|
instance = self.model(
|
|
|
|
element_id=get_element_id(element['collection_string'], element['id']),
|
|
|
|
information=element['information'],
|
|
|
|
user_id=element['user_id'],
|
|
|
|
full_data=data,
|
|
|
|
)
|
|
|
|
instance.save(skip_autoupdate=True) # Skip autoupdate and of course history saving.
|
|
|
|
instances.append(instance)
|
|
|
|
return instances
|
|
|
|
|
|
|
|
def build_history(self):
|
|
|
|
"""
|
|
|
|
Method to add all cachables to the history.
|
|
|
|
"""
|
|
|
|
# TODO: Add lock to prevent multiple history builds at once. See #4039.
|
|
|
|
instances = None
|
|
|
|
if self.all().count() == 0:
|
|
|
|
elements = []
|
|
|
|
all_full_data = async_to_sync(element_cache.get_all_full_data)()
|
|
|
|
for collection_string, data in all_full_data.items():
|
|
|
|
for full_data in data:
|
|
|
|
elements.append(Element(
|
|
|
|
id=full_data['id'],
|
|
|
|
collection_string=collection_string,
|
|
|
|
full_data=full_data,
|
|
|
|
information='',
|
|
|
|
user_id=None,
|
|
|
|
disable_history=False,
|
|
|
|
))
|
|
|
|
instances = self.add_elements(elements)
|
|
|
|
return instances
|
|
|
|
|
|
|
|
|
|
|
|
class History(RESTModelMixin, models.Model):
|
|
|
|
"""
|
|
|
|
Django model to save the history of OpenSlides.
|
|
|
|
|
|
|
|
This model itself is not part of the history. This means that if you
|
|
|
|
delete a user you may lose the information of the user field here.
|
|
|
|
"""
|
|
|
|
access_permissions = HistoryAccessPermissions()
|
|
|
|
|
|
|
|
objects = HistoryManager()
|
|
|
|
|
|
|
|
element_id = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
)
|
|
|
|
|
|
|
|
now = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
|
|
information = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
)
|
|
|
|
|
|
|
|
user = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
null=True,
|
|
|
|
on_delete=models.SET_NULL)
|
|
|
|
|
|
|
|
full_data = models.OneToOneField(
|
|
|
|
HistoryData,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_permissions = ()
|