Skip to content

Commit

Permalink
Merge pull request #254 from reiterl/dev_motion_submitter_create
Browse files Browse the repository at this point in the history
Update motion_submitter create action.
  • Loading branch information
jsangmeister committed Oct 27, 2020
2 parents 9c9d5b6 + 6055006 commit 9938ed0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions openslides_backend/action/motion_submitter/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from ...models.models import MotionSubmitter
from ...shared.exceptions import ActionException
from ...shared.filters import And, FilterOperator
from ...shared.patterns import Collection, FullQualifiedId
from ..create_action_with_inferred_meeting import CreateActionWithInferredMeetingMixin
from ..default_schema import DefaultSchema
Expand Down Expand Up @@ -36,4 +37,15 @@ def update_instance(self, instance: Dict[str, Any]) -> Dict[str, Any]:
raise ActionException(
"Cannot create motion_submitter, meeting id of motion and (temporary) user don't match."
)

# check, if (user_id, motion_id) already in the databse.
filter = And(
FilterOperator("user_id", "=", instance["user_id"]),
FilterOperator("motion_id", "=", instance["motion_id"]),
)
another_exist = self.database.exists(
collection=self.model.collection, filter=filter
)
if another_exist["exists"]:
raise ActionException("(user_id, motion_id) must be unique.")
return instance
21 changes: 21 additions & 0 deletions tests/system/action/motion_submitter/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ def test_create(self) -> None:
assert model.get("motion_id") == 357
assert model.get("user_id") == 78

def test_create_not_unique(self) -> None:
self.create_model("meeting/111", {"name": "name_m123etrd"})
self.create_model("motion/357", {"title": "title_YIDYXmKj", "meeting_id": 111})
self.create_model(
"user/78", {"username": "username_loetzbfg", "meeting_id": 111}
)
self.create_model(
"motion_submitter/12", {"motion_id": 357, "user_id": 78, "meeting_id": 111}
)
response = self.client.post(
"/",
json=[
{
"action": "motion_submitter.create",
"data": [{"motion_id": 357, "user_id": 78}],
}
],
)
self.assert_status_code(response, 400)
assert "(user_id, motion_id) must be unique." in str(response.data)

def test_create_empty_data(self) -> None:
response = self.client.post(
"/", json=[{"action": "motion_submitter.create", "data": [{}]}],
Expand Down

0 comments on commit 9938ed0

Please sign in to comment.