2015-01-24 16:35:50 +01:00
|
|
|
from openslides.utils.rest_api import serializers
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
|
|
from .models import (
|
|
|
|
models,
|
|
|
|
Assignment,
|
|
|
|
AssignmentCandidate,
|
|
|
|
AssignmentOption,
|
|
|
|
AssignmentPoll,
|
|
|
|
AssignmentVote)
|
|
|
|
|
|
|
|
|
2015-02-04 00:08:38 +01:00
|
|
|
class AssignmentCandidateSerializer(serializers.ModelSerializer):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
Serializer for assignment.models.AssignmentCandidate objects.
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = AssignmentCandidate
|
|
|
|
fields = (
|
|
|
|
'id',
|
|
|
|
'person',
|
|
|
|
'elected',
|
2015-02-04 00:08:38 +01:00
|
|
|
'blocked',)
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
|
|
|
2015-02-04 00:08:38 +01:00
|
|
|
class AssignmentVoteSerializer(serializers.ModelSerializer):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
Serializer for assignment.models.AssignmentVote objects.
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = AssignmentVote
|
2015-02-04 00:08:38 +01:00
|
|
|
fields = ('weight', 'value',)
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
|
|
|
2015-02-04 00:08:38 +01:00
|
|
|
class AssignmentOptionSerializer(serializers.ModelSerializer):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
Serializer for assignment.models.AssignmentOption objects.
|
|
|
|
"""
|
|
|
|
assignmentvote_set = AssignmentVoteSerializer(many=True, read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AssignmentOption
|
2015-02-04 00:08:38 +01:00
|
|
|
fields = ('candidate', 'assignmentvote_set',)
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
|
|
|
2015-01-24 16:35:50 +01:00
|
|
|
class FilterPollListSerializer(serializers.ListSerializer):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
2015-01-24 16:35:50 +01:00
|
|
|
Customized serializer to filter polls (exclude unpublished).
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
def to_representation(self, data):
|
|
|
|
"""
|
|
|
|
List of object instances -> List of dicts of primitive datatypes.
|
|
|
|
|
|
|
|
This method is adapted to filter the data and exclude unpublished polls.
|
|
|
|
"""
|
|
|
|
# Dealing with nested relationships, data can be a Manager,
|
|
|
|
# so, first get a queryset from the Manager if needed
|
|
|
|
iterable = data.filter(published=True) if isinstance(data, models.Manager) else data
|
|
|
|
return [self.child.to_representation(item) for item in iterable]
|
|
|
|
|
|
|
|
|
2015-02-04 00:08:38 +01:00
|
|
|
class AssignmentAllPollSerializer(serializers.ModelSerializer):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
Serializer for assignment.models.AssignmentPoll objects.
|
|
|
|
|
|
|
|
Serializes all polls.
|
|
|
|
"""
|
|
|
|
assignmentoption_set = AssignmentOptionSerializer(many=True, read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AssignmentPoll
|
|
|
|
fields = (
|
|
|
|
'id',
|
|
|
|
'yesnoabstain',
|
|
|
|
'description',
|
|
|
|
'published',
|
|
|
|
'assignmentoption_set',
|
|
|
|
'votesvalid',
|
|
|
|
'votesinvalid',
|
2015-02-04 00:08:38 +01:00
|
|
|
'votescast',)
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AssignmentShortPollSerializer(AssignmentAllPollSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for assignment.models.AssignmentPoll objects.
|
|
|
|
|
|
|
|
Serializes only short polls.
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
list_serializer_class = FilterPollListSerializer
|
|
|
|
model = AssignmentPoll
|
|
|
|
fields = (
|
|
|
|
'id',
|
|
|
|
'yesnoabstain',
|
|
|
|
'description',
|
|
|
|
'published',
|
|
|
|
'assignmentoption_set',
|
|
|
|
'votesvalid',
|
|
|
|
'votesinvalid',
|
2015-02-04 00:08:38 +01:00
|
|
|
'votescast',)
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
|
|
|
2015-02-04 00:08:38 +01:00
|
|
|
class AssignmentFullSerializer(serializers.ModelSerializer):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
Serializer for assignment.models.Assignment objects. With all polls.
|
|
|
|
"""
|
|
|
|
assignmentcandidate_set = AssignmentCandidateSerializer(many=True, read_only=True)
|
|
|
|
poll_set = AssignmentAllPollSerializer(many=True, read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Assignment
|
|
|
|
fields = (
|
2015-02-04 00:08:38 +01:00
|
|
|
'id',
|
2015-01-17 14:25:05 +01:00
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'posts',
|
|
|
|
'status',
|
|
|
|
'assignmentcandidate_set',
|
2015-01-24 16:35:50 +01:00
|
|
|
'poll_description_default',
|
2015-01-17 14:25:05 +01:00
|
|
|
'poll_set',
|
2015-01-24 16:35:50 +01:00
|
|
|
'tags',)
|
2015-01-17 14:25:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AssignmentShortSerializer(AssignmentFullSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for assignment.models.Assignment objects. Without unpublished poll.
|
|
|
|
"""
|
|
|
|
poll_set = AssignmentShortPollSerializer(many=True, read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Assignment
|
|
|
|
fields = (
|
2015-02-04 00:08:38 +01:00
|
|
|
'id',
|
2015-01-17 14:25:05 +01:00
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'posts',
|
|
|
|
'status',
|
|
|
|
'assignmentcandidate_set',
|
2015-01-24 16:35:50 +01:00
|
|
|
'poll_description_default',
|
2015-01-17 14:25:05 +01:00
|
|
|
'poll_set',
|
2015-01-24 16:35:50 +01:00
|
|
|
'tags',)
|