New projector III

* Add a new view `project` to set the projector elements-fields
This commit is contained in:
Oskar Hahn 2019-01-19 10:37:05 +01:00
parent cb3e6c4e47
commit b4c3ffe01f
3 changed files with 113 additions and 49 deletions

View File

@ -101,12 +101,6 @@ class ProjectorSerializer(ModelSerializer):
) )
read_only_fields = ("scale", "scroll") read_only_fields = ("scale", "scroll")
def validate_elements_history(self, value):
"""
Validates the value of the element field of the projector model.
"""
self.validate_elements_preview(value)
class TagSerializer(ModelSerializer): class TagSerializer(ModelSerializer):
""" """

View File

@ -127,9 +127,9 @@ class ProjectorViewSet(ModelViewSet):
"partial_update", "partial_update",
"destroy", "destroy",
"control_view", "control_view",
"set_resolution",
"set_scroll", "set_scroll",
"set_projectiondefault", "set_projectiondefault",
"project",
): ):
result = has_perm(self.request.user, "core.can_see_projector") and has_perm( result = has_perm(self.request.user, "core.can_see_projector") and has_perm(
self.request.user, "core.can_manage_projector" self.request.user, "core.can_manage_projector"
@ -152,6 +152,39 @@ class ProjectorViewSet(ModelViewSet):
projection_default.save() projection_default.save()
return super(ProjectorViewSet, self).destroy(*args, **kwargs) return super(ProjectorViewSet, self).destroy(*args, **kwargs)
@detail_route(methods=["post"])
def project(self, request, pk):
"""
Sets the `elements` and `elements_preview` and adds one item to the
`elements_history`.
`request.data` can have three arguments: `append_to_history`, `elements`
and `preview`. Non of them is required.
`append_to_history` adds one element to the end of the history_elements.
`elements` and `preview` preplaces the coresponding fields in the
database.
"""
projector = self.get_object()
elements = request.data.get("elements")
preview = request.data.get("preview")
history_element = request.data.get("append_to_history")
changed_data = {}
if elements is not None:
changed_data["elements"] = elements
if preview is not None:
changed_data["elements_preview"] = preview
if history_element is not None:
history = projector.elements_history + [history_element]
changed_data["elements_history"] = history
serializer = self.get_serializer(projector, data=changed_data, partial=True)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
return Response()
@detail_route(methods=["post"]) @detail_route(methods=["post"])
def control_view(self, request, pk): def control_view(self, request, pk):
""" """

View File

@ -1,71 +1,108 @@
import json import json
import pytest
from django.urls import reverse from django.urls import reverse
from rest_framework import status from rest_framework import status
from rest_framework.test import APIClient from rest_framework.test import APIClient
from openslides import __license__ as license, __url__ as url, __version__ as version from openslides import __license__ as license, __url__ as url, __version__ as version
from openslides.core.config import ConfigVariable, config from openslides.core.config import ConfigVariable, config
from openslides.core.models import Projector
from openslides.utils.rest_api import ValidationError from openslides.utils.rest_api import ValidationError
from openslides.utils.test import TestCase from openslides.utils.test import TestCase
class ProjectorAPI(TestCase): @pytest.mark.django_db(transaction=False)
def test_slide_on_default_projector(self): def test_slide_on_default_projector(client):
self.client.login(username="admin", password="admin") client.login(username="admin", password="admin")
self.client.put( client.put(
reverse("projector-detail", args=["1"]), reverse("projector-detail", args=["1"]),
{"elements": [{"name": "topics/topic", "id": 1}]}, {"elements": [{"name": "topics/topic", "id": 1}]},
content_type="application/json", content_type="application/json",
) )
response = self.client.get(reverse("projector-detail", args=["1"])) response = client.get(reverse("projector-detail", args=["1"]))
self.assertEqual(response.status_code, status.HTTP_200_OK) assert response.status_code == 200
def test_invalid_element_non_existing_slide(self):
self.client.login(username="admin", password="admin")
response = self.client.put( @pytest.mark.django_db(transaction=False)
def test_invalid_element_non_existing_slide(client):
client.login(username="admin", password="admin")
response = client.put(
reverse("projector-detail", args=["1"]), reverse("projector-detail", args=["1"]),
{"elements": [{"name": "invalid_slide_name", "id": 1}]}, {"elements": [{"name": "invalid_slide_name", "id": 1}]},
content_type="application/json", content_type="application/json",
) )
self.assertEqual(response.status_code, 400) assert response.status_code == 400
def test_invalid_element_no_name_attribute(self):
self.client.login(username="admin", password="admin")
response = self.client.put( @pytest.mark.django_db(transaction=False)
def test_invalid_element_no_name_attribute(client):
client.login(username="admin", password="admin")
response = client.put(
reverse("projector-detail", args=["1"]), reverse("projector-detail", args=["1"]),
{"elements": [{"id": 1}]}, {"elements": [{"id": 1}]},
content_type="application/json", content_type="application/json",
) )
self.assertEqual(response.status_code, 400) assert response.status_code == 400
def test_invalid_element_not_a_inner_dict(self):
self.client.login(username="admin", password="admin")
response = self.client.put( @pytest.mark.django_db(transaction=False)
def test_invalid_element_not_a_inner_dict(client):
client.login(username="admin", password="admin")
response = client.put(
reverse("projector-detail", args=["1"]), reverse("projector-detail", args=["1"]),
{"elements": ["not a dict"]}, {"elements": ["not a dict"]},
content_type="application/json", content_type="application/json",
) )
self.assertEqual(response.status_code, 400) assert response.status_code == 400
def test_invalid_element_a_list(self):
self.client.login(username="admin", password="admin")
response = self.client.put( @pytest.mark.django_db(transaction=False)
def test_invalid_element_a_list(client):
client.login(username="admin", password="admin")
response = client.put(
reverse("projector-detail", args=["1"]), reverse("projector-detail", args=["1"]),
{"elements": {"name": "invalid_slide_name", "id": 1}}, {"elements": {"name": "invalid_slide_name", "id": 1}},
content_type="application/json", content_type="application/json",
) )
self.assertEqual(response.status_code, 400) assert response.status_code == 400
@pytest.mark.django_db(transaction=False)
def test_project_view(client):
client.login(username="admin", password="admin")
projector = Projector.objects.get(pk=1)
projector.elements_history = [[{"name": "topics/topic", "id": 3}]]
projector.save()
response = client.post(
reverse("projector-project", args=["1"]),
{
"append_to_history": [{"name": "topics/topic", "id": 1}],
"elements": [{"name": "topics/topic", "id": 2}],
"preview": [[{"name": "topics/topic", "id": 3}]],
},
content_type="application/json",
)
projector.refresh_from_db()
assert response.status_code == 200
assert projector.elements == [{"name": "topics/topic", "id": 2}]
assert projector.elements_history == [
[{"name": "topics/topic", "id": 3}],
[{"name": "topics/topic", "id": 1}],
]
assert projector.elements_preview == [[{"name": "topics/topic", "id": 3}]]
class VersionView(TestCase): class VersionView(TestCase):