From cb03908a1bab83e8a4bc8b6361aa936ac2c23576 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Thu, 16 Oct 2014 18:04:30 +0200 Subject: [PATCH] Returns a 404 response if a requested poll does not exist. fixes #1355 --- openslides/motion/views.py | 6 ++++-- openslides/poll/views.py | 7 ++++++- tests/assignment/test_views.py | 17 +++++++++++++++++ tests/motion/test_views.py | 17 +++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/openslides/motion/views.py b/openslides/motion/views.py index 829421665..97da9976d 100644 --- a/openslides/motion/views.py +++ b/openslides/motion/views.py @@ -7,6 +7,7 @@ from django.utils.text import slugify from django.utils.translation import ugettext as _ from django.utils.translation import ugettext_lazy, ugettext_noop from reportlab.platypus import SimpleDocTemplate +from django.shortcuts import get_object_or_404 from openslides.agenda.views import CreateRelatedAgendaItemView as _CreateRelatedAgendaItemView from openslides.config.api import config @@ -524,9 +525,10 @@ class PollMixin(object): Use the motion id and the poll_number from the url kwargs to get the object. """ - return MotionPoll.objects.filter( + queryset = MotionPoll.objects.filter( motion=self.kwargs['pk'], - poll_number=self.kwargs['poll_number']).get() + poll_number=self.kwargs['poll_number']) + return get_object_or_404(queryset) def get_url_name_args(self): """ diff --git a/openslides/poll/views.py b/openslides/poll/views.py index f16806734..41ee176d5 100644 --- a/openslides/poll/views.py +++ b/openslides/poll/views.py @@ -2,6 +2,7 @@ from django.forms.models import modelform_factory from django.http import HttpResponseRedirect +from django.shortcuts import get_object_or_404 from openslides.utils.views import TemplateView, FormMixin @@ -53,7 +54,11 @@ class PollFormView(FormMixin, TemplateView): 'a get_poll_class method.') def get_object(self): - return self.get_poll_class().objects.get(pk=self.kwargs['poll_id']) + """ + Returns the poll object. Raises Http404 if the poll does not exist. + """ + queryset = self.get_poll_class().objects.filter(pk=self.kwargs['poll_id']) + return get_object_or_404(queryset) def get_context_data(self, **kwargs): context = super(PollFormView, self).get_context_data(**kwargs) diff --git a/tests/assignment/test_views.py b/tests/assignment/test_views.py index 2b2358b44..4717be190 100644 --- a/tests/assignment/test_views.py +++ b/tests/assignment/test_views.py @@ -109,3 +109,20 @@ class TestAssignmentPollCreateView(TestCase): self.assertEqual(poll.assignment, self.assignment) self.assertEqual(poll.assignmentoption_set.count(), 1) self.assertTrue(poll.yesnoabstain) + + +class TestPollUpdateView(TestCase): + def setUp(self): + self.admin_client = Client() + self.admin_client.login(username='admin', password='admin') + + def test_not_existing_poll(self): + """ + Tests that a 404 is returned, when a non existing poll is requested. + """ + Assignment.objects.create(name='test assignment', posts=1) + url = '/assignment/poll/1/edit/' + + response = self.admin_client.get(url) + + self.assertTrue(response.status_code, '404') diff --git a/tests/motion/test_views.py b/tests/motion/test_views.py index 8b47f0310..96e1c819b 100644 --- a/tests/motion/test_views.py +++ b/tests/motion/test_views.py @@ -528,3 +528,20 @@ class CategoryViewsTest(TestCase): response = self.admin_client.post(url, {'yes': 'true'}) self.assertRedirects(response, '/motion/category/') self.assertFalse(Category.objects.exists()) + + +class PollUpdateViewTest(TestCase): + def setUp(self): + self.admin_client = Client() + self.admin_client.login(username='admin', password='admin') + + def test_not_existing_poll(self): + """ + Tests that a 404 is returned, when a non existing poll is requested + """ + Motion.objects.create(title='test_motion') + url = '/motion/1/poll/1/edit/' + + response = self.admin_client.get(url) + + self.assertTrue(response.status_code, '404')