2018-11-04 14:02:30 +01:00
|
|
|
import datetime
|
2018-10-13 08:41:51 +02:00
|
|
|
import os
|
2015-09-05 17:52:40 +02:00
|
|
|
import uuid
|
2018-10-13 08:41:51 +02:00
|
|
|
from typing import Any, Dict, List
|
2015-02-14 10:10:08 +01:00
|
|
|
|
2015-06-18 21:48:20 +02:00
|
|
|
from django.conf import settings
|
2018-10-13 08:41:51 +02:00
|
|
|
from django.contrib.staticfiles import finders
|
|
|
|
from django.contrib.staticfiles.views import serve
|
2015-09-14 23:16:31 +02:00
|
|
|
from django.db.models import F
|
2015-06-29 12:08:15 +02:00
|
|
|
from django.http import Http404, HttpResponse
|
2015-09-24 21:28:30 +02:00
|
|
|
from django.utils.timezone import now
|
2016-10-17 16:56:19 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-10-13 08:41:51 +02:00
|
|
|
from django.views import static
|
|
|
|
from django.views.generic.base import View
|
2017-08-24 12:26:55 +02:00
|
|
|
from mypy_extensions import TypedDict
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
from .. import __license__ as license, __url__ as url, __version__ as version
|
2016-10-17 16:56:19 +02:00
|
|
|
from ..utils import views as utils_views
|
2018-10-13 08:41:51 +02:00
|
|
|
from ..utils.arguments import arguments
|
2019-01-06 16:22:33 +01:00
|
|
|
from ..utils.auth import GROUP_ADMIN_PK, anonymous_is_enabled, has_perm, in_some_groups
|
2016-11-08 21:29:26 +01:00
|
|
|
from ..utils.autoupdate import inform_changed_data, inform_deleted_data
|
2016-10-17 16:56:19 +02:00
|
|
|
from ..utils.plugins import (
|
2015-06-18 21:48:20 +02:00
|
|
|
get_plugin_description,
|
2017-12-01 10:10:35 +01:00
|
|
|
get_plugin_license,
|
|
|
|
get_plugin_url,
|
2015-06-18 21:48:20 +02:00
|
|
|
get_plugin_verbose_name,
|
|
|
|
get_plugin_version,
|
|
|
|
)
|
2016-10-17 16:56:19 +02:00
|
|
|
from ..utils.rest_api import (
|
2018-11-04 14:02:30 +01:00
|
|
|
GenericViewSet,
|
|
|
|
ListModelMixin,
|
2015-02-18 01:45:39 +01:00
|
|
|
ModelViewSet,
|
|
|
|
Response,
|
2018-11-04 14:02:30 +01:00
|
|
|
RetrieveModelMixin,
|
2015-02-18 01:45:39 +01:00
|
|
|
ValidationError,
|
2015-06-16 10:37:23 +02:00
|
|
|
detail_route,
|
2016-10-17 16:56:19 +02:00
|
|
|
list_route,
|
2015-02-18 01:45:39 +01:00
|
|
|
)
|
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-06-29 12:08:15 +02:00
|
|
|
from .config import config
|
|
|
|
from .exceptions import ConfigError, ConfigNotFound
|
2016-10-21 11:05:24 +02:00
|
|
|
from .models import (
|
|
|
|
ChatMessage,
|
|
|
|
ConfigStore,
|
|
|
|
Countdown,
|
2018-11-04 14:02:30 +01:00
|
|
|
History,
|
|
|
|
HistoryData,
|
2016-10-21 11:05:24 +02:00
|
|
|
ProjectionDefault,
|
|
|
|
Projector,
|
|
|
|
ProjectorMessage,
|
|
|
|
Tag,
|
|
|
|
)
|
2013-12-09 23:56:01 +01:00
|
|
|
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
# Special Django views
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-10-13 08:41:51 +02:00
|
|
|
class IndexView(View):
|
2015-01-30 11:58:36 +01:00
|
|
|
"""
|
2018-10-13 08:41:51 +02:00
|
|
|
The primary view for the OpenSlides client. Serves static files. If a file
|
|
|
|
does not exist or a directory is requested, the index.html is delivered instead.
|
2016-08-25 16:40:34 +02:00
|
|
|
"""
|
|
|
|
|
2018-10-13 08:41:51 +02:00
|
|
|
cache: Dict[str, str] = {}
|
2015-06-17 09:45:00 +02:00
|
|
|
"""
|
2018-10-13 08:41:51 +02:00
|
|
|
Saves the path to the index.html.
|
2015-06-17 09:45:00 +02:00
|
|
|
|
2018-10-13 08:41:51 +02:00
|
|
|
May be extended later to cache every template.
|
2015-07-01 23:18:48 +02:00
|
|
|
"""
|
2018-01-16 16:02:23 +01:00
|
|
|
|
2017-09-26 14:19:48 +02:00
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
|
|
super().__init__(*args, **kwargs)
|
2018-01-16 16:02:23 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
no_caching = arguments.get("no_template_caching", False)
|
|
|
|
if "index" not in self.cache or no_caching:
|
|
|
|
self.cache["index"] = finders.find("index.html")
|
2018-10-13 08:41:51 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
self.index_document_root, self.index_path = os.path.split(self.cache["index"])
|
2018-10-13 08:41:51 +02:00
|
|
|
|
|
|
|
def get(self, request, path, **kwargs) -> HttpResponse:
|
|
|
|
"""
|
|
|
|
Tries to serve the requested file. If it is not found or a directory is
|
|
|
|
requested, the index.html is delivered.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
response = serve(request, path, **kwargs)
|
|
|
|
except Http404:
|
2019-01-06 16:22:33 +01:00
|
|
|
response = static.serve(
|
|
|
|
request,
|
|
|
|
self.index_path,
|
|
|
|
document_root=self.index_document_root,
|
|
|
|
**kwargs,
|
|
|
|
)
|
2018-10-13 08:41:51 +02:00
|
|
|
return response
|
2015-07-01 23:18:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Viewsets for the REST API
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-09-12 11:05:34 +02:00
|
|
|
class ProjectorViewSet(ModelViewSet):
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
API endpoint for the projector slide info.
|
|
|
|
|
2017-02-27 15:37:01 +01:00
|
|
|
There are the following views: See strings in check_view_permissions().
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
access_permissions = ProjectorAccessPermissions()
|
2015-02-18 01:45:39 +01:00
|
|
|
queryset = Projector.objects.all()
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
def check_view_permissions(self):
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
Returns True if the user has required permissions.
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
if self.action in ("list", "retrieve"):
|
2016-09-17 22:26:23 +02:00
|
|
|
result = self.get_access_permissions().check_permissions(self.request.user)
|
2019-01-06 16:22:33 +01:00
|
|
|
elif self.action == "metadata":
|
|
|
|
result = has_perm(self.request.user, "core.can_see_projector")
|
2016-09-29 15:32:58 +02:00
|
|
|
elif self.action in (
|
2019-01-06 16:22:33 +01:00
|
|
|
"create",
|
|
|
|
"update",
|
|
|
|
"partial_update",
|
|
|
|
"destroy",
|
|
|
|
"activate_elements",
|
|
|
|
"prune_elements",
|
|
|
|
"update_elements",
|
|
|
|
"deactivate_elements",
|
|
|
|
"clear_elements",
|
|
|
|
"project",
|
|
|
|
"control_view",
|
|
|
|
"set_resolution",
|
|
|
|
"set_scroll",
|
|
|
|
"control_blank",
|
|
|
|
"broadcast",
|
|
|
|
"set_projectiondefault",
|
2016-09-29 15:32:58 +02:00
|
|
|
):
|
2019-01-06 16:22:33 +01:00
|
|
|
result = has_perm(self.request.user, "core.can_see_projector") and has_perm(
|
|
|
|
self.request.user, "core.can_manage_projector"
|
|
|
|
)
|
2015-07-01 23:18:48 +02:00
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return result
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2016-09-12 11:05:34 +02:00
|
|
|
def destroy(self, *args, **kwargs):
|
2016-11-19 13:04:24 +01:00
|
|
|
"""
|
|
|
|
REST API operation for DELETE requests.
|
|
|
|
|
|
|
|
Assigns all ProjectionDefault objects from this projector to the
|
|
|
|
default projector (pk=1). Resets broadcast if set to this projector.
|
|
|
|
"""
|
2016-09-12 11:05:34 +02:00
|
|
|
projector_instance = self.get_object()
|
2016-09-29 15:32:58 +02:00
|
|
|
for projection_default in ProjectionDefault.objects.all():
|
|
|
|
if projection_default.projector.id == projector_instance.id:
|
|
|
|
projection_default.projector_id = 1
|
|
|
|
projection_default.save()
|
2019-01-06 16:22:33 +01:00
|
|
|
if config["projector_broadcast"] == projector_instance.pk:
|
|
|
|
config["projector_broadcast"] = 0
|
2016-09-12 11:05:34 +02:00
|
|
|
return super(ProjectorViewSet, self).destroy(*args, **kwargs)
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["post"])
|
2015-02-18 01:45:39 +01:00
|
|
|
def activate_elements(self, request, pk):
|
|
|
|
"""
|
|
|
|
REST API operation to activate projector elements. It expects a POST
|
|
|
|
request to /rest/core/projector/<pk>/activate_elements/ with a list
|
2015-09-06 13:28:25 +02:00
|
|
|
of dictionaries to be appended to the projector config entry.
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2015-09-06 13:28:25 +02:00
|
|
|
if not isinstance(request.data, list):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Data must be a list."})
|
2015-09-06 13:28:25 +02:00
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
projector_instance = self.get_object()
|
|
|
|
projector_config = projector_instance.config
|
2015-09-06 13:28:25 +02:00
|
|
|
for element in request.data:
|
2019-01-06 16:22:33 +01:00
|
|
|
if element.get("name") is None:
|
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "Invalid projector element. Name is missing."}
|
|
|
|
)
|
2015-09-06 13:28:25 +02:00
|
|
|
projector_config[uuid.uuid4().hex] = element
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
serializer = self.get_serializer(
|
|
|
|
projector_instance, data={"config": projector_config}, partial=False
|
|
|
|
)
|
2015-02-18 01:45:39 +01:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save()
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["post"])
|
2015-02-18 01:45:39 +01:00
|
|
|
def prune_elements(self, request, pk):
|
|
|
|
"""
|
|
|
|
REST API operation to activate projector elements. It expects a POST
|
|
|
|
request to /rest/core/projector/<pk>/prune_elements/ with a list of
|
|
|
|
dictionaries to write them to the projector config entry. All old
|
|
|
|
entries are deleted but not entries with stable == True.
|
|
|
|
"""
|
2015-09-06 13:28:25 +02:00
|
|
|
if not isinstance(request.data, list):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Data must be a list."})
|
2015-09-06 13:28:25 +02:00
|
|
|
|
2018-04-17 10:05:27 +02:00
|
|
|
projector = self.get_object()
|
|
|
|
elements = request.data
|
|
|
|
if not isinstance(elements, list):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": _("The data has to be a list.")})
|
2018-04-17 10:05:27 +02:00
|
|
|
for element in elements:
|
|
|
|
if not isinstance(element, dict):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": _("All elements have to be dicts.")})
|
|
|
|
if element.get("name") is None:
|
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "Invalid projector element. Name is missing."}
|
|
|
|
)
|
2018-04-17 10:05:27 +02:00
|
|
|
return Response(self.prune(projector, elements))
|
|
|
|
|
|
|
|
def prune(self, projector, elements):
|
|
|
|
"""
|
|
|
|
Prunes all non stable elements from the projector and adds the given elements.
|
|
|
|
The elements have to a list of dicts, each gict containing at least a name. This
|
|
|
|
is not validated at this point! Should be done before.
|
|
|
|
Returns the new serialized data.
|
|
|
|
"""
|
2015-09-06 13:28:25 +02:00
|
|
|
projector_config = {}
|
2018-04-17 10:05:27 +02:00
|
|
|
for key, value in projector.config.items():
|
2019-01-06 16:22:33 +01:00
|
|
|
if value.get("stable"):
|
2015-09-06 13:28:25 +02:00
|
|
|
projector_config[key] = value
|
2018-04-17 10:05:27 +02:00
|
|
|
for element in elements:
|
2015-09-06 13:28:25 +02:00
|
|
|
projector_config[uuid.uuid4().hex] = element
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
serializer = self.get_serializer(
|
|
|
|
projector, data={"config": projector_config}, partial=False
|
|
|
|
)
|
2015-02-18 01:45:39 +01:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save()
|
2018-05-09 08:48:37 +02:00
|
|
|
# reset scroll level
|
2019-01-06 16:22:33 +01:00
|
|
|
if projector.scroll != 0:
|
2018-05-09 08:48:37 +02:00
|
|
|
projector.scroll = 0
|
|
|
|
projector.save()
|
2018-04-17 10:05:27 +02:00
|
|
|
return serializer.data
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["post"])
|
2015-09-05 17:52:40 +02:00
|
|
|
def update_elements(self, request, pk):
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2015-09-05 17:52:40 +02:00
|
|
|
REST API operation to update projector elements. It expects a POST
|
2015-09-14 23:16:31 +02:00
|
|
|
request to /rest/core/projector/<pk>/update_elements/ with a
|
|
|
|
dictonary to update the projector config. This must be a dictionary
|
|
|
|
with UUIDs as keys and projector element dictionaries as values.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
{
|
|
|
|
"191c0878cdc04abfbd64f3177a21891a": {
|
|
|
|
"name": "core/countdown",
|
|
|
|
"stable": true,
|
|
|
|
"status": "running",
|
|
|
|
"countdown_time": 1374321600.0,
|
|
|
|
"visable": true,
|
|
|
|
"default": 42
|
|
|
|
}
|
|
|
|
}
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2015-09-06 13:28:25 +02:00
|
|
|
if not isinstance(request.data, dict):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Data must be a dictionary."})
|
|
|
|
error = {
|
|
|
|
"detail": "Data must be a dictionary with UUIDs as keys and dictionaries as values."
|
|
|
|
}
|
2015-09-06 13:28:25 +02:00
|
|
|
for key, value in request.data.items():
|
2015-09-05 17:52:40 +02:00
|
|
|
try:
|
2015-09-06 13:28:25 +02:00
|
|
|
uuid.UUID(hex=str(key))
|
2015-09-05 17:52:40 +02:00
|
|
|
except ValueError:
|
|
|
|
raise ValidationError(error)
|
2015-09-06 13:28:25 +02:00
|
|
|
if not isinstance(value, dict):
|
2015-09-05 17:52:40 +02:00
|
|
|
raise ValidationError(error)
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
projector_instance = self.get_object()
|
|
|
|
projector_config = projector_instance.config
|
2015-09-06 13:28:25 +02:00
|
|
|
for key, value in request.data.items():
|
|
|
|
if key not in projector_config:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "Invalid projector element. Wrong UUID."}
|
|
|
|
)
|
2015-09-05 23:32:10 +02:00
|
|
|
projector_config[key].update(request.data[key])
|
2015-09-06 13:28:25 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
serializer = self.get_serializer(
|
|
|
|
projector_instance, data={"config": projector_config}, partial=False
|
|
|
|
)
|
2015-09-05 17:52:40 +02:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save()
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["post"])
|
2015-09-05 17:52:40 +02:00
|
|
|
def deactivate_elements(self, request, pk):
|
|
|
|
"""
|
|
|
|
REST API operation to deactivate projector elements. It expects a
|
|
|
|
POST request to /rest/core/projector/<pk>/deactivate_elements/ with
|
|
|
|
a list of hex UUIDs. These are the projector_elements in the config
|
|
|
|
that should be deleted.
|
|
|
|
"""
|
|
|
|
if not isinstance(request.data, list):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Data must be a list of hex UUIDs."})
|
2015-09-05 17:52:40 +02:00
|
|
|
for item in request.data:
|
2015-02-18 01:45:39 +01:00
|
|
|
try:
|
2015-09-05 17:52:40 +02:00
|
|
|
uuid.UUID(hex=str(item))
|
2015-02-18 01:45:39 +01:00
|
|
|
except ValueError:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Data must be a list of hex UUIDs."})
|
2015-09-05 17:52:40 +02:00
|
|
|
|
|
|
|
projector_instance = self.get_object()
|
2015-09-06 13:28:25 +02:00
|
|
|
projector_config = projector_instance.config
|
|
|
|
for key in request.data:
|
|
|
|
try:
|
|
|
|
del projector_config[key]
|
|
|
|
except KeyError:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Invalid UUID."})
|
2015-09-06 13:28:25 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
serializer = self.get_serializer(
|
|
|
|
projector_instance, data={"config": projector_config}, partial=False
|
|
|
|
)
|
2015-02-18 01:45:39 +01:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save()
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["post"])
|
2015-02-18 01:45:39 +01:00
|
|
|
def clear_elements(self, request, pk):
|
|
|
|
"""
|
|
|
|
REST API operation to deactivate all projector elements but not
|
|
|
|
entries with stable == True. It expects a POST request to
|
|
|
|
/rest/core/projector/<pk>/clear_elements/.
|
|
|
|
"""
|
2018-04-17 10:05:27 +02:00
|
|
|
projector = self.get_object()
|
|
|
|
return Response(self.clear(projector))
|
|
|
|
|
|
|
|
def clear(self, projector):
|
2015-09-06 13:28:25 +02:00
|
|
|
projector_config = {}
|
2018-04-17 10:05:27 +02:00
|
|
|
for key, value in projector.config.items():
|
2019-01-06 16:22:33 +01:00
|
|
|
if value.get("stable"):
|
2015-09-06 13:28:25 +02:00
|
|
|
projector_config[key] = value
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
serializer = self.get_serializer(
|
|
|
|
projector, data={"config": projector_config}, partial=False
|
|
|
|
)
|
2015-02-18 01:45:39 +01:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save()
|
2018-04-17 10:05:27 +02:00
|
|
|
return serializer.data
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@list_route(methods=["post"])
|
2018-04-17 10:05:27 +02:00
|
|
|
def project(self, request, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
REST API operation. Does a combination of clear_elements and prune_elements:
|
|
|
|
In the most cases when projecting an element it first need to be removed from
|
|
|
|
all projectors where it is projected. In a second step the new element (which
|
|
|
|
may be not given if the element is just deprojected) needs to be projected on
|
|
|
|
a maybe different projector. The request data has to have this scheme:
|
|
|
|
{
|
|
|
|
clear_ids: [<projector id1>, ...], # May be an empty list
|
|
|
|
prune: { # May not be given.
|
|
|
|
id: <projector id>,
|
|
|
|
element: <projector element to add>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2018-04-25 10:53:42 +02:00
|
|
|
# The data has to be a dict.
|
|
|
|
if not isinstance(request.data, dict):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": _("The data has to be a dict.")})
|
2018-04-25 10:53:42 +02:00
|
|
|
|
2018-04-17 10:05:27 +02:00
|
|
|
# Get projector ids to clear
|
2019-01-06 16:22:33 +01:00
|
|
|
clear_projector_ids = request.data.get("clear_ids", [])
|
2018-04-17 10:05:27 +02:00
|
|
|
for id in clear_projector_ids:
|
|
|
|
if not isinstance(id, int):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{"detail": _('The id "{}" has to be int.').format(id)}
|
|
|
|
)
|
2018-04-17 10:05:27 +02:00
|
|
|
|
|
|
|
# Get the projector id and validate element to prune. This is optional.
|
2019-01-06 16:22:33 +01:00
|
|
|
prune = request.data.get("prune")
|
2018-04-17 10:05:27 +02:00
|
|
|
if prune is not None:
|
|
|
|
if not isinstance(prune, dict):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": _("Prune has to be an object.")})
|
|
|
|
prune_projector_id = prune.get("id")
|
2018-04-17 10:05:27 +02:00
|
|
|
if not isinstance(prune_projector_id, int):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{"detail": _("The prune projector id has to be int.")}
|
|
|
|
)
|
2018-04-17 10:05:27 +02:00
|
|
|
|
|
|
|
# Get the projector after all clear operations, but check, if it exist.
|
|
|
|
if not Projector.objects.filter(pk=prune_projector_id).exists():
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{
|
|
|
|
"detail": _('The projector with id "{}" does not exist').format(
|
|
|
|
prune_projector_id
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
prune_element = prune.get("element", {})
|
2018-04-17 10:05:27 +02:00
|
|
|
if not isinstance(prune_element, dict):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{"detail": _("Prune element has to be a dict or not given.")}
|
|
|
|
)
|
|
|
|
if prune_element.get("name") is None:
|
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "Invalid projector element. Name is missing."}
|
|
|
|
)
|
2018-04-17 10:05:27 +02:00
|
|
|
|
|
|
|
# First step: Clear all given projectors
|
|
|
|
for projector in Projector.objects.filter(pk__in=clear_projector_ids):
|
|
|
|
self.clear(projector)
|
|
|
|
|
|
|
|
# Second step: optionally prune
|
|
|
|
if prune is not None:
|
|
|
|
# This get is save. We checked that the projector exists above.
|
|
|
|
prune_projector = Projector.objects.get(pk=prune_projector_id)
|
|
|
|
self.prune(prune_projector, [prune_element])
|
|
|
|
|
|
|
|
return Response()
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["post"])
|
2016-08-25 16:40:34 +02:00
|
|
|
def set_resolution(self, request, pk):
|
|
|
|
"""
|
|
|
|
REST API operation to set the resolution.
|
|
|
|
|
|
|
|
It is actually unused, because the resolution is currently set in the config.
|
|
|
|
But with the multiprojector feature this will become importent to set the
|
|
|
|
resolution per projector individually.
|
|
|
|
|
|
|
|
It expects a POST request to
|
|
|
|
/rest/core/projector/<pk>/set_resolution/ with a dictionary with the width
|
|
|
|
and height and the values.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
{
|
|
|
|
"width": "1024",
|
|
|
|
"height": "768"
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
if not isinstance(request.data, dict):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Data must be a dictionary."})
|
|
|
|
if request.data.get("width") is None or request.data.get("height") is None:
|
|
|
|
raise ValidationError({"detail": "A width and a height have to be given."})
|
|
|
|
if not isinstance(request.data["width"], int) or not isinstance(
|
|
|
|
request.data["height"], int
|
|
|
|
):
|
|
|
|
raise ValidationError({"detail": "Data has to be integers."})
|
|
|
|
if (
|
|
|
|
request.data["width"] < 800
|
|
|
|
or request.data["width"] > 3840
|
|
|
|
or request.data["height"] < 340
|
|
|
|
or request.data["height"] > 2880
|
|
|
|
):
|
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "The Resolution have to be between 800x340 and 3840x2880."}
|
|
|
|
)
|
2016-08-25 16:40:34 +02:00
|
|
|
|
|
|
|
projector_instance = self.get_object()
|
2019-01-06 16:22:33 +01:00
|
|
|
projector_instance.width = request.data["width"]
|
|
|
|
projector_instance.height = request.data["height"]
|
2016-08-25 16:40:34 +02:00
|
|
|
projector_instance.save()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
message = "Changing resolution to {width}x{height} was successful.".format(
|
|
|
|
width=request.data["width"], height=request.data["height"]
|
|
|
|
)
|
|
|
|
return Response({"detail": message})
|
2016-08-25 16:40:34 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["post"])
|
2015-09-14 23:16:31 +02:00
|
|
|
def control_view(self, request, pk):
|
|
|
|
"""
|
|
|
|
REST API operation to control the projector view, i. e. scale and
|
|
|
|
scroll the projector.
|
|
|
|
|
|
|
|
It expects a POST request to
|
|
|
|
/rest/core/projector/<pk>/control_view/ with a dictionary with an
|
|
|
|
action ('scale' or 'scroll') and a direction ('up', 'down' or
|
|
|
|
'reset').
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
{
|
|
|
|
"action": "scale",
|
|
|
|
"direction": "up"
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
if not isinstance(request.data, dict):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Data must be a dictionary."})
|
|
|
|
if request.data.get("action") not in ("scale", "scroll") or request.data.get(
|
|
|
|
"direction"
|
|
|
|
) not in ("up", "down", "reset"):
|
|
|
|
raise ValidationError(
|
|
|
|
{
|
|
|
|
"detail": "Data must be a dictionary with an action ('scale' or 'scroll') "
|
|
|
|
"and a direction ('up', 'down' or 'reset')."
|
|
|
|
}
|
|
|
|
)
|
2015-09-14 23:16:31 +02:00
|
|
|
|
|
|
|
projector_instance = self.get_object()
|
2019-01-06 16:22:33 +01:00
|
|
|
if request.data["action"] == "scale":
|
|
|
|
if request.data["direction"] == "up":
|
|
|
|
projector_instance.scale = F("scale") + 1
|
|
|
|
elif request.data["direction"] == "down":
|
|
|
|
projector_instance.scale = F("scale") - 1
|
2015-09-14 23:16:31 +02:00
|
|
|
else:
|
|
|
|
# request.data['direction'] == 'reset'
|
|
|
|
projector_instance.scale = 0
|
|
|
|
else:
|
|
|
|
# request.data['action'] == 'scroll'
|
2019-01-06 16:22:33 +01:00
|
|
|
if request.data["direction"] == "up":
|
|
|
|
projector_instance.scroll = F("scroll") + 1
|
|
|
|
elif request.data["direction"] == "down":
|
|
|
|
projector_instance.scroll = F("scroll") - 1
|
2015-09-14 23:16:31 +02:00
|
|
|
else:
|
|
|
|
# request.data['direction'] == 'reset'
|
|
|
|
projector_instance.scroll = 0
|
|
|
|
|
2016-11-08 21:29:26 +01:00
|
|
|
projector_instance.save(skip_autoupdate=True)
|
|
|
|
projector_instance.refresh_from_db()
|
|
|
|
inform_changed_data(projector_instance)
|
2019-01-06 16:22:33 +01:00
|
|
|
message = "{action} {direction} was successful.".format(
|
|
|
|
action=request.data["action"].capitalize(),
|
|
|
|
direction=request.data["direction"],
|
|
|
|
)
|
|
|
|
return Response({"detail": message})
|
2015-09-14 23:16:31 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["post"])
|
2016-09-05 16:23:44 +02:00
|
|
|
def set_scroll(self, request, pk):
|
|
|
|
"""
|
|
|
|
REST API operation to scroll the projector.
|
|
|
|
|
|
|
|
It expects a POST request to
|
|
|
|
/rest/core/projector/<pk>/set_scroll/ with a new value for scroll.
|
|
|
|
"""
|
|
|
|
if not isinstance(request.data, int):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Data must be an int."})
|
2016-09-05 16:23:44 +02:00
|
|
|
|
|
|
|
projector_instance = self.get_object()
|
|
|
|
projector_instance.scroll = request.data
|
|
|
|
|
|
|
|
projector_instance.save()
|
2019-01-06 16:22:33 +01:00
|
|
|
message = "Setting scroll to {scroll} was successful.".format(
|
|
|
|
scroll=request.data
|
|
|
|
)
|
|
|
|
return Response({"detail": message})
|
2016-09-05 16:23:44 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["post"])
|
2016-09-12 11:05:34 +02:00
|
|
|
def control_blank(self, request, pk):
|
|
|
|
"""
|
|
|
|
REST API operation to blank the projector.
|
|
|
|
|
|
|
|
It expects a POST request to
|
|
|
|
/rest/core/projector/<pk>/control_blank/ with a value for blank.
|
|
|
|
"""
|
|
|
|
if not isinstance(request.data, bool):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Data must be a bool."})
|
2016-09-12 11:05:34 +02:00
|
|
|
|
|
|
|
projector_instance = self.get_object()
|
|
|
|
projector_instance.blank = request.data
|
|
|
|
projector_instance.save()
|
|
|
|
message = "Setting 'blank' to {blank} was successful.".format(
|
2019-01-06 16:22:33 +01:00
|
|
|
blank=request.data
|
|
|
|
)
|
|
|
|
return Response({"detail": message})
|
2016-09-12 11:05:34 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["post"])
|
2016-09-12 11:05:34 +02:00
|
|
|
def broadcast(self, request, pk):
|
|
|
|
"""
|
|
|
|
REST API operation to (un-)broadcast the given projector.
|
|
|
|
This method takes care, that all other projectors get the new requirements.
|
|
|
|
|
|
|
|
It expects a POST request to
|
|
|
|
/rest/core/projector/<pk>/broadcast/ without an argument
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
if config["projector_broadcast"] == 0:
|
|
|
|
config["projector_broadcast"] = pk
|
2016-09-12 11:05:34 +02:00
|
|
|
message = "Setting projector {id} as broadcast projector was successful.".format(
|
2019-01-06 16:22:33 +01:00
|
|
|
id=pk
|
|
|
|
)
|
2016-09-12 11:05:34 +02:00
|
|
|
else:
|
2019-01-06 16:22:33 +01:00
|
|
|
config["projector_broadcast"] = 0
|
2016-09-12 11:05:34 +02:00
|
|
|
message = "Disabling broadcast was successful."
|
2019-01-06 16:22:33 +01:00
|
|
|
return Response({"detail": message})
|
2016-09-12 11:05:34 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["post"])
|
2016-09-12 11:05:34 +02:00
|
|
|
def set_projectiondefault(self, request, pk):
|
|
|
|
"""
|
|
|
|
REST API operation to set a projectiondefault to the requested projector. The argument
|
|
|
|
has to be an int representing the pk from the projectiondefault to be set.
|
|
|
|
|
|
|
|
It expects a POST request to
|
|
|
|
/rest/core/projector/<pk>/set_projectiondefault/ with the projectiondefault id as the argument
|
|
|
|
"""
|
|
|
|
if not isinstance(request.data, int):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Data must be an int."})
|
2016-09-12 11:05:34 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
projectiondefault = ProjectionDefault.objects.get(pk=request.data)
|
|
|
|
except ProjectionDefault.DoesNotExist:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{
|
|
|
|
"detail": "The projectiondefault with pk={pk} was not found.".format(
|
|
|
|
pk=request.data
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2016-09-12 11:05:34 +02:00
|
|
|
else:
|
|
|
|
projector_instance = self.get_object()
|
|
|
|
projectiondefault.projector = projector_instance
|
|
|
|
projectiondefault.save()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
return Response(
|
|
|
|
'Setting projectiondefault "{name}" to projector {projector_id} was successful.'.format(
|
|
|
|
name=projectiondefault.display_name, projector_id=projector_instance.pk
|
|
|
|
)
|
|
|
|
)
|
2016-09-12 11:05:34 +02:00
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
class TagViewSet(ModelViewSet):
|
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
API endpoint for tags.
|
|
|
|
|
2015-08-31 14:07:24 +02:00
|
|
|
There are the following views: metadata, list, retrieve, create,
|
|
|
|
partial_update, update and destroy.
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
access_permissions = TagAccessPermissions()
|
2015-02-18 01:45:39 +01:00
|
|
|
queryset = Tag.objects.all()
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
def check_view_permissions(self):
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
Returns True if the user has required permissions.
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
if self.action in ("list", "retrieve"):
|
2016-09-17 22:26:23 +02:00
|
|
|
result = self.get_access_permissions().check_permissions(self.request.user)
|
2019-01-06 16:22:33 +01:00
|
|
|
elif self.action == "metadata":
|
2016-12-17 09:30:20 +01:00
|
|
|
# Every authenticated user can see the metadata.
|
2016-02-11 22:58:32 +01:00
|
|
|
# Anonymous users can do so if they are enabled.
|
2018-07-09 23:22:26 +02:00
|
|
|
result = self.request.user.is_authenticated or anonymous_is_enabled()
|
2019-01-06 16:22:33 +01:00
|
|
|
elif self.action in ("create", "partial_update", "update", "destroy"):
|
|
|
|
result = has_perm(self.request.user, "core.can_manage_tags")
|
2015-07-01 23:18:48 +02:00
|
|
|
else:
|
|
|
|
result = False
|
2015-06-18 21:48:20 +02:00
|
|
|
return result
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
|
2017-08-22 14:17:20 +02:00
|
|
|
class ConfigViewSet(ModelViewSet):
|
2015-06-29 12:08:15 +02:00
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
API endpoint for the config.
|
|
|
|
|
2017-02-27 15:37:01 +01:00
|
|
|
There are the following views: metadata, list, retrieve, update and
|
|
|
|
partial_update.
|
2015-06-29 12:08:15 +02:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
access_permissions = ConfigAccessPermissions()
|
2017-08-22 14:17:20 +02:00
|
|
|
queryset = ConfigStore.objects.all()
|
2015-06-29 12:08:15 +02:00
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
def check_view_permissions(self):
|
|
|
|
"""
|
|
|
|
Returns True if the user has required permissions.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
if self.action in ("list", "retrieve"):
|
2016-09-17 22:26:23 +02:00
|
|
|
result = self.get_access_permissions().check_permissions(self.request.user)
|
2019-01-06 16:22:33 +01:00
|
|
|
elif self.action == "metadata":
|
2015-08-31 14:07:24 +02:00
|
|
|
# Every authenticated user can see the metadata and list or
|
|
|
|
# retrieve the config. Anonymous users can do so if they are
|
|
|
|
# enabled.
|
2018-07-09 23:22:26 +02:00
|
|
|
result = self.request.user.is_authenticated or anonymous_is_enabled()
|
2019-01-06 16:22:33 +01:00
|
|
|
elif self.action in ("partial_update", "update"):
|
2018-01-30 16:12:02 +01:00
|
|
|
# The user needs 'core.can_manage_logos_and_fonts' for all config values
|
|
|
|
# starting with 'logo' and 'font'. For all other config values th euser needs
|
2017-03-31 13:48:41 +02:00
|
|
|
# the default permissions 'core.can_manage_config'.
|
2019-01-06 16:22:33 +01:00
|
|
|
pk = self.kwargs["pk"]
|
|
|
|
if pk.startswith("logo") or pk.startswith("font"):
|
|
|
|
result = has_perm(self.request.user, "core.can_manage_logos_and_fonts")
|
2017-03-31 13:48:41 +02:00
|
|
|
else:
|
2019-01-06 16:22:33 +01:00
|
|
|
result = has_perm(self.request.user, "core.can_manage_config")
|
2015-07-01 23:18:48 +02:00
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return result
|
|
|
|
|
2015-06-29 12:08:15 +02:00
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Updates a config variable. Only managers can do this.
|
|
|
|
|
|
|
|
Example: {"value": 42}
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
key = kwargs["pk"]
|
|
|
|
value = request.data.get("value")
|
2016-01-13 15:04:47 +01:00
|
|
|
if value is None:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Invalid input. Config value is missing."})
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
# Validate and change value.
|
|
|
|
try:
|
|
|
|
config[key] = value
|
|
|
|
except ConfigNotFound:
|
|
|
|
raise Http404
|
|
|
|
except ConfigError as e:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": str(e)})
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
# Return response.
|
2019-01-06 16:22:33 +01:00
|
|
|
return Response({"key": key, "value": value})
|
2015-07-01 17:48:41 +02:00
|
|
|
|
|
|
|
|
2015-09-07 16:46:04 +02:00
|
|
|
class ChatMessageViewSet(ModelViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint for chat messages.
|
|
|
|
|
|
|
|
There are the following views: metadata, list, retrieve and create.
|
|
|
|
The views partial_update, update and destroy are disabled.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
access_permissions = ChatMessageAccessPermissions()
|
2015-09-07 16:46:04 +02:00
|
|
|
queryset = ChatMessage.objects.all()
|
|
|
|
|
|
|
|
def check_view_permissions(self):
|
|
|
|
"""
|
|
|
|
Returns True if the user has required permissions.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
if self.action in ("list", "retrieve"):
|
2016-09-17 22:26:23 +02:00
|
|
|
result = self.get_access_permissions().check_permissions(self.request.user)
|
2019-01-06 16:22:33 +01:00
|
|
|
elif self.action in ("metadata", "create"):
|
2016-02-11 22:58:32 +01:00
|
|
|
# We do not want anonymous users to use the chat even the anonymous
|
|
|
|
# group has the permission core.can_use_chat.
|
2019-01-06 16:22:33 +01:00
|
|
|
result = self.request.user.is_authenticated and has_perm(
|
|
|
|
self.request.user, "core.can_use_chat"
|
|
|
|
)
|
|
|
|
elif self.action == "clear":
|
|
|
|
result = has_perm(self.request.user, "core.can_use_chat") and has_perm(
|
|
|
|
self.request.user, "core.can_manage_chat"
|
|
|
|
)
|
2016-10-17 16:56:19 +02:00
|
|
|
else:
|
|
|
|
result = False
|
2016-02-11 22:58:32 +01:00
|
|
|
return result
|
2015-09-07 16:46:04 +02:00
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
"""
|
|
|
|
Customized method to inject the request.user into serializer's save
|
|
|
|
method so that the request.user can be saved into the model field.
|
|
|
|
"""
|
|
|
|
serializer.save(user=self.request.user)
|
2017-04-28 22:10:18 +02:00
|
|
|
# Send chatter via autoupdate because users without permission
|
|
|
|
# to see users may not have it but can get it now.
|
|
|
|
inform_changed_data([self.request.user])
|
2015-09-07 16:46:04 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@list_route(methods=["post"])
|
2016-10-17 16:56:19 +02:00
|
|
|
def clear(self, request):
|
|
|
|
"""
|
|
|
|
Deletes all chat messages.
|
|
|
|
"""
|
|
|
|
# Collect all chat messages with their collection_string and id
|
|
|
|
chatmessages = ChatMessage.objects.all()
|
|
|
|
args = []
|
|
|
|
for chatmessage in chatmessages:
|
2017-09-04 00:25:45 +02:00
|
|
|
args.append((chatmessage.get_collection_string(), chatmessage.pk))
|
2016-10-17 16:56:19 +02:00
|
|
|
chatmessages.delete()
|
|
|
|
# Trigger autoupdate and setup response.
|
2017-04-13 10:41:13 +02:00
|
|
|
if len(args) > 0:
|
2017-09-04 00:25:45 +02:00
|
|
|
inform_deleted_data(args)
|
2019-01-06 16:22:33 +01:00
|
|
|
return Response({"detail": _("All chat messages deleted successfully.")})
|
2016-10-17 16:56:19 +02:00
|
|
|
|
2015-09-07 16:46:04 +02:00
|
|
|
|
2016-10-21 11:05:24 +02:00
|
|
|
class ProjectorMessageViewSet(ModelViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint for messages.
|
|
|
|
|
2017-02-27 15:37:01 +01:00
|
|
|
There are the following views: list, retrieve, create, update,
|
|
|
|
partial_update and destroy.
|
2016-10-21 11:05:24 +02:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-10-21 11:05:24 +02:00
|
|
|
access_permissions = ProjectorMessageAccessPermissions()
|
|
|
|
queryset = ProjectorMessage.objects.all()
|
|
|
|
|
|
|
|
def check_view_permissions(self):
|
|
|
|
"""
|
|
|
|
Returns True if the user has required permissions.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
if self.action in ("list", "retrieve"):
|
2016-10-21 11:05:24 +02:00
|
|
|
result = self.get_access_permissions().check_permissions(self.request.user)
|
2019-01-06 16:22:33 +01:00
|
|
|
elif self.action in ("create", "partial_update", "update", "destroy"):
|
|
|
|
result = has_perm(self.request.user, "core.can_manage_projector")
|
2016-10-21 11:05:24 +02:00
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class CountdownViewSet(ModelViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint for Countdown.
|
|
|
|
|
2017-02-27 15:37:01 +01:00
|
|
|
There are the following views: list, retrieve, create, update,
|
|
|
|
partial_update and destroy.
|
2016-10-21 11:05:24 +02:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-10-21 11:05:24 +02:00
|
|
|
access_permissions = CountdownAccessPermissions()
|
|
|
|
queryset = Countdown.objects.all()
|
|
|
|
|
|
|
|
def check_view_permissions(self):
|
|
|
|
"""
|
|
|
|
Returns True if the user has required permissions.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
if self.action in ("list", "retrieve"):
|
2016-10-21 11:05:24 +02:00
|
|
|
result = self.get_access_permissions().check_permissions(self.request.user)
|
2019-01-06 16:22:33 +01:00
|
|
|
elif self.action in ("create", "partial_update", "update", "destroy"):
|
|
|
|
result = has_perm(self.request.user, "core.can_manage_projector")
|
2016-10-21 11:05:24 +02:00
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2018-11-04 14:02:30 +01:00
|
|
|
class HistoryViewSet(ListModelMixin, RetrieveModelMixin, GenericViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint for History.
|
|
|
|
|
|
|
|
There are the following views: list, retrieve, clear_history.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-11-04 14:02:30 +01:00
|
|
|
access_permissions = HistoryAccessPermissions()
|
|
|
|
queryset = History.objects.all()
|
|
|
|
|
|
|
|
def check_view_permissions(self):
|
|
|
|
"""
|
|
|
|
Returns True if the user has required permissions.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
if self.action in ("list", "retrieve", "clear_history"):
|
2018-11-04 14:02:30 +01:00
|
|
|
result = self.get_access_permissions().check_permissions(self.request.user)
|
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return result
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@list_route(methods=["post"])
|
2018-11-04 14:02:30 +01:00
|
|
|
def clear_history(self, request):
|
|
|
|
"""
|
|
|
|
Deletes and rebuilds the history.
|
|
|
|
"""
|
|
|
|
# Collect all history objects with their collection_string and id.
|
|
|
|
args = []
|
|
|
|
for history_obj in History.objects.all():
|
|
|
|
args.append((history_obj.get_collection_string(), history_obj.pk))
|
|
|
|
|
|
|
|
# Delete history data and history (via CASCADE)
|
|
|
|
HistoryData.objects.all().delete()
|
|
|
|
|
|
|
|
# Trigger autoupdate.
|
|
|
|
if len(args) > 0:
|
|
|
|
inform_deleted_data(args)
|
|
|
|
|
|
|
|
# Rebuild history.
|
|
|
|
history_instances = History.objects.build_history()
|
|
|
|
inform_changed_data(history_instances)
|
|
|
|
|
|
|
|
# Setup response.
|
2019-01-06 16:22:33 +01:00
|
|
|
return Response({"detail": _("History was deleted successfully.")})
|
2018-11-04 14:02:30 +01:00
|
|
|
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
# Special API views
|
2015-07-01 17:48:41 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-09-24 21:28:30 +02:00
|
|
|
class ServerTime(utils_views.APIView):
|
|
|
|
"""
|
|
|
|
Returns the server time as UNIX timestamp.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
|
|
|
http_method_names = ["get"]
|
2015-09-24 21:28:30 +02:00
|
|
|
|
|
|
|
def get_context_data(self, **context):
|
|
|
|
return now().timestamp()
|
|
|
|
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
class VersionView(utils_views.APIView):
|
|
|
|
"""
|
|
|
|
Returns a dictionary with the OpenSlides version and the version of all
|
|
|
|
plugins.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
|
|
|
http_method_names = ["get"]
|
2015-07-01 23:18:48 +02:00
|
|
|
|
|
|
|
def get_context_data(self, **context):
|
2019-01-06 16:22:33 +01:00
|
|
|
Result = TypedDict(
|
|
|
|
"Result",
|
|
|
|
{
|
|
|
|
"openslides_version": str,
|
|
|
|
"openslides_license": str,
|
|
|
|
"openslides_url": str,
|
|
|
|
"plugins": List[Dict[str, str]],
|
|
|
|
},
|
|
|
|
)
|
2018-08-22 22:00:08 +02:00
|
|
|
result: Result = dict(
|
2017-12-01 10:10:35 +01:00
|
|
|
openslides_version=version,
|
|
|
|
openslides_license=license,
|
|
|
|
openslides_url=url,
|
2019-01-06 16:22:33 +01:00
|
|
|
plugins=[],
|
|
|
|
)
|
2015-07-01 23:18:48 +02:00
|
|
|
# Versions of plugins.
|
|
|
|
for plugin in settings.INSTALLED_PLUGINS:
|
2019-01-06 16:22:33 +01:00
|
|
|
result["plugins"].append(
|
|
|
|
{
|
|
|
|
"verbose_name": get_plugin_verbose_name(plugin),
|
|
|
|
"description": get_plugin_description(plugin),
|
|
|
|
"version": get_plugin_version(plugin),
|
|
|
|
"license": get_plugin_license(plugin),
|
|
|
|
"url": get_plugin_url(plugin),
|
|
|
|
}
|
|
|
|
)
|
2015-07-01 23:18:48 +02:00
|
|
|
return result
|
2018-11-04 14:02:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HistoryView(utils_views.APIView):
|
|
|
|
"""
|
|
|
|
View to retrieve the history data of OpenSlides.
|
|
|
|
|
|
|
|
Use query paramter timestamp (UNIX timestamp) to get all elements from begin
|
|
|
|
until (including) this timestamp.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
|
|
|
http_method_names = ["get"]
|
2018-11-04 14:02:30 +01:00
|
|
|
|
|
|
|
def get_context_data(self, **context):
|
|
|
|
"""
|
|
|
|
Checks if user is in admin group. If yes all history data until
|
|
|
|
(including) timestamp are added to the response data.
|
|
|
|
"""
|
|
|
|
if not in_some_groups(self.request.user.pk or 0, [GROUP_ADMIN_PK]):
|
|
|
|
self.permission_denied(self.request)
|
|
|
|
try:
|
2019-01-06 16:22:33 +01:00
|
|
|
timestamp = int(self.request.query_params.get("timestamp", 0))
|
2018-11-04 14:02:30 +01:00
|
|
|
except (ValueError):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "Invalid input. Timestamp should be an integer."}
|
|
|
|
)
|
2018-11-04 14:02:30 +01:00
|
|
|
data = []
|
2019-01-06 16:22:33 +01:00
|
|
|
queryset = History.objects.select_related("full_data")
|
2018-11-04 14:02:30 +01:00
|
|
|
if timestamp:
|
2019-01-06 16:22:33 +01:00
|
|
|
queryset = queryset.filter(
|
|
|
|
now__lte=datetime.datetime.fromtimestamp(timestamp)
|
|
|
|
)
|
2018-11-04 14:02:30 +01:00
|
|
|
for instance in queryset:
|
2019-01-06 16:22:33 +01:00
|
|
|
data.append(
|
|
|
|
{
|
|
|
|
"full_data": instance.full_data.full_data,
|
|
|
|
"element_id": instance.element_id,
|
|
|
|
"timestamp": instance.now.timestamp(),
|
|
|
|
"information": instance.information,
|
|
|
|
"user_id": instance.user.pk if instance.user else None,
|
|
|
|
}
|
|
|
|
)
|
2018-11-04 14:02:30 +01:00
|
|
|
return data
|