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
|
|
|
)
|
2015-06-14 23:26:06 +02:00
|
|
|
from openslides.utils.rest_api import (
|
2019-07-10 08:31:58 +02:00
|
|
|
BooleanField,
|
2018-08-22 17:34:16 +02:00
|
|
|
DecimalField,
|
2015-06-14 23:26:06 +02:00
|
|
|
IntegerField,
|
|
|
|
ModelSerializer,
|
2015-06-16 10:37:23 +02:00
|
|
|
ValidationError,
|
|
|
|
)
|
2015-01-17 14:25:05 +01:00
|
|
|
|
2019-10-29 09:44:19 +01:00
|
|
|
from ..utils.auth import has_perm
|
2019-03-04 18:28:21 +01:00
|
|
|
from ..utils.autoupdate import inform_changed_data
|
2019-03-06 14:53:24 +01:00
|
|
|
from ..utils.validate import validate_html
|
2015-01-17 14:25:05 +01:00
|
|
|
from .models import (
|
|
|
|
Assignment,
|
|
|
|
AssignmentOption,
|
|
|
|
AssignmentPoll,
|
2015-06-16 10:37:23 +02:00
|
|
|
AssignmentRelatedUser,
|
|
|
|
AssignmentVote,
|
|
|
|
)
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
|
|
|
2017-01-14 12:07:14 +01:00
|
|
|
def posts_validator(data):
|
|
|
|
"""
|
|
|
|
Validator for open posts. It checks that the values for the open posts are greater than 0.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
if data["open_posts"] and data["open_posts"] is not None and data["open_posts"] < 1:
|
|
|
|
raise ValidationError(
|
2019-01-12 23:01:42 +01:00
|
|
|
{"detail": "Value for 'open_posts' must be greater than 0"}
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2017-01-14 12:07:14 +01:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class AssignmentRelatedUserSerializer(ModelSerializer):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
2015-01-25 15:10:34 +01:00
|
|
|
Serializer for assignment.models.AssignmentRelatedUser objects.
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
class Meta:
|
2015-01-25 15:10:34 +01:00
|
|
|
model = AssignmentRelatedUser
|
2019-10-18 14:18:49 +02:00
|
|
|
fields = ("id", "user", "elected", "weight")
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
|
|
|
2019-10-29 09:44:19 +01:00
|
|
|
class AssignmentVoteSerializer(BaseVoteSerializer):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
Serializer for assignment.models.AssignmentVote objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
class Meta:
|
|
|
|
model = AssignmentVote
|
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-17 14:25:05 +01:00
|
|
|
|
2019-10-29 09:44:19 +01:00
|
|
|
class AssignmentOptionSerializer(BaseOptionSerializer):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
Serializer for assignment.models.AssignmentOption objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
class Meta:
|
|
|
|
model = AssignmentOption
|
2019-10-29 09:00:11 +01:00
|
|
|
fields = ("user", "weight") + BASE_OPTION_FIELDS
|
|
|
|
read_only_fields = ("user", "weight") + BASE_OPTION_FIELDS
|
2016-01-09 19:27:02 +01:00
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
|
2019-10-29 09:44:19 +01:00
|
|
|
class AssignmentPollSerializer(BasePollSerializer):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
Serializer for assignment.models.AssignmentPoll objects.
|
|
|
|
|
|
|
|
Serializes all polls.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-11-25 21:31:08 +01:00
|
|
|
options = AssignmentOptionSerializer(many=True, read_only=True)
|
2019-10-29 09:44:19 +01:00
|
|
|
amount_global_no = DecimalField(
|
2019-10-18 14:18:49 +02:00
|
|
|
max_digits=15, decimal_places=6, min_value=-2, read_only=True
|
|
|
|
)
|
2019-10-29 09:44:19 +01:00
|
|
|
amount_global_abstain = DecimalField(
|
2019-10-18 14:18:49 +02:00
|
|
|
max_digits=15, decimal_places=6, min_value=-2, read_only=True
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AssignmentPoll
|
|
|
|
fields = (
|
2019-01-06 16:22:33 +01:00
|
|
|
"assignment",
|
2019-10-29 09:44:19 +01:00
|
|
|
"description",
|
2019-10-18 14:18:49 +02:00
|
|
|
"pollmethod",
|
|
|
|
"votes_amount",
|
|
|
|
"allow_multiple_votes_per_candidate",
|
|
|
|
"global_no",
|
2019-10-29 09:44:19 +01:00
|
|
|
"amount_global_no",
|
2019-10-18 14:18:49 +02:00
|
|
|
"global_abstain",
|
2019-10-29 09:44:19 +01:00
|
|
|
"amount_global_abstain",
|
2019-10-18 14:18:49 +02:00
|
|
|
) + BASE_POLL_FIELDS
|
|
|
|
read_only_fields = ("state",)
|
2016-01-09 19:27:02 +01:00
|
|
|
|
2015-06-14 23:26:06 +02:00
|
|
|
def update(self, instance, validated_data):
|
2019-10-29 09:44:19 +01:00
|
|
|
""" Prevent updating the assignment """
|
2019-10-18 14:18:49 +02:00
|
|
|
validated_data.pop("assignment", None)
|
|
|
|
return super().update(instance, validated_data)
|
2015-01-17 14:25:05 +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
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Returns None, if the 100-%-base must not be changed, otherwise the correct 100-%-base.
|
|
|
|
"""
|
|
|
|
if pollmethod == AssignmentPoll.POLLMETHOD_YN and onehundred_percent_base in (
|
|
|
|
AssignmentPoll.PERCENT_BASE_VOTES,
|
|
|
|
AssignmentPoll.PERCENT_BASE_YNA,
|
|
|
|
):
|
|
|
|
return AssignmentPoll.PERCENT_BASE_YN
|
|
|
|
if (
|
|
|
|
pollmethod == AssignmentPoll.POLLMETHOD_YNA
|
|
|
|
and onehundred_percent_base == AssignmentPoll.PERCENT_BASE_VOTES
|
|
|
|
):
|
|
|
|
if old_100_percent_base is None:
|
|
|
|
return AssignmentPoll.PERCENT_BASE_YNA
|
|
|
|
else:
|
|
|
|
if old_100_percent_base in (
|
|
|
|
AssignmentPoll.PERCENT_BASE_YN,
|
|
|
|
AssignmentPoll.PERCENT_BASE_YNA,
|
|
|
|
):
|
|
|
|
return old_100_percent_base
|
|
|
|
else:
|
|
|
|
return pollmethod
|
|
|
|
if (
|
|
|
|
pollmethod == AssignmentPoll.POLLMETHOD_VOTES
|
|
|
|
and onehundred_percent_base
|
|
|
|
in (AssignmentPoll.PERCENT_BASE_YN, AssignmentPoll.PERCENT_BASE_YNA)
|
|
|
|
):
|
|
|
|
return AssignmentPoll.PERCENT_BASE_VOTES
|
|
|
|
return None
|
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
class AssignmentSerializer(ModelSerializer):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
Serializer for assignment.models.Assignment objects. With all polls.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
|
|
|
assignment_related_users = AssignmentRelatedUserSerializer(
|
|
|
|
many=True, read_only=True
|
|
|
|
)
|
2019-07-10 08:31:58 +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-07-10 08:31:58 +02: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)
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Assignment
|
|
|
|
fields = (
|
2019-01-06 16:22:33 +01:00
|
|
|
"id",
|
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"open_posts",
|
|
|
|
"phase",
|
|
|
|
"assignment_related_users",
|
2019-10-29 09:44:19 +01:00
|
|
|
"default_poll_description",
|
2019-01-06 16:22:33 +01:00
|
|
|
"agenda_item_id",
|
2019-04-23 16:57:35 +02:00
|
|
|
"list_of_speakers_id",
|
2019-07-10 08:31:58 +02:00
|
|
|
"agenda_create",
|
2019-01-06 16:22:33 +01:00
|
|
|
"agenda_type",
|
|
|
|
"agenda_parent_id",
|
|
|
|
"tags",
|
2019-04-26 13:25:45 +02:00
|
|
|
"attachments",
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2017-01-14 12:07:14 +01:00
|
|
|
validators = (posts_validator,)
|
2015-01-17 14:25:05 +01:00
|
|
|
|
2017-11-20 16:40:39 +01:00
|
|
|
def validate(self, data):
|
2019-01-06 16:22:33 +01:00
|
|
|
if "description" in data:
|
|
|
|
data["description"] = validate_html(data["description"])
|
2017-11-20 16:40:39 +01:00
|
|
|
return data
|
|
|
|
|
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-03-04 18:28:21 +01:00
|
|
|
tags = validated_data.pop("tags", [])
|
2019-04-26 13:25:45 +02:00
|
|
|
attachments = validated_data.pop("attachments", [])
|
2019-07-10 08:31:58 +02:00
|
|
|
request_user = validated_data.pop("request_user") # this should always be there
|
|
|
|
agenda_create = validated_data.pop("agenda_create", None)
|
|
|
|
agenda_type = validated_data.pop("agenda_type", None)
|
|
|
|
agenda_parent_id = validated_data.pop("agenda_parent_id", None)
|
|
|
|
|
2018-01-20 09:54:46 +01:00
|
|
|
assignment = Assignment(**validated_data)
|
2019-07-10 08:31:58 +02:00
|
|
|
if has_perm(request_user, "agenda.can_manage"):
|
|
|
|
assignment.agenda_item_update_information["create"] = agenda_create
|
|
|
|
assignment.agenda_item_update_information["type"] = agenda_type
|
|
|
|
assignment.agenda_item_update_information["parent_id"] = agenda_parent_id
|
|
|
|
|
2018-01-20 09:54:46 +01:00
|
|
|
assignment.save()
|
2019-03-04 18:28:21 +01:00
|
|
|
assignment.tags.add(*tags)
|
2019-04-26 13:25:45 +02:00
|
|
|
assignment.attachments.add(*attachments)
|
2019-03-04 18:28:21 +01:00
|
|
|
inform_changed_data(assignment)
|
2018-01-20 09:54:46 +01:00
|
|
|
return assignment
|