Merge pull request #913 from ostcar/issue-911

Cache the results of ItemDelete.get_answer_options
This commit is contained in:
Oskar Hahn 2013-10-17 23:00:50 -07:00
commit 2b04980fc7
2 changed files with 25 additions and 4 deletions

View File

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

View File

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