Returns a 404 response if a requested poll does not exist.

fixes #1355
This commit is contained in:
Oskar Hahn 2014-10-16 18:04:30 +02:00
parent 9b381b712e
commit cb03908a1b
4 changed files with 44 additions and 3 deletions

View File

@ -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):
"""

View File

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

View File

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

View File

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