Changed Motion Poll REST fields.

This commit is contained in:
Norman Jäckel 2015-10-21 21:13:45 +02:00
parent 719330e6f6
commit 817e15b01a
2 changed files with 18 additions and 28 deletions

View File

@ -8,6 +8,7 @@ from openslides.utils.rest_api import (
IntegerField,
ModelSerializer,
PrimaryKeyRelatedField,
SerializerMethodField,
ValidationError,
)
@ -15,10 +16,8 @@ from .models import (
Category,
Motion,
MotionLog,
MotionOption,
MotionPoll,
MotionVersion,
MotionVote,
State,
Workflow,
)
@ -83,31 +82,13 @@ class MotionLogSerializer(ModelSerializer):
fields = ('message_list', 'person', 'time',)
class MotionVoteSerializer(ModelSerializer):
"""
Serializer for motion.models.MotionVote objects.
"""
class Meta:
model = MotionVote
fields = ('value', 'weight',)
class MotionOptionSerializer(ModelSerializer):
"""
Serializer for motion.models.MotionOption objects.
"""
motionvote_set = MotionVoteSerializer(many=True, read_only=True)
class Meta:
model = MotionOption
fields = ('motionvote_set',)
class MotionPollSerializer(ModelSerializer):
"""
Serializer for motion.models.MotionPoll objects.
"""
motionoption_set = MotionOptionSerializer(many=True, read_only=True)
yes = SerializerMethodField()
no = SerializerMethodField()
abstain = SerializerMethodField()
votes = DictField(
child=IntegerField(min_value=-2),
write_only=True)
@ -116,13 +97,22 @@ class MotionPollSerializer(ModelSerializer):
model = MotionPoll
fields = (
'id',
'poll_number',
'motionoption_set',
'yes',
'no',
'abstain',
'votesvalid',
'votesinvalid',
'votescast',
'votes',)
read_only_fields = ('poll_number',)
def get_yes(self, obj):
return obj.get_votes().get(value='Yes').weight
def get_no(self, obj):
return obj.get_votes().get(value='No').weight
def get_abstain(self, obj):
return obj.get_votes().get(value='Abstain').weight
@transaction.atomic
def update(self, instance, validated_data):

View File

@ -122,9 +122,9 @@ class CollectDefaultVotesMixin(models.Model):
return value
def get_percent_base(self):
if self.get_percent_base_choice() == "WITHOUT_INVALID" and self.votesvalid > 0:
if self.get_percent_base_choice() == "WITHOUT_INVALID" and self.votesvalid and self.votesvalid > 0:
base = 100 / float(self.votesvalid)
elif self.get_percent_base_choice() == "WITH_INVALID" and self.votescast > 0:
elif self.get_percent_base_choice() == "WITH_INVALID" and self.votescast and self.votescast > 0:
base = 100 / float(self.votescast)
else:
base = None