From 31af5c0ce6ad98d8515f5062fd183d02cc441612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Tue, 19 Nov 2013 01:00:27 +0100 Subject: [PATCH] Add check for set state view, fix #1080. --- openslides/motion/views.py | 22 +++++++++++++--------- tests/motion/test_views.py | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/openslides/motion/views.py b/openslides/motion/views.py index 7c27d5b91..bfa2a75a1 100644 --- a/openslides/motion/views.py +++ b/openslides/motion/views.py @@ -25,7 +25,7 @@ from .forms import (BaseMotionForm, MotionCategoryMixin, MotionImportForm, MotionSubmitterMixin, MotionSupporterMixin, MotionWorkflowMixin) from .models import (Category, Motion, MotionPoll, MotionSubmitter, - MotionSupporter, MotionVersion, WorkflowError) + MotionSupporter, MotionVersion, State) from .pdf import motion_poll_to_pdf, motion_to_pdf, motions_to_pdf @@ -647,17 +647,21 @@ class MotionSetStateView(SingleObjectMixin, RedirectView): Save the new state and write a log message. """ self.object = self.get_object() - try: - if self.reset: - self.object.reset_state() - else: - self.object.set_state(int(kwargs['state'])) - except WorkflowError, e: # TODO: Is a WorkflowError still possible here? - messages.error(request, e) + success = False + if self.reset: + self.object.reset_state() + success = True + elif self.object.state.id == int(kwargs['state']): + messages.error(request, _('You can not set the state of the motion. It is already done.')) + elif int(kwargs['state']) not in [state.id for state in self.object.state.next_states.all()]: + messages.error(request, _('You can not set the state of the motion to %s.') % _(State.objects.get(pk=int(kwargs['state'])).name)) else: + self.object.set_state(int(kwargs['state'])) + success = True + if success: self.object.save(update_fields=['state', 'identifier']) self.object.write_log( - message_list=[ugettext_noop('State changed to'), ' %s' % self.object.state.name], + message_list=[ugettext_noop('State changed to'), ' %s' % self.object.state.name], # TODO: Change string to 'State set to ...' person=self.request.user) messages.success(request, _('The state of the motion was set to %s.') diff --git a/tests/motion/test_views.py b/tests/motion/test_views.py index f31295960..844514bd5 100644 --- a/tests/motion/test_views.py +++ b/tests/motion/test_views.py @@ -459,6 +459,21 @@ class TestVersionDeleteView(MotionViewTestCase): self.assertEqual(response.status_code, 404) +class MotionSetStatusView(MotionViewTestCase): + def test_set_status(self): + self.assertEqual(self.motion1.state, State.objects.get(name='submitted')) + self.check_url('/motion/1/set_state/4/', self.registered_client, 403) + response = self.staff_client.get('/motion/1/set_state/4/') + self.assertRedirects(response, '/motion/1/') + self.assertEqual(Motion.objects.get(pk=1).state, State.objects.get(name='not decided')) + response = self.staff_client.get('/motion/1/set_state/1/') + self.assertTrue('You can not set the state of the motion to submitted.' in response.cookies['messages'].value) + self.assertRedirects(response, '/motion/1/') + response = self.staff_client.get('/motion/1/set_state/4/') + self.assertTrue('You can not set the state of the motion. It is already done.' in response.cookies['messages'].value) + self.assertRedirects(response, '/motion/1/') + + class CategoryViewsTest(TestCase): def setUp(self): self.admin_client = Client()