Merge pull request #1082 from normanjaeckel/SetStateView

Add check for set state view, fix #1080.
This commit is contained in:
Oskar Hahn 2013-11-19 08:13:10 -08:00
commit 3d19ae7db2
2 changed files with 28 additions and 9 deletions

View File

@ -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.')

View File

@ -470,6 +470,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()