OpenSlides/openslides/assignments/serializers.py

246 lines
7.9 KiB
Python
Raw Normal View History

from django.db import transaction
from openslides.poll.serializers import default_votes_validator
from openslides.utils.rest_api import (
BooleanField,
DecimalField,
DictField,
IntegerField,
ListField,
ModelSerializer,
SerializerMethodField,
ValidationError,
)
from ..utils.auth import has_perm
2019-03-04 18:28:21 +01:00
from ..utils.autoupdate import inform_changed_data
from ..utils.validate import validate_html
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 = (
2019-01-06 16:22:33 +01:00
"id",
"user",
"elected",
"assignment",
"weight",
) # js-data needs the assignment-id in the nested object to define relations.
class AssignmentVoteSerializer(ModelSerializer):
"""
Serializer for assignment.models.AssignmentVote objects.
"""
2019-01-06 16:22:33 +01:00
class Meta:
model = AssignmentVote
2019-01-06 16:22:33 +01:00
fields = ("weight", "value")
class AssignmentOptionSerializer(ModelSerializer):
"""
Serializer for assignment.models.AssignmentOption objects.
"""
2019-01-06 16:22:33 +01:00
votes = AssignmentVoteSerializer(many=True, read_only=True)
is_elected = SerializerMethodField()
class Meta:
model = AssignmentOption
2019-01-06 16:22:33 +01:00
fields = ("id", "candidate", "is_elected", "votes", "poll", "weight")
def get_is_elected(self, obj):
"""
Returns the election status of the candidate of this option.
"""
return obj.poll.assignment.is_elected(obj.candidate)
class AssignmentAllPollSerializer(ModelSerializer):
"""
Serializer for assignment.models.AssignmentPoll objects.
Serializes all polls.
"""
2019-01-06 16:22:33 +01:00
options = AssignmentOptionSerializer(many=True, read_only=True)
votes = ListField(
child=DictField(
2019-01-06 16:22:33 +01:00
child=DecimalField(max_digits=15, decimal_places=6, min_value=-2)
),
write_only=True,
2019-01-06 16:22:33 +01:00
required=False,
)
has_votes = SerializerMethodField()
class Meta:
model = AssignmentPoll
fields = (
2019-01-06 16:22:33 +01:00
"id",
"pollmethod",
"description",
"published",
"options",
"votesabstain",
"votesno",
"votesvalid",
"votesinvalid",
"votescast",
"votes",
"has_votes",
"assignment",
) # js-data needs the assignment-id in the nested object to define relations.
read_only_fields = ("pollmethod",)
validators = (default_votes_validator,)
def get_has_votes(self, obj):
"""
Returns True if this poll has some votes.
"""
return obj.has_votes()
@transaction.atomic
def update(self, instance, validated_data):
"""
Customized update method for polls. To update votes use the write
only field 'votes'.
Example data for a 'pollmethod'='yna' poll with two candidates:
"votes": [{"Yes": 10, "No": 4, "Abstain": -2},
{"Yes": -1, "No": 0, "Abstain": -2}]
Example data for a 'pollmethod' ='yn' poll with two candidates:
"votes": [{"Votes": 10}, {"Votes": 0}]
"""
# Update votes.
2019-01-06 16:22:33 +01:00
votes = validated_data.get("votes")
if votes:
options = list(instance.get_options())
if len(votes) != len(options):
2019-01-06 16:22:33 +01:00
raise ValidationError(
{
2019-01-12 23:01:42 +01:00
"detail": f"You have to submit data for {len(options)} candidates."
2019-01-06 16:22:33 +01:00
}
)
for index, option in enumerate(options):
if len(votes[index]) != len(instance.get_vote_values()):
2019-01-06 16:22:33 +01:00
raise ValidationError(
{
2019-01-12 23:01:42 +01:00
"detail": f"You have to submit data for {len(instance.get_vote_values())} vote values."
2019-01-06 16:22:33 +01:00
}
)
2019-01-12 23:01:42 +01:00
for vote_value, __ in votes[index].items():
if vote_value not in instance.get_vote_values():
2019-01-06 16:22:33 +01:00
raise ValidationError(
2019-01-12 23:01:42 +01:00
{"detail": f"Vote value {vote_value} is invalid."}
2019-01-06 16:22:33 +01:00
)
instance.set_vote_objects_with_values(
option, votes[index], skip_autoupdate=True
)
# Update remaining writeable fields.
2019-01-06 16:22:33 +01:00
instance.description = validated_data.get("description", instance.description)
instance.published = validated_data.get("published", instance.published)
instance.votesabstain = validated_data.get(
"votesabstain", instance.votesabstain
)
instance.votesno = validated_data.get("votesno", instance.votesno)
instance.votesvalid = validated_data.get("votesvalid", instance.votesvalid)
instance.votesinvalid = validated_data.get(
"votesinvalid", instance.votesinvalid
)
instance.votescast = validated_data.get("votescast", instance.votescast)
instance.save()
return instance
class AssignmentFullSerializer(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
)
polls = AssignmentAllPollSerializer(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)
class Meta:
model = Assignment
fields = (
2019-01-06 16:22:33 +01:00
"id",
"title",
"description",
"open_posts",
"phase",
"assignment_related_users",
"poll_description_default",
"polls",
"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-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:
data["description"] = validate_html(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