Code

Ticket #12281: admin-action-empty-queryset-12281-patch.diff

File admin-action-empty-queryset-12281-patch.diff, 3.0 KB (added by blinkylights, 4 years ago)

Added tests

Line 
1Index: django/contrib/admin/options.py
2===================================================================
3--- django/contrib/admin/options.py     (revision 11833)
4+++ django/contrib/admin/options.py     (working copy)
5@@ -692,6 +692,9 @@
6             # perform an action on it, so bail.
7             selected = request.POST.getlist(helpers.ACTION_CHECKBOX_NAME)
8             if not selected:
9+                # Reminder that something needs to be selected or nothing will happen
10+                msg = "Items must be selected in order to perform actions on them. No items have been changed."
11+                self.message_user(request, _(msg))
12                 return None
13 
14             response = func(self, request, queryset.filter(pk__in=selected))
15@@ -703,6 +706,9 @@
16                 return response
17             else:
18                 return HttpResponseRedirect(".")
19+        else:
20+            msg = "No action selected."
21+            self.message_user(request, _(msg))
22 
23     @csrf_protect
24     @transaction.commit_on_success
25Index: tests/regressiontests/admin_views/tests.py
26===================================================================
27--- tests/regressiontests/admin_views/tests.py  (revision 11833)
28+++ tests/regressiontests/admin_views/tests.py  (working copy)
29@@ -1175,6 +1175,38 @@
30         self.assertEquals(len(mail.outbox), 1)
31         self.assertEquals(mail.outbox[0].subject, 'Greetings from a function action')
32 
33+    def test_user_message_on_none_selected(self):
34+        """
35+        User should see a warning when 'Go' is pressed and no items are selected.
36+        """
37+        action_data = {
38+            ACTION_CHECKBOX_NAME: [],
39+            'action' : 'delete_selected',
40+            'index': 0,
41+        }
42+        response = self.client.post('/test_admin/admin/admin_views/subscriber/', action_data)
43+        msg = """Items must be selected in order to perform actions on them. No items have been changed."""
44+        self.assertContains(response, msg)
45+        self.failUnlessEqual(Subscriber.objects.count(), 2)
46+
47+    def test_user_message_on_no_action(self):
48+        """
49+        User should see a warning when 'Go' is pressed and no action is selected.
50+        """
51+        action_data = {
52+            ACTION_CHECKBOX_NAME: [1, 2],
53+            'action' : '',
54+            'index': 0,
55+        }
56+        response = self.client.post('/test_admin/admin/admin_views/subscriber/', action_data)
57+        msg = """No action selected."""
58+        self.assertContains(response, msg)
59+        self.failUnlessEqual(Subscriber.objects.count(), 2)
60+
61+
62+
63+
64+
65 class TestInlineNotEditable(TestCase):
66     fixtures = ['admin-views-users.xml']
67 
68Index: AUTHORS
69===================================================================
70--- AUTHORS     (revision 11833)
71+++ AUTHORS     (working copy)
72@@ -397,6 +397,7 @@
73     Jozko Skrablin <jozko.skrablin@gmail.com>
74     Ben Slavin <benjamin.slavin@gmail.com>
75     sloonz <simon.lipp@insa-lyon.fr>
76+    Paul Smith <blinkylights23@gmail.com>
77     Warren Smith <warren@wandrsmith.net>
78     smurf@smurf.noris.de
79     Vsevolod Solovyov