-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add projector.add_to_preview and tests. (#552)
* Add projector.add_to_preview and tests. * Small fixes and update tests-
- Loading branch information
Showing
5 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from . import delete, set_weight # noqa | ||
from . import create, delete, set_weight # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from ....models.models import Projection | ||
from ...generics.create import CreateAction | ||
from ...util.default_schema import DefaultSchema | ||
from ...util.register import register_action | ||
|
||
|
||
@register_action("projection.create", internal=True) | ||
class ProjectionCreate(CreateAction): | ||
""" | ||
Action to create a projection. | ||
""" | ||
|
||
model = Projection() | ||
schema = DefaultSchema(Projection()).get_create_schema( | ||
required_properties=["content_object_id", "meeting_id"], | ||
optional_properties=[ | ||
"options", | ||
"stable", | ||
"type", | ||
"weight", | ||
"current_projector_id", | ||
"preview_projector_id", | ||
"history_projector_id", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from . import ( # noqa | ||
add_to_preview, | ||
control_view, | ||
create, | ||
next, | ||
|
56 changes: 56 additions & 0 deletions
56
openslides_backend/action/actions/projector/add_to_preview.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from ....models.models import Projection, Projector | ||
from ....shared.filters import FilterOperator | ||
from ....shared.patterns import Collection, string_to_fqid | ||
from ....shared.schema import required_id_schema | ||
from ...generics.update import UpdateAction | ||
from ...util.default_schema import DefaultSchema | ||
from ...util.register import register_action | ||
from ...util.typing import ActionData | ||
from ..projection.create import ProjectionCreate | ||
|
||
|
||
@register_action("projector.add_to_preview") | ||
class ProjectorProject(UpdateAction): | ||
""" | ||
Action to projector project. | ||
""" | ||
|
||
model = Projector() | ||
schema = DefaultSchema(Projection()).get_default_schema( | ||
required_properties=["content_object_id"], | ||
optional_properties=["options", "stable", "type"], | ||
additional_required_fields={ | ||
"ids": {"type": "array", "items": required_id_schema, "uniqueItems": True} | ||
}, | ||
title="Projector project schema", | ||
) | ||
|
||
def get_updated_instances(self, action_data: ActionData) -> ActionData: | ||
for instance in action_data: | ||
# check content object and get meeting id. | ||
content_object = self.datastore.get( | ||
string_to_fqid(instance["content_object_id"]), ["meeting_id"] | ||
) | ||
meeting_id = content_object["meeting_id"] | ||
|
||
# add the preview projections | ||
for projector_id in instance["ids"]: | ||
max_weight = self.get_max_projection_weight(projector_id) | ||
data = { | ||
"meeting_id": meeting_id, | ||
"preview_projector_id": projector_id, | ||
"weight": max_weight + 1, | ||
"content_object_id": instance["content_object_id"], | ||
} | ||
for field in ("options", "stable", "type"): | ||
if instance.get(field): | ||
data[field] = instance[field] | ||
self.execute_other_action(ProjectionCreate, [data]) | ||
return [] | ||
|
||
def get_max_projection_weight(self, projector_id: int) -> int: | ||
filter_ = FilterOperator("preview_projector_id", "=", projector_id) | ||
maximum = self.datastore.max(Collection("projection"), filter_, "weight", "int") | ||
if maximum is None: | ||
maximum = 1 | ||
return maximum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from tests.system.action.base import BaseActionTestCase | ||
|
||
|
||
class ProjectorAddToPreview(BaseActionTestCase): | ||
def setUp(self) -> None: | ||
super().setUp() | ||
self.set_models( | ||
{ | ||
"meeting/1": {}, | ||
"meeting/2": {}, | ||
"assignment/1": {"meeting_id": 1}, | ||
"projector/1": {"meeting_id": 1, "preview_projection_ids": [10]}, | ||
"projector/2": {"meeting_id": 1, "preview_projection_ids": [11, 12]}, | ||
"projector/3": {"meeting_id": 1}, | ||
"projector/4": {"meeting_id": 2}, | ||
"projection/10": { | ||
"meeting_id": 1, | ||
"content_object_id": "assignment/1", | ||
"preview_projector_id": 1, | ||
"weight": 10, | ||
}, | ||
"projection/11": { | ||
"meeting_id": 1, | ||
"content_object_id": "assignment/1", | ||
"preview_projector_id": 2, | ||
"weight": 20, | ||
}, | ||
"projection/12": { | ||
"meeting_id": 1, | ||
"content_object_id": "assignment/1", | ||
"preview_projector_id": 2, | ||
"weight": 30, | ||
}, | ||
} | ||
) | ||
|
||
def test_add_to_preview(self) -> None: | ||
response = self.request( | ||
"projector.add_to_preview", | ||
{"ids": [1, 2], "content_object_id": "assignment/1", "stable": False}, | ||
) | ||
self.assert_status_code(response, 200) | ||
projector_1 = self.get_model("projector/1") | ||
assert projector_1.get("preview_projection_ids") == [10, 13] | ||
projector_2 = self.get_model("projector/2") | ||
assert projector_2.get("preview_projection_ids") == [11, 12, 14] | ||
projection_13 = self.get_model("projection/13") | ||
assert projection_13.get("preview_projector_id") == 1 | ||
assert projection_13.get("content_object_id") == "assignment/1" | ||
assert projection_13.get("weight") == 11 | ||
projection_14 = self.get_model("projection/14") | ||
assert projection_14.get("preview_projector_id") == 2 | ||
assert projection_14.get("content_object_id") == "assignment/1" | ||
assert projection_14.get("weight") == 31 | ||
|
||
def test_add_to_preview_empty_projector(self) -> None: | ||
response = self.request( | ||
"projector.add_to_preview", | ||
{"ids": [3], "content_object_id": "assignment/1", "stable": False}, | ||
) | ||
self.assert_status_code(response, 200) | ||
projector_1 = self.get_model("projector/3") | ||
assert projector_1.get("preview_projection_ids") == [13] | ||
projection_13 = self.get_model("projection/13") | ||
assert projection_13.get("preview_projector_id") == 3 | ||
assert projection_13.get("content_object_id") == "assignment/1" | ||
assert projection_13.get("weight") == 2 | ||
|
||
def test_add_to_preview_non_unique_ids(self) -> None: | ||
response = self.request( | ||
"projector.add_to_preview", | ||
{"ids": [1, 1], "content_object_id": "assignment/1", "stable": False}, | ||
) | ||
self.assert_status_code(response, 400) | ||
assert "data.ids must contain unique items" in response.data.decode() | ||
|
||
def test_add_to_preview_check_meeting_id(self) -> None: | ||
response = self.request( | ||
"projector.add_to_preview", | ||
{"ids": [4], "content_object_id": "assignment/1", "stable": False}, | ||
) | ||
self.assert_status_code(response, 400) | ||
assert ( | ||
"The relation preview_projector_id requires the following fields to be equal:\\nprojection/13/meeting_id: 1\\nprojector/4/meeting_id: 2" | ||
in response.data.decode() | ||
) |