OpenSlides/server/openslides/assignments/serializers.py

206 lines
6.4 KiB
Python
Raw Normal View History

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
)
from openslides.utils.rest_api import (
BooleanField,
DecimalField,
IdPrimaryKeyRelatedField,
IntegerField,
ModelSerializer,
ValidationError,
)
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
2020-04-15 11:59:16 +02:00
from ..utils.validate import validate_html_strict
from .models import (
Assignment,
AssignmentOption,
AssignmentPoll,
AssignmentRelatedUser,
AssignmentVote,
)
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
)
return data
class AssignmentRelatedUserSerializer(ModelSerializer):
"""
Serializer for assignment.models.AssignmentRelatedUser objects.
"""
2019-01-06 16:22:33 +01:00
class Meta:
model = AssignmentRelatedUser
fields = ("id", "user", "weight")
2019-10-29 09:44:19 +01:00
class AssignmentVoteSerializer(BaseVoteSerializer):
"""
Serializer for assignment.models.AssignmentVote objects.
"""
2019-01-06 16:22:33 +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
2019-10-29 09:44:19 +01:00
class AssignmentOptionSerializer(BaseOptionSerializer):
"""
Serializer for assignment.models.AssignmentOption objects.
"""
2019-01-06 16:22:33 +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
2019-10-29 09:44:19 +01:00
class AssignmentPollSerializer(BasePollSerializer):
"""
Serializer for assignment.models.AssignmentPoll objects.
Serializes all polls.
"""
2019-01-06 16:22:33 +01:00
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
)
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",)
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)
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
2019-10-18 14:18:49 +02:00
class AssignmentSerializer(ModelSerializer):
"""
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
)
agenda_create = BooleanField(write_only=True, required=False, allow_null=True)
2019-01-06 16:22:33 +01:00
agenda_type = IntegerField(
write_only=True, required=False, min_value=1, max_value=3, allow_null=True
2019-01-06 16:22:33 +01:00
)
agenda_parent_id = IntegerField(write_only=True, required=False, min_value=1)
polls = IdPrimaryKeyRelatedField(many=True, read_only=True)
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",
"list_of_speakers_id",
"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-11-12 09:20:10 +01:00
"number_poll_candidates",
"polls",
2019-01-06 16:22:33 +01:00
)
validators = (posts_validator,)
def validate(self, data):
2019-01-06 16:22:33 +01:00
if "description" in data:
2020-04-15 11:59:16 +02:00
data["description"] = validate_html_strict(data["description"])
return data
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", [])
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)
assignment = Assignment(**validated_data)
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
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)
return assignment