-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create mixin for motion update and create payload validation
- Loading branch information
1 parent
2d8d4a4
commit fa95426
Showing
6 changed files
with
158 additions
and
57 deletions.
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
69 changes: 69 additions & 0 deletions
69
openslides_backend/action/actions/motion/check_create_update_payload_mixin.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,69 @@ | ||
from typing import Any, Dict | ||
|
||
from openslides_backend.shared.patterns import fqid_from_collection_and_id | ||
|
||
from .set_number_mixin import SetNumberMixin | ||
|
||
|
||
class MotionCheckCreateUpdatePayloadMixin(SetNumberMixin): | ||
""" | ||
TODO: Add description | ||
""" | ||
|
||
def get_payload_integrity_error_message( | ||
self, instance: Dict[str, Any], is_update: bool = False | ||
) -> str | None: | ||
meeting_id = instance["meeting_id"] if not is_update else 0 | ||
if is_update: | ||
if ( | ||
instance.get("text") | ||
or instance.get("amendment_paragraphs") | ||
or instance.get("reason") == "" | ||
or instance.get("number") | ||
): | ||
motion = self.datastore.get( | ||
fqid_from_collection_and_id(self.model.collection, instance["id"]), | ||
["text", "amendment_paragraphs", "meeting_id"], | ||
) | ||
meeting_id = motion["meeting_id"] | ||
|
||
if instance.get("text"): | ||
if not motion.get("text"): | ||
return ( | ||
"Cannot update text, because it was not set in the old values." | ||
) | ||
if instance.get("amendment_paragraphs"): | ||
if not motion.get("amendment_paragraphs"): | ||
return "Cannot update amendment_paragraphs, because it was not set in the old values." | ||
else: | ||
if instance.get("lead_motion_id"): | ||
if instance.get("statute_paragraph_id"): | ||
return "You can't give both of lead_motion_id and statute_paragraph_id." | ||
if not instance.get("text") and not instance.get( | ||
"amendment_paragraphs" | ||
): | ||
return "Text or amendment_paragraphs is required in this context." | ||
if instance.get("text") and instance.get("amendment_paragraphs"): | ||
return "You can't give both of text and amendment_paragraphs" | ||
else: | ||
if not instance.get("text"): | ||
return "Text is required" | ||
if instance.get("amendment_paragraphs"): | ||
return "You can't give amendment_paragraphs in this context" | ||
if instance.get("reason") == "" if is_update else not instance.get("reason"): | ||
meeting = self.datastore.get( | ||
fqid_from_collection_and_id("meeting", meeting_id), | ||
["motions_reason_required"], | ||
) | ||
if meeting.get("motions_reason_required"): | ||
return ( | ||
"Reason is required to update." | ||
if is_update | ||
else "Reason is required" | ||
) | ||
if instance.get("number"): | ||
if not self._check_if_unique( | ||
instance["number"], meeting_id, instance["id"] | ||
): | ||
return "Number is not unique." | ||
return None |
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
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,65 @@ | ||
from ....models.models import Motion | ||
from ....permissions.permissions import Permissions | ||
from ....shared.schema import required_id_schema | ||
from ...mixins.import_mixins import ImportState, JsonUploadMixin | ||
from ...util.default_schema import DefaultSchema | ||
from ...util.register import register_action | ||
|
||
|
||
@register_action("motion.json_upload") | ||
class MotionJsonUpload(JsonUploadMixin): | ||
""" | ||
Action to allow to upload a json. It is used as first step of an import. | ||
""" | ||
|
||
model = Motion() | ||
schema = DefaultSchema(Motion()).get_default_schema( | ||
additional_required_fields={ | ||
"data": { | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"properties": { | ||
**model.get_properties( | ||
"title", | ||
"text", | ||
"number", | ||
"reason", | ||
), | ||
# "submitters_verbose": str, | ||
# "submitters_username": str, | ||
# "supporters_verbose": str, | ||
# "supporters_username": str, | ||
# "category_name": str, | ||
# "category_prefix": str, | ||
# "tags": str, | ||
# "block": str, | ||
# "motion_amendment": str, | ||
}, | ||
"required": ["title", "text"], | ||
"additionalProperties": False, | ||
}, | ||
"minItems": 1, | ||
"uniqueItems": False, | ||
}, | ||
"meeting_id": required_id_schema, | ||
} | ||
) | ||
|
||
headers = [ | ||
# {"property": "title", "type": "string"}, | ||
# {"property": "text", "type": "string"}, | ||
# {"property": "number", "type": "string", "is_object": True}, | ||
# {"property": "reason", "type": "string"}, | ||
# {"property": "submitters_verbose", "type": "string", "is_list": True}, | ||
# {"property": "submitters_username", "type": "string", "is_object": True, "is_list": True}, | ||
# {"property": "supporters_verbose", "type": "string", "is_list": True}, | ||
# {"property": "supporters_usernames", "type": "string", "is_object": True, "is_list": True}, | ||
# {"property": "category_name", "type": "string", "is_object": True}, | ||
# {"property": "category_prefix", "type": "string"}, | ||
# {"property": "tags", "type": "string", "is_object": True, "is_list": True}, | ||
# {"property": "block", "type": "string", "is_object": True}, | ||
# {"property": "motion_amendment", "type": "boolean", "is_object": True}, | ||
] | ||
permission = Permissions.Motion.CAN_MANAGE | ||
row_state: ImportState |
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
Empty file.