From 68b9a2e2917197a908cec3e40d0913345a82a7a5 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Thu, 17 Oct 2013 08:29:54 +0200 Subject: [PATCH] Cache the results of ItemDelete.get_answer_options Fix #911 --- openslides/agenda/views.py | 21 +++++++++++++++++---- tests/agenda/tests.py | 8 ++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/openslides/agenda/views.py b/openslides/agenda/views.py index 5f55f9499..dcdf847fa 100644 --- a/openslides/agenda/views.py +++ b/openslides/agenda/views.py @@ -246,12 +246,25 @@ class ItemDelete(DeleteView): url_name_args = [] def get_answer_options(self): - if self.object.children.exists(): - return [('all', _("Yes, with all child items."))] + self.answer_options - else: - return self.answer_options + """ + Returns the possible answers to the delete view. + + 'all' is a possible answer, when the item has children. + """ + # Cache the result in the request, so when the children are deleted, the + # result does not change + try: + options = self.item_delete_answer_options + except AttributeError: + if self.object.children.exists(): + options = [('all', _("Yes, with all child items."))] + self.answer_options + else: + options = self.answer_options + self.item_delete_answer_options = options + return options def pre_post_redirect(self, request, *args, **kwargs): + # TODO: rewrite this method with on_case_all and on_case_yes if self.get_answer() == 'all': self.object.delete(with_children=True) messages.success( diff --git a/tests/agenda/tests.py b/tests/agenda/tests.py index a3e849759..4b5c9fd04 100644 --- a/tests/agenda/tests.py +++ b/tests/agenda/tests.py @@ -214,6 +214,14 @@ class ViewTest(TestCase): self.assertRedirects(response, '/agenda/') self.assertFalse(Item.objects.filter(pk=1).exists()) + def test_delete_item_with_children(self): + item1 = Item.objects.create(title='item1') + item2 = Item.objects.create(title='item2', parent=item1) + + self.adminClient.post('/agenda/%d/del/' % item1.pk, {'all': 'all'}) + query = Item.objects.filter(pk__in=[item1.pk, item2.pk]) + self.assertFalse(query) + class ConfigTest(TestCase): def setUp(self):