Merge pull request #1651 from normanjaeckel/MotionPoll

Changed Motion Poll REST fields.
This commit is contained in:
Oskar Hahn 2015-10-21 21:25:06 +02:00
commit bf3e125786
2 changed files with 18 additions and 28 deletions

View File

@ -8,6 +8,7 @@ from openslides.utils.rest_api import (
IntegerField, IntegerField,
ModelSerializer, ModelSerializer,
PrimaryKeyRelatedField, PrimaryKeyRelatedField,
SerializerMethodField,
ValidationError, ValidationError,
) )
@ -15,10 +16,8 @@ from .models import (
Category, Category,
Motion, Motion,
MotionLog, MotionLog,
MotionOption,
MotionPoll, MotionPoll,
MotionVersion, MotionVersion,
MotionVote,
State, State,
Workflow, Workflow,
) )
@ -83,31 +82,13 @@ class MotionLogSerializer(ModelSerializer):
fields = ('message_list', 'person', 'time',) 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): class MotionPollSerializer(ModelSerializer):
""" """
Serializer for motion.models.MotionPoll objects. Serializer for motion.models.MotionPoll objects.
""" """
motionoption_set = MotionOptionSerializer(many=True, read_only=True) yes = SerializerMethodField()
no = SerializerMethodField()
abstain = SerializerMethodField()
votes = DictField( votes = DictField(
child=IntegerField(min_value=-2), child=IntegerField(min_value=-2),
write_only=True) write_only=True)
@ -116,13 +97,22 @@ class MotionPollSerializer(ModelSerializer):
model = MotionPoll model = MotionPoll
fields = ( fields = (
'id', 'id',
'poll_number', 'yes',
'motionoption_set', 'no',
'abstain',
'votesvalid', 'votesvalid',
'votesinvalid', 'votesinvalid',
'votescast', 'votescast',
'votes',) '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 @transaction.atomic
def update(self, instance, validated_data): def update(self, instance, validated_data):

View File

@ -122,9 +122,9 @@ class CollectDefaultVotesMixin(models.Model):
return value return value
def get_percent_base(self): 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) 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) base = 100 / float(self.votescast)
else: else:
base = None base = None