From 30c3f5ea641a53229cd43fde8e609ecee4d648d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Thu, 25 Jul 2013 16:53:22 +0200 Subject: [PATCH] Disable deleting active versions of a motion --- .../templates/motion/motion_detail.html | 8 +++++--- openslides/motion/views.py | 19 ++++++++++++++----- tests/motion/test_models.py | 2 +- tests/motion/test_views.py | 8 ++++++++ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/openslides/motion/templates/motion/motion_detail.html b/openslides/motion/templates/motion/motion_detail.html index 170ba55ec..cb1de90d1 100644 --- a/openslides/motion/templates/motion/motion_detail.html +++ b/openslides/motion/templates/motion/motion_detail.html @@ -130,9 +130,11 @@ - - - + {% if version != motion.active_version %} + + + + {% endif %} {% if forloop.last %} diff --git a/openslides/motion/views.py b/openslides/motion/views.py index 8512681de..bbbcc31ab 100644 --- a/openslides/motion/views.py +++ b/openslides/motion/views.py @@ -303,10 +303,19 @@ class VersionDeleteView(DeleteView): success_url_name = 'motion_detail' def get_object(self): - motion_id = int(self.kwargs.get('pk')) - version_number = int(self.kwargs.get('version_number')) - return MotionVersion.objects.get(motion=motion_id, - version_number=version_number) + try: + motion = Motion.objects.get(pk=int(self.kwargs.get('pk'))) + except Motion.DoesNotExist: + raise Http404('Motion %s not found.' % self.kwargs.get('pk')) + try: + version = MotionVersion.objects.get( + motion=motion, + version_number=int(self.kwargs.get('version_number'))) + except MotionVersion.DoesNotExist: + raise Http404('Version %s not found.' % self.kwargs.get('version_number')) + if version == motion.active_version: + raise Http404('You can not delete the active version of a motion.') + return version def get_success_url_name_args(self): return (self.object.motion_id, ) @@ -333,7 +342,7 @@ class VersionPermitView(SingleObjectMixin, QuestionMixin, RedirectView): try: self.version = self.object.versions.get(version_number=int(version_number)) except MotionVersion.DoesNotExist: - raise Http404('Version %s not found' % version_number) + raise Http404('Version %s not found.' % version_number) return super(VersionPermitView, self).get(*args, **kwargs) def get_url_name_args(self): diff --git a/tests/motion/test_models.py b/tests/motion/test_models.py index 9db4cc796..943f9f481 100644 --- a/tests/motion/test_models.py +++ b/tests/motion/test_models.py @@ -150,7 +150,7 @@ class ModelTest(TestCase): motion = Motion.objects.create(title='foo', text='bar', identifier='') motion.active_version = None motion.save(update_fields=['active_version']) - self.assertEqual(str(motion), 'foo') # motion.__unicode__() did rais an AttributeError + self.assertEqual(str(motion), 'foo') # motion.__unicode__() raised an AttributeError class ConfigTest(TestCase): diff --git a/tests/motion/test_views.py b/tests/motion/test_views.py index e3be2e3c5..7726aef1c 100644 --- a/tests/motion/test_views.py +++ b/tests/motion/test_views.py @@ -412,6 +412,7 @@ class TestVersionPermitView(MotionViewTestCase): class TestVersionDeleteView(MotionViewTestCase): def test_get(self): + self.motion1.save(use_version=self.motion1.get_new_version(title='new', text='new')) response = self.check_url('/motion/1/version/1/del/', self.admin_client, 302) self.assertRedirects(response, '/motion/1/version/1/') @@ -424,3 +425,10 @@ class TestVersionDeleteView(MotionViewTestCase): response = self.admin_client.post('/motion/1/version/2/del/', {'yes': 1}) self.assertRedirects(response, '/motion/1/') self.assertEqual(self.motion1.versions.count(), 2) + + def test_delete_active_version(self): + self.motion1.save(use_version=self.motion1.get_new_version(title='new_title_yae6Aequaiw5saeb8suG', text='new')) + motion = Motion.objects.all()[0] + self.assertEqual(motion.get_active_version().title, 'new_title_yae6Aequaiw5saeb8suG') + response = self.admin_client.post('/motion/1/version/2/del/', {'yes': 1}) + self.assertEqual(response.status_code, 404)