From bc49f00c665b9913f502127d9d4d1ce2bb7066a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Sat, 27 Feb 2016 20:49:28 +0100 Subject: [PATCH] Fixed error in motion poll serializer. Fixed #2014. --- openslides/motions/serializers.py | 11 ++++++++--- tests/integration/motions/test_viewset.py | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/openslides/motions/serializers.py b/openslides/motions/serializers.py index 6808d9247..8932dda2c 100644 --- a/openslides/motions/serializers.py +++ b/openslides/motions/serializers.py @@ -118,6 +118,11 @@ class MotionPollSerializer(ModelSerializer): 'has_votes') 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): try: result = self.get_votes_dict(obj)['Yes'] @@ -141,9 +146,9 @@ class MotionPollSerializer(ModelSerializer): def get_votes_dict(self, obj): try: - votes_dict = self._votes_dict - except AttributeError: - votes_dict = self._votes_dict = {} + votes_dict = self._votes_dicts[obj.pk] + except KeyError: + votes_dict = self._votes_dicts[obj.pk] = {} for vote in obj.get_votes(): votes_dict[vote.value] = vote.weight return votes_dict diff --git a/tests/integration/motions/test_viewset.py b/tests/integration/motions/test_viewset.py index f91b193da..32399ca7c 100644 --- a/tests/integration/motions/test_viewset.py +++ b/tests/integration/motions/test_viewset.py @@ -380,6 +380,29 @@ class SetState(TestCase): 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): """ Tests updating polls of motions.