Cache the results of ItemDelete.get_answer_options

Fix #911
This commit is contained in:
Oskar Hahn 2013-10-17 08:29:54 +02:00
parent d3f673a276
commit 68b9a2e291
2 changed files with 25 additions and 4 deletions

View File

@ -246,12 +246,25 @@ class ItemDelete(DeleteView):
url_name_args = [] url_name_args = []
def get_answer_options(self): def get_answer_options(self):
"""
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(): if self.object.children.exists():
return [('all', _("Yes, with all child items."))] + self.answer_options options = [('all', _("Yes, with all child items."))] + self.answer_options
else: else:
return self.answer_options options = self.answer_options
self.item_delete_answer_options = options
return options
def pre_post_redirect(self, request, *args, **kwargs): 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': if self.get_answer() == 'all':
self.object.delete(with_children=True) self.object.delete(with_children=True)
messages.success( messages.success(

View File

@ -214,6 +214,14 @@ class ViewTest(TestCase):
self.assertRedirects(response, '/agenda/') self.assertRedirects(response, '/agenda/')
self.assertFalse(Item.objects.filter(pk=1).exists()) 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): class ConfigTest(TestCase):
def setUp(self): def setUp(self):