Merge pull request #2017 from normanjaeckel/FixMotionPoll

Fixed error in motion poll serializer. Fixed #2014.
This commit is contained in:
Emanuel Schütze 2016-02-28 00:21:04 +01:00
commit 8d1de6923b
2 changed files with 31 additions and 3 deletions

View File

@ -118,6 +118,11 @@ class MotionPollSerializer(ModelSerializer):
'has_votes') 'has_votes')
validators = (default_votes_validator,) validators = (default_votes_validator,)
def __init__(self, *args, **kwargs):
# The following dictionary is just a cache for several votes.
self._votes_dicts = {}
return super().__init__(*args, **kwargs)
def get_yes(self, obj): def get_yes(self, obj):
try: try:
result = self.get_votes_dict(obj)['Yes'] result = self.get_votes_dict(obj)['Yes']
@ -141,9 +146,9 @@ class MotionPollSerializer(ModelSerializer):
def get_votes_dict(self, obj): def get_votes_dict(self, obj):
try: try:
votes_dict = self._votes_dict votes_dict = self._votes_dicts[obj.pk]
except AttributeError: except KeyError:
votes_dict = self._votes_dict = {} votes_dict = self._votes_dicts[obj.pk] = {}
for vote in obj.get_votes(): for vote in obj.get_votes():
votes_dict[vote.value] = vote.weight votes_dict[vote.value] = vote.weight
return votes_dict return votes_dict

View File

@ -380,6 +380,29 @@ class SetState(TestCase):
self.assertEqual(Motion.objects.get(pk=self.motion.pk).state.name, 'submitted') self.assertEqual(Motion.objects.get(pk=self.motion.pk).state.name, 'submitted')
class CreateMotionPoll(TestCase):
"""
Tests creating polls of motions.
"""
def setUp(self):
self.client = APIClient()
self.client.login(username='admin', password='admin')
self.motion = Motion(
title='test_title_Aiqueigh2dae9phabiqu',
text='test_text_Neekoh3zou6li5rue8iL')
self.motion.save()
def test_create_first_poll_with_values_then_second_poll_without(self):
self.poll = self.motion.create_poll()
self.poll.set_vote_objects_with_values(self.poll.get_options().get(), {'Yes': 42, 'No': 43, 'Abstain': 44})
response = self.client.post(
reverse('motion-create-poll', args=[self.motion.pk]))
self.assertEqual(self.motion.polls.count(), 2)
response = self.client.get(reverse('motion-detail', args=[self.motion.pk]))
for key in ('yes', 'no', 'abstain'):
self.assertTrue(response.data['polls'][1][key] is None, 'Vote value "{}" should be None.'.format(key))
class UpdateMotionPoll(TestCase): class UpdateMotionPoll(TestCase):
""" """
Tests updating polls of motions. Tests updating polls of motions.