2019-03-20 17:21:01 +01:00
|
|
|
import jsonschema
|
2015-04-30 19:13:28 +02:00
|
|
|
from django.db import transaction
|
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
from openslides.poll.serializers import (
|
|
|
|
BASE_OPTION_FIELDS,
|
|
|
|
BASE_POLL_FIELDS,
|
|
|
|
BASE_VOTE_FIELDS,
|
2019-10-29 09:44:19 +01:00
|
|
|
BaseOptionSerializer,
|
|
|
|
BasePollSerializer,
|
|
|
|
BaseVoteSerializer,
|
2019-10-18 14:18:49 +02:00
|
|
|
)
|
|
|
|
|
2019-01-31 22:33:44 +01:00
|
|
|
from ..core.config import config
|
2019-07-10 08:31:58 +02:00
|
|
|
from ..utils.auth import get_group_model, has_perm
|
2018-10-01 15:36:16 +02:00
|
|
|
from ..utils.autoupdate import inform_changed_data
|
2017-04-28 22:10:18 +02:00
|
|
|
from ..utils.rest_api import (
|
2019-06-03 17:04:30 +02:00
|
|
|
BooleanField,
|
2015-04-30 19:13:28 +02:00
|
|
|
CharField,
|
2016-07-29 23:33:47 +02:00
|
|
|
Field,
|
2018-08-31 15:33:41 +02:00
|
|
|
IdPrimaryKeyRelatedField,
|
2015-04-30 19:13:28 +02:00
|
|
|
IntegerField,
|
2019-04-01 14:35:02 +02:00
|
|
|
JSONField,
|
2015-04-30 19:13:28 +02:00
|
|
|
ModelSerializer,
|
2015-10-21 21:13:45 +02:00
|
|
|
SerializerMethodField,
|
2015-06-16 10:37:23 +02:00
|
|
|
ValidationError,
|
|
|
|
)
|
2020-04-15 11:59:16 +02:00
|
|
|
from ..utils.validate import validate_html_strict
|
2019-03-06 14:53:24 +01:00
|
|
|
from .models import (
|
|
|
|
Category,
|
|
|
|
Motion,
|
|
|
|
MotionBlock,
|
|
|
|
MotionChangeRecommendation,
|
|
|
|
MotionComment,
|
|
|
|
MotionCommentSection,
|
2019-10-18 14:18:49 +02:00
|
|
|
MotionOption,
|
2019-03-06 14:53:24 +01:00
|
|
|
MotionPoll,
|
2019-10-18 14:18:49 +02:00
|
|
|
MotionVote,
|
2019-03-06 14:53:24 +01:00
|
|
|
State,
|
|
|
|
StatuteParagraph,
|
|
|
|
Submitter,
|
|
|
|
Workflow,
|
|
|
|
)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
|
2015-04-30 19:13:28 +02:00
|
|
|
def validate_workflow_field(value):
|
|
|
|
"""
|
|
|
|
Validator to ensure that the workflow with the given id exists.
|
|
|
|
"""
|
|
|
|
if not Workflow.objects.filter(pk=value).exists():
|
2019-09-02 11:09:03 +02:00
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "Workflow {0} does not exist.", "args": [value]}
|
|
|
|
)
|
2015-04-30 19:13:28 +02:00
|
|
|
|
|
|
|
|
2018-09-24 10:28:31 +02:00
|
|
|
class StatuteParagraphSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for motion.models.StatuteParagraph objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-09-24 10:28:31 +02:00
|
|
|
class Meta:
|
|
|
|
model = StatuteParagraph
|
2019-01-06 16:22:33 +01:00
|
|
|
fields = ("id", "title", "text", "weight")
|
2018-09-24 10:28:31 +02:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class CategorySerializer(ModelSerializer):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
Serializer for motion.models.Category objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-01-24 16:35:50 +01:00
|
|
|
class Meta:
|
|
|
|
model = Category
|
2019-06-03 14:17:42 +02:00
|
|
|
fields = ("id", "name", "prefix", "parent", "weight", "level")
|
|
|
|
read_only_fields = ("parent", "weight")
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
|
2016-10-01 20:42:44 +02:00
|
|
|
class MotionBlockSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for motion.models.Category objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2019-06-03 17:04:30 +02:00
|
|
|
agenda_create = BooleanField(write_only=True, required=False, allow_null=True)
|
2019-01-06 16:22:33 +01:00
|
|
|
agenda_type = IntegerField(
|
2019-03-05 14:18:56 +01:00
|
|
|
write_only=True, required=False, min_value=1, max_value=3, allow_null=True
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2018-01-20 09:54:46 +01:00
|
|
|
agenda_parent_id = IntegerField(write_only=True, required=False, min_value=1)
|
2020-05-28 13:50:54 +02:00
|
|
|
motions_id = SerializerMethodField()
|
2018-01-20 09:54:46 +01:00
|
|
|
|
2016-10-01 20:42:44 +02:00
|
|
|
class Meta:
|
|
|
|
model = MotionBlock
|
2019-04-23 16:57:35 +02:00
|
|
|
fields = (
|
|
|
|
"id",
|
|
|
|
"title",
|
|
|
|
"agenda_item_id",
|
|
|
|
"list_of_speakers_id",
|
2019-06-03 17:04:30 +02:00
|
|
|
"agenda_create",
|
2019-04-23 16:57:35 +02:00
|
|
|
"agenda_type",
|
|
|
|
"agenda_parent_id",
|
2019-05-21 13:52:10 +02:00
|
|
|
"internal",
|
2020-05-28 13:50:54 +02:00
|
|
|
"motions_id",
|
2019-04-23 16:57:35 +02:00
|
|
|
)
|
2018-01-20 09:54:46 +01:00
|
|
|
|
2020-05-28 13:50:54 +02:00
|
|
|
def get_motions_id(self, block):
|
|
|
|
return [motion.id for motion in block.motion_set.all()]
|
|
|
|
|
2018-01-20 09:54:46 +01:00
|
|
|
def create(self, validated_data):
|
|
|
|
"""
|
|
|
|
Customized create method. Set information about related agenda item
|
|
|
|
into agenda_item_update_information container.
|
|
|
|
"""
|
2019-06-03 17:04:30 +02:00
|
|
|
agenda_create = validated_data.pop("agenda_create", None)
|
2019-01-06 16:22:33 +01:00
|
|
|
agenda_type = validated_data.pop("agenda_type", None)
|
|
|
|
agenda_parent_id = validated_data.pop("agenda_parent_id", None)
|
2019-07-10 08:31:58 +02:00
|
|
|
request_user = validated_data.pop("request_user") # this should always be there
|
2018-01-20 09:54:46 +01:00
|
|
|
motion_block = MotionBlock(**validated_data)
|
2019-07-10 08:31:58 +02:00
|
|
|
if has_perm(request_user, "agenda.can_manage"):
|
|
|
|
motion_block.agenda_item_update_information["create"] = agenda_create
|
|
|
|
motion_block.agenda_item_update_information["type"] = agenda_type
|
|
|
|
motion_block.agenda_item_update_information["parent_id"] = agenda_parent_id
|
2018-01-20 09:54:46 +01:00
|
|
|
motion_block.save()
|
|
|
|
return motion_block
|
2016-10-01 20:42:44 +02:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class StateSerializer(ModelSerializer):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
Serializer for motion.models.State objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2019-04-27 16:23:31 +02:00
|
|
|
restriction = JSONField(required=False)
|
2019-04-01 14:35:02 +02:00
|
|
|
|
2015-01-24 16:35:50 +01:00
|
|
|
class Meta:
|
|
|
|
model = State
|
|
|
|
fields = (
|
2019-01-06 16:22:33 +01:00
|
|
|
"id",
|
|
|
|
"name",
|
|
|
|
"recommendation_label",
|
|
|
|
"css_class",
|
2019-03-20 17:21:01 +01:00
|
|
|
"restriction",
|
2019-01-06 16:22:33 +01:00
|
|
|
"allow_support",
|
|
|
|
"allow_create_poll",
|
|
|
|
"allow_submitter_edit",
|
|
|
|
"dont_set_identifier",
|
|
|
|
"show_state_extension_field",
|
|
|
|
"merge_amendment_into_final",
|
|
|
|
"show_recommendation_extension_field",
|
|
|
|
"next_states",
|
|
|
|
"workflow",
|
|
|
|
)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2019-03-20 17:21:01 +01:00
|
|
|
def validate_restriction(self, value):
|
|
|
|
"""
|
|
|
|
Ensures that the value is a list and only contains valid values.
|
|
|
|
"""
|
|
|
|
schema = {
|
|
|
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
|
|
"title": "Motion workflow state restriction field schema",
|
|
|
|
"description": "An array containing one or more explicit strings to control restriction for motions in this state.",
|
|
|
|
"type": "array",
|
2019-04-01 14:35:02 +02:00
|
|
|
"items": {
|
|
|
|
"type": "string",
|
|
|
|
"enum": [
|
|
|
|
"motions.can_see_internal",
|
|
|
|
"motions.can_manage_metadata",
|
2019-04-29 09:02:10 +02:00
|
|
|
"motions.can_manage",
|
2019-04-01 14:35:02 +02:00
|
|
|
"is_submitter",
|
|
|
|
],
|
|
|
|
},
|
2019-03-20 17:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Validate value.
|
|
|
|
try:
|
2019-04-01 14:35:02 +02:00
|
|
|
jsonschema.validate(value, schema)
|
2019-03-20 17:21:01 +01:00
|
|
|
except jsonschema.ValidationError as err:
|
|
|
|
raise ValidationError({"detail": str(err)})
|
|
|
|
return value
|
|
|
|
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class WorkflowSerializer(ModelSerializer):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
Serializer for motion.models.Workflow objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2019-07-17 16:13:49 +02:00
|
|
|
# states = StateSerializer(many=True, read_only=True)
|
|
|
|
states = IdPrimaryKeyRelatedField(many=True, read_only=True)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Workflow
|
2019-01-06 16:22:33 +01:00
|
|
|
fields = ("id", "name", "states", "first_state")
|
|
|
|
read_only_fields = ("first_state",)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2018-06-26 15:59:05 +02:00
|
|
|
@transaction.atomic
|
|
|
|
def create(self, validated_data):
|
|
|
|
"""
|
|
|
|
Customized create method. Creating a new workflow does always create a
|
|
|
|
new state which is used as first state.
|
|
|
|
"""
|
|
|
|
workflow = super().create(validated_data)
|
|
|
|
first_state = State.objects.create(
|
2019-01-06 16:22:33 +01:00
|
|
|
name="new",
|
2018-06-26 15:59:05 +02:00
|
|
|
workflow=workflow,
|
|
|
|
allow_create_poll=True,
|
|
|
|
allow_support=True,
|
2019-01-06 16:22:33 +01:00
|
|
|
allow_submitter_edit=True,
|
2018-06-26 15:59:05 +02:00
|
|
|
)
|
|
|
|
workflow.first_state = first_state
|
|
|
|
workflow.save()
|
|
|
|
return workflow
|
|
|
|
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2018-06-12 13:43:28 +02:00
|
|
|
class AmendmentParagraphsJSONSerializerField(Field):
|
|
|
|
"""
|
|
|
|
Serializer for motions's amendment_paragraphs JSONField.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-06-12 13:43:28 +02:00
|
|
|
def to_representation(self, obj):
|
|
|
|
"""
|
|
|
|
Returns the value of the field.
|
|
|
|
"""
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
"""
|
|
|
|
Checks that data is a list of strings.
|
|
|
|
"""
|
2019-01-12 23:01:42 +01:00
|
|
|
if not isinstance(data, list):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": "Data must be a list."})
|
2018-06-12 13:43:28 +02:00
|
|
|
for paragraph in data:
|
2019-01-12 23:01:42 +01:00
|
|
|
if not isinstance(paragraph, str) and paragraph is not None:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "Paragraph must be either a string or null/None."}
|
|
|
|
)
|
2018-06-12 13:43:28 +02:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
2019-10-29 09:44:19 +01:00
|
|
|
class MotionVoteSerializer(BaseVoteSerializer):
|
2019-10-18 14:18:49 +02:00
|
|
|
class Meta:
|
|
|
|
model = MotionVote
|
2019-10-29 09:44:19 +01:00
|
|
|
fields = BASE_VOTE_FIELDS
|
2019-10-18 14:18:49 +02:00
|
|
|
read_only_fields = BASE_VOTE_FIELDS
|
|
|
|
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2019-10-29 09:44:19 +01:00
|
|
|
class MotionOptionSerializer(BaseOptionSerializer):
|
2019-10-18 14:18:49 +02:00
|
|
|
class Meta:
|
|
|
|
model = MotionOption
|
|
|
|
fields = BASE_OPTION_FIELDS
|
|
|
|
read_only_fields = BASE_OPTION_FIELDS
|
2016-02-27 20:49:28 +01:00
|
|
|
|
2015-10-21 21:13:45 +02:00
|
|
|
|
2019-10-29 09:44:19 +01:00
|
|
|
class MotionPollSerializer(BasePollSerializer):
|
2019-10-18 14:18:49 +02:00
|
|
|
"""
|
|
|
|
Serializer for motion.models.MotionPoll objects.
|
|
|
|
"""
|
2015-10-21 21:13:45 +02:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
class Meta:
|
|
|
|
model = MotionPoll
|
|
|
|
fields = ("motion", "pollmethod") + BASE_POLL_FIELDS
|
|
|
|
read_only_fields = ("state",)
|
2015-07-22 15:23:57 +02:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def update(self, instance, validated_data):
|
2021-04-26 08:31:22 +02:00
|
|
|
"""Prevent updating the motion"""
|
2019-10-18 14:18:49 +02:00
|
|
|
validated_data.pop("motion", None)
|
|
|
|
return super().update(instance, validated_data)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2019-10-29 09:44:19 +01:00
|
|
|
def norm_100_percent_base_to_pollmethod(
|
|
|
|
self, onehundred_percent_base, pollmethod, old_100_percent_base=None
|
|
|
|
):
|
|
|
|
if (
|
|
|
|
pollmethod == MotionPoll.POLLMETHOD_YN
|
|
|
|
and onehundred_percent_base == MotionPoll.PERCENT_BASE_YNA
|
|
|
|
):
|
|
|
|
return MotionPoll.PERCENT_BASE_YN
|
|
|
|
return None
|
|
|
|
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2016-09-10 18:49:38 +02:00
|
|
|
class MotionChangeRecommendationSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for motion.models.MotionChangeRecommendation objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-09-10 18:49:38 +02:00
|
|
|
class Meta:
|
|
|
|
model = MotionChangeRecommendation
|
|
|
|
fields = (
|
2019-01-06 16:22:33 +01:00
|
|
|
"id",
|
|
|
|
"motion",
|
|
|
|
"rejected",
|
|
|
|
"internal",
|
|
|
|
"type",
|
|
|
|
"other_description",
|
|
|
|
"line_from",
|
|
|
|
"line_to",
|
|
|
|
"text",
|
|
|
|
"creation_time",
|
|
|
|
)
|
2016-09-10 18:49:38 +02:00
|
|
|
|
2018-03-07 16:36:30 +01:00
|
|
|
def is_title_cr(self, data):
|
2019-01-06 16:22:33 +01:00
|
|
|
return int(data["line_from"]) == 0 and int(data["line_to"]) == 0
|
2018-03-07 16:36:30 +01:00
|
|
|
|
2017-01-20 11:34:05 +01:00
|
|
|
def validate(self, data):
|
2018-03-07 16:36:30 +01:00
|
|
|
# Change recommendations for titles are stored as plain-text, thus they don't need to be html-escaped
|
2019-01-06 16:22:33 +01:00
|
|
|
if "text" in data and not self.is_title_cr(data):
|
2020-04-15 11:59:16 +02:00
|
|
|
data["text"] = validate_html_strict(data["text"])
|
2017-01-20 11:34:05 +01:00
|
|
|
return data
|
|
|
|
|
2016-09-10 18:49:38 +02:00
|
|
|
|
2018-08-31 15:33:41 +02:00
|
|
|
class MotionCommentSectionSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for motion.models.MotionCommentSection objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-08-31 15:33:41 +02:00
|
|
|
read_groups = IdPrimaryKeyRelatedField(
|
2019-01-06 16:22:33 +01:00
|
|
|
many=True, required=False, queryset=get_group_model().objects.all()
|
|
|
|
)
|
2018-08-31 15:33:41 +02:00
|
|
|
|
|
|
|
write_groups = IdPrimaryKeyRelatedField(
|
2019-01-06 16:22:33 +01:00
|
|
|
many=True, required=False, queryset=get_group_model().objects.all()
|
|
|
|
)
|
2018-08-31 15:33:41 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = MotionCommentSection
|
2019-07-19 13:43:33 +02:00
|
|
|
fields = ("id", "name", "read_groups", "write_groups", "weight")
|
|
|
|
read_only_fields = ("weight",)
|
2018-08-31 15:33:41 +02:00
|
|
|
|
2018-10-01 15:36:16 +02:00
|
|
|
def create(self, validated_data):
|
2021-04-26 08:31:22 +02:00
|
|
|
"""Call inform_changed_data on creation, so the cache includes the groups."""
|
2018-10-01 15:36:16 +02:00
|
|
|
section = super().create(validated_data)
|
|
|
|
inform_changed_data(section)
|
|
|
|
return section
|
|
|
|
|
2018-08-31 15:33:41 +02:00
|
|
|
|
|
|
|
class MotionCommentSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for motion.models.MotionComment objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-08-31 15:33:41 +02:00
|
|
|
read_groups_id = SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = MotionComment
|
2019-01-06 16:22:33 +01:00
|
|
|
fields = ("id", "comment", "section", "read_groups_id")
|
2018-08-31 15:33:41 +02:00
|
|
|
|
|
|
|
def get_read_groups_id(self, comment):
|
|
|
|
return [group.id for group in comment.section.read_groups.all()]
|
|
|
|
|
|
|
|
|
2018-06-12 14:17:02 +02:00
|
|
|
class SubmitterSerializer(ModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for motion.models.Submitter objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-06-12 14:17:02 +02:00
|
|
|
class Meta:
|
|
|
|
model = Submitter
|
2019-01-06 16:22:33 +01:00
|
|
|
fields = ("id", "user", "motion", "weight")
|
2018-06-12 14:17:02 +02:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class MotionSerializer(ModelSerializer):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
Serializer for motion.models.Motion objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-08-31 15:33:41 +02:00
|
|
|
comments = MotionCommentSerializer(many=True, read_only=True)
|
|
|
|
modified_final_version = CharField(allow_blank=True, required=False)
|
|
|
|
reason = CharField(allow_blank=True, required=False)
|
2019-03-20 17:21:01 +01:00
|
|
|
state_restriction = SerializerMethodField()
|
2019-05-20 12:25:07 +02:00
|
|
|
text = CharField(allow_blank=True, required=False) # This will be checked
|
|
|
|
# during validation
|
2018-08-31 15:33:41 +02:00
|
|
|
title = CharField(max_length=255)
|
2019-10-07 08:22:16 +02:00
|
|
|
amendment_paragraphs = AmendmentParagraphsJSONSerializerField(
|
|
|
|
required=False, allow_null=True
|
|
|
|
)
|
2015-11-03 10:03:44 +01:00
|
|
|
workflow_id = IntegerField(
|
2019-01-06 16:22:33 +01:00
|
|
|
min_value=1, required=False, validators=[validate_workflow_field]
|
|
|
|
)
|
2019-06-03 17:04:30 +02:00
|
|
|
agenda_create = BooleanField(write_only=True, required=False, allow_null=True)
|
2019-01-06 16:22:33 +01:00
|
|
|
agenda_type = IntegerField(
|
2019-03-05 14:18:56 +01:00
|
|
|
write_only=True, required=False, min_value=1, max_value=3, allow_null=True
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2018-01-20 09:54:46 +01:00
|
|
|
agenda_parent_id = IntegerField(write_only=True, required=False, min_value=1)
|
2018-06-12 14:17:02 +02:00
|
|
|
submitters = SubmitterSerializer(many=True, read_only=True)
|
2019-07-17 16:13:49 +02:00
|
|
|
change_recommendations = IdPrimaryKeyRelatedField(many=True, read_only=True)
|
2020-05-28 13:50:54 +02:00
|
|
|
amendments_id = SerializerMethodField()
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Motion
|
|
|
|
fields = (
|
2019-01-06 16:22:33 +01:00
|
|
|
"id",
|
|
|
|
"identifier",
|
|
|
|
"title",
|
|
|
|
"text",
|
|
|
|
"amendment_paragraphs",
|
|
|
|
"modified_final_version",
|
|
|
|
"reason",
|
|
|
|
"parent",
|
|
|
|
"category",
|
2019-04-30 13:48:21 +02:00
|
|
|
"category_weight",
|
2019-01-06 16:22:33 +01:00
|
|
|
"comments",
|
|
|
|
"motion_block",
|
|
|
|
"origin",
|
|
|
|
"submitters",
|
|
|
|
"supporters",
|
|
|
|
"state",
|
|
|
|
"state_extension",
|
2019-03-20 17:21:01 +01:00
|
|
|
"state_restriction",
|
2019-01-06 16:22:33 +01:00
|
|
|
"statute_paragraph",
|
|
|
|
"workflow_id",
|
|
|
|
"recommendation",
|
|
|
|
"recommendation_extension",
|
|
|
|
"tags",
|
|
|
|
"attachments",
|
|
|
|
"agenda_item_id",
|
2019-04-23 16:57:35 +02:00
|
|
|
"list_of_speakers_id",
|
2019-06-03 17:04:30 +02:00
|
|
|
"agenda_create",
|
2019-01-06 16:22:33 +01:00
|
|
|
"agenda_type",
|
|
|
|
"agenda_parent_id",
|
|
|
|
"sort_parent",
|
|
|
|
"weight",
|
2019-01-19 10:27:50 +01:00
|
|
|
"created",
|
|
|
|
"last_modified",
|
2019-01-27 13:17:17 +01:00
|
|
|
"change_recommendations",
|
2020-05-28 13:50:54 +02:00
|
|
|
"amendments_id",
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
|
|
|
read_only_fields = (
|
|
|
|
"state",
|
|
|
|
"recommendation",
|
2019-04-30 13:48:21 +02:00
|
|
|
"weight",
|
|
|
|
"category_weight",
|
2020-05-28 13:50:54 +02:00
|
|
|
"amendments_id",
|
2019-01-06 16:22:33 +01:00
|
|
|
) # Some other fields are also read_only. See definitions above.
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2020-05-28 13:50:54 +02:00
|
|
|
def get_amendments_id(self, motion):
|
|
|
|
return [amendment.id for amendment in motion.amendments.all()]
|
|
|
|
|
2017-01-20 11:34:05 +01:00
|
|
|
def validate(self, data):
|
2019-01-06 16:22:33 +01:00
|
|
|
if "text" in data:
|
2020-04-15 11:59:16 +02:00
|
|
|
data["text"] = validate_html_strict(data["text"])
|
2019-01-06 16:22:33 +01:00
|
|
|
|
|
|
|
if "modified_final_version" in data:
|
2020-04-15 11:59:16 +02:00
|
|
|
data["modified_final_version"] = validate_html_strict(
|
2019-01-06 16:22:33 +01:00
|
|
|
data["modified_final_version"]
|
|
|
|
)
|
|
|
|
|
|
|
|
if "reason" in data:
|
2020-04-15 11:59:16 +02:00
|
|
|
data["reason"] = validate_html_strict(data["reason"])
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2019-05-20 12:25:07 +02:00
|
|
|
# The motion text is only needed, if it is not a paragraph based amendment.
|
2019-10-07 08:22:16 +02:00
|
|
|
if data.get("amendment_paragraphs") is not None:
|
2019-01-06 16:22:33 +01:00
|
|
|
data["amendment_paragraphs"] = list(
|
|
|
|
map(
|
2020-04-15 11:59:16 +02:00
|
|
|
lambda entry: validate_html_strict(entry)
|
2019-01-12 23:01:42 +01:00
|
|
|
if isinstance(entry, str)
|
|
|
|
else None,
|
2019-01-06 16:22:33 +01:00
|
|
|
data["amendment_paragraphs"],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
data["text"] = ""
|
2018-06-12 13:43:28 +02:00
|
|
|
else:
|
2019-05-20 12:25:07 +02:00
|
|
|
if (self.partial and "text" in data and not data["text"]) or (
|
|
|
|
not self.partial and not data.get("text")
|
|
|
|
):
|
2019-01-31 22:33:44 +01:00
|
|
|
raise ValidationError({"detail": "The text field may not be blank."})
|
2019-03-30 11:08:35 +01:00
|
|
|
if config["motions_reason_required"]:
|
|
|
|
if (self.partial and "reason" in data and not data["reason"]) or (
|
|
|
|
not self.partial and not data.get("reason")
|
2019-01-31 22:33:44 +01:00
|
|
|
):
|
|
|
|
raise ValidationError({"detail": "The reason field may not be blank."})
|
2018-06-12 13:43:28 +02:00
|
|
|
|
2017-01-20 11:34:05 +01:00
|
|
|
return data
|
|
|
|
|
2015-04-30 19:13:28 +02:00
|
|
|
@transaction.atomic
|
|
|
|
def create(self, validated_data):
|
|
|
|
"""
|
|
|
|
Customized method to create a new motion from some data.
|
2018-01-20 09:54:46 +01:00
|
|
|
|
|
|
|
Set also information about related agenda item into
|
|
|
|
agenda_item_update_information container.
|
2015-04-30 19:13:28 +02:00
|
|
|
"""
|
|
|
|
motion = Motion()
|
2019-01-06 16:22:33 +01:00
|
|
|
motion.title = validated_data["title"]
|
|
|
|
motion.text = validated_data["text"]
|
|
|
|
motion.amendment_paragraphs = validated_data.get("amendment_paragraphs")
|
|
|
|
motion.modified_final_version = validated_data.get("modified_final_version", "")
|
|
|
|
motion.reason = validated_data.get("reason", "")
|
|
|
|
motion.identifier = validated_data.get("identifier")
|
|
|
|
motion.category = validated_data.get("category")
|
|
|
|
motion.motion_block = validated_data.get("motion_block")
|
|
|
|
motion.origin = validated_data.get("origin", "")
|
|
|
|
motion.parent = validated_data.get("parent")
|
|
|
|
motion.statute_paragraph = validated_data.get("statute_paragraph")
|
|
|
|
motion.reset_state(validated_data.get("workflow_id"))
|
2019-07-10 08:31:58 +02:00
|
|
|
if has_perm(validated_data["request_user"], "agenda.can_manage"):
|
|
|
|
motion.agenda_item_update_information["create"] = validated_data.get(
|
|
|
|
"agenda_create"
|
|
|
|
)
|
|
|
|
motion.agenda_item_update_information["type"] = validated_data.get(
|
|
|
|
"agenda_type"
|
|
|
|
)
|
|
|
|
motion.agenda_item_update_information["parent_id"] = validated_data.get(
|
|
|
|
"agenda_parent_id"
|
|
|
|
)
|
2015-04-30 19:13:28 +02:00
|
|
|
motion.save()
|
2019-01-06 16:22:33 +01:00
|
|
|
motion.supporters.add(*validated_data.get("supporters", []))
|
|
|
|
motion.attachments.add(*validated_data.get("attachments", []))
|
|
|
|
motion.tags.add(*validated_data.get("tags", []))
|
2020-05-28 13:50:54 +02:00
|
|
|
|
|
|
|
if motion.parent:
|
|
|
|
inform_changed_data(motion.parent)
|
|
|
|
if motion.motion_block:
|
|
|
|
inform_changed_data(motion.motion_block)
|
|
|
|
|
2015-04-30 19:13:28 +02:00
|
|
|
return motion
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def update(self, motion, validated_data):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
2015-04-30 19:13:28 +02:00
|
|
|
Customized method to update a motion.
|
2019-04-30 13:48:21 +02:00
|
|
|
- If the workflow changes, the state of the motions is resetted to
|
|
|
|
the initial state of the new workflow.
|
|
|
|
- If the category changes, the category_weight is reset to the default value.
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
2018-08-31 15:33:41 +02:00
|
|
|
workflow_id = None
|
2019-01-06 16:22:33 +01:00
|
|
|
if "workflow_id" in validated_data:
|
|
|
|
workflow_id = validated_data.pop("workflow_id")
|
2018-08-31 15:33:41 +02:00
|
|
|
|
2019-04-30 13:48:21 +02:00
|
|
|
old_category_id = motion.category.pk if motion.category is not None else None
|
|
|
|
new_category_id = (
|
|
|
|
validated_data["category"].pk
|
|
|
|
if validated_data.get("category") is not None
|
|
|
|
else None
|
|
|
|
)
|
2020-05-28 13:50:54 +02:00
|
|
|
old_block = motion.motion_block
|
|
|
|
new_block = validated_data.get("motion_block")
|
2019-04-30 13:48:21 +02:00
|
|
|
|
2018-08-31 15:33:41 +02:00
|
|
|
result = super().update(motion, validated_data)
|
2015-04-30 19:13:28 +02:00
|
|
|
|
2019-04-30 13:48:21 +02:00
|
|
|
# Check for changed workflow
|
2018-09-06 13:39:16 +02:00
|
|
|
if workflow_id is not None and workflow_id != motion.workflow_id:
|
2015-11-03 10:03:44 +01:00
|
|
|
motion.reset_state(workflow_id)
|
2019-04-30 13:48:21 +02:00
|
|
|
motion.save(skip_autoupdate=True)
|
|
|
|
|
|
|
|
# Check for changed category
|
|
|
|
if old_category_id != new_category_id:
|
|
|
|
motion.category_weight = 10000
|
|
|
|
motion.save(skip_autoupdate=True)
|
|
|
|
|
|
|
|
inform_changed_data(motion)
|
2015-04-30 19:13:28 +02:00
|
|
|
|
2020-05-28 13:50:54 +02:00
|
|
|
if new_block != old_block:
|
|
|
|
if new_block:
|
|
|
|
inform_changed_data(new_block)
|
|
|
|
if old_block:
|
|
|
|
inform_changed_data(old_block)
|
|
|
|
|
2018-08-31 15:33:41 +02:00
|
|
|
return result
|
2016-12-09 18:00:45 +01:00
|
|
|
|
2019-03-20 17:21:01 +01:00
|
|
|
def get_state_restriction(self, motion):
|
2016-12-09 18:00:45 +01:00
|
|
|
"""
|
2019-03-20 17:21:01 +01:00
|
|
|
Returns the restriction of this state. The default is an empty list so everybody
|
2019-02-01 13:15:08 +01:00
|
|
|
with permission to see motions can see this motion.
|
2016-12-09 18:00:45 +01:00
|
|
|
"""
|
2019-03-20 17:21:01 +01:00
|
|
|
return motion.state.restriction
|