Ticket #13166: modeladmin-13166.diff
File modeladmin-13166.diff, 8.0 KB (added by , 15 years ago) |
---|
-
django/contrib/admin/options.py
732 732 733 733 # Get the list of selected PKs. If nothing's selected, we can't 734 734 # perform an action on it, so bail. Except we want to perform 735 # the action explicit ely on all objects.735 # the action explicitly on all objects. 736 736 selected = request.POST.getlist(helpers.ACTION_CHECKBOX_NAME) 737 737 if not selected and not select_across: 738 738 # Reminder that something needs to be selected or nothing will happen … … 756 756 else: 757 757 msg = _("No action selected.") 758 758 self.message_user(request, msg) 759 return None 759 760 760 761 @csrf_protect_m 761 762 @transaction.commit_on_success … … 974 975 return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1') 975 976 976 977 # If the request was POSTed, this might be a bulk action or a bulk edit. 977 # Try to look up an action or confirmation first, but if this isn't an 978 # action the POST will fall through to the bulk edit check, below. 979 if actions and request.method == 'POST' and (helpers.ACTION_CHECKBOX_NAME in request.POST or 'index' in request.POST): 980 response = self.response_action(request, queryset=cl.get_query_set()) 981 if response: 982 return response 978 # Try to look up an action or confirmation first, but if this isn't an action the POST 979 # will fall through to the bulk edit check, below. 983 980 981 action_failed = False 982 983 # Actions with no confirmation 984 if actions and request.method == 'POST' and 'index' in request.POST and '_save' not in request.POST: 985 if len(request.POST.get(helpers.ACTION_CHECKBOX_NAME, [])): 986 response = self.response_action(request, queryset=cl.get_query_set()) 987 if response: 988 return response 989 else: 990 action_failed = True 991 else: 992 msg = _("Items must be selected in order to perform actions on them. No items have been changed.") 993 self.message_user(request, msg) 994 action_failed = True 995 996 # Actions with confirmation 997 if actions and request.method == 'POST' and helpers.ACTION_CHECKBOX_NAME in request.POST and 'index' not in request.POST and '_save' not in request.POST: 998 if len(request.POST.get(helpers.ACTION_CHECKBOX_NAME, [])): 999 response = self.response_action(request, queryset=cl.get_query_set()) 1000 if response: 1001 return response 1002 else: 1003 action_failed = True 1004 1005 984 1006 # If we're allowing changelist editing, we need to construct a formset 985 1007 # for the changelist given all the fields to be edited. Then we'll 986 1008 # use the formset to validate/process POSTed data. 987 1009 formset = cl.formset = None 988 1010 989 1011 # Handle POSTed bulk-edit data. 990 if request.method == "POST" and self.list_editable :1012 if request.method == "POST" and self.list_editable and '_save' in request.POST and not action_failed: 991 1013 FormSet = self.get_changelist_formset(request) 992 1014 formset = cl.formset = FormSet(request.POST, request.FILES, queryset=cl.result_list) 993 1015 if formset.is_valid(): -
tests/regressiontests/admin_views/tests.py
1085 1085 "form-2-alive": "checked", 1086 1086 "form-2-gender": "1", 1087 1087 "form-2-id": "3", 1088 1089 "_save": "Save", 1088 1090 } 1089 1091 response = self.client.post('/test_admin/admin/admin_views/person/', 1090 1092 data, follow=True) … … 1105 1107 "form-2-alive": "checked", 1106 1108 "form-2-gender": "1", 1107 1109 "form-2-id": "3", 1110 1111 "_save": "Save", 1108 1112 } 1109 1113 self.client.post('/test_admin/admin/admin_views/person/', data) 1110 1114 … … 1124 1128 "form-1-id": "3", 1125 1129 "form-1-gender": "1", 1126 1130 "form-1-alive": "checked", 1131 1132 "_save": "Save", 1127 1133 } 1128 1134 self.client.post('/test_admin/admin/admin_views/person/?gender__exact=1', data) 1129 1135 … … 1136 1142 "form-MAX_NUM_FORMS": "0", 1137 1143 1138 1144 "form-0-id": "1", 1139 "form-0-gender": "1" 1145 "form-0-gender": "1", 1146 1147 "_save": "Save", 1140 1148 } 1141 1149 self.client.post('/test_admin/admin/admin_views/person/?q=mauchly', data) 1142 1150 … … 1152 1160 "form-0-id": "2", 1153 1161 "form-0-alive": "1", 1154 1162 "form-0-gender": "2", 1163 1164 # Ensure that the form processing understands this as a list_editable "Save" 1165 # and not an action "Go". 1166 "_save": "Save", 1155 1167 } 1156 1168 response = self.client.post('/test_admin/admin/admin_views/person/', data) 1157 1169 self.assertContains(response, "Grace is not a Zombie") … … 1166 1178 "form-0-id": "2", 1167 1179 "form-0-alive": "1", 1168 1180 "form-0-gender": "2", 1181 1182 "_save": "Save", 1169 1183 } 1170 1184 response = self.client.post('/test_admin/admin/admin_views/person/', data) 1171 1185 non_form_errors = response.context['cl'].formset.non_form_errors() … … 1201 1215 "form-3-order": "0", 1202 1216 "form-3-id": "4", 1203 1217 "form-3-collector": "1", 1218 1219 # Ensure that the form processing understands this as a list_editable "Save" 1220 # and not an action "Go". 1221 "_save": "Save", 1204 1222 } 1205 1223 response = self.client.post('/test_admin/admin/admin_views/category/', data) 1206 1224 # Successful post will redirect … … 1212 1230 self.failUnlessEqual(Category.objects.get(id=3).order, 1) 1213 1231 self.failUnlessEqual(Category.objects.get(id=4).order, 0) 1214 1232 1233 def test_list_editable_action_submit(self): 1234 # List editable changes should not be executed if the action "Go" button is 1235 # used to submit the form. 1236 data = { 1237 "form-TOTAL_FORMS": "3", 1238 "form-INITIAL_FORMS": "3", 1239 "form-MAX_NUM_FORMS": "0", 1240 1241 "form-0-gender": "1", 1242 "form-0-id": "1", 1243 1244 "form-1-gender": "2", 1245 "form-1-id": "2", 1246 1247 "form-2-alive": "checked", 1248 "form-2-gender": "1", 1249 "form-2-id": "3", 1250 1251 "index": "0", 1252 "_selected_action": [u'3'], 1253 "action": [u'', u'delete_selected'], 1254 } 1255 self.client.post('/test_admin/admin/admin_views/person/', data) 1256 1257 self.failUnlessEqual(Person.objects.get(name="John Mauchly").alive, True) 1258 self.failUnlessEqual(Person.objects.get(name="Grace Hopper").gender, 1) 1259 1260 def test_list_editable_action_choices(self): 1261 # List editable changes should be executed if the "Save" button is 1262 # used to submit the form - any action choices should be ignored. 1263 data = { 1264 "form-TOTAL_FORMS": "3", 1265 "form-INITIAL_FORMS": "3", 1266 "form-MAX_NUM_FORMS": "0", 1267 1268 "form-0-gender": "1", 1269 "form-0-id": "1", 1270 1271 "form-1-gender": "2", 1272 "form-1-id": "2", 1273 1274 "form-2-alive": "checked", 1275 "form-2-gender": "1", 1276 "form-2-id": "3", 1277 1278 "_save": "Save", 1279 "_selected_action": [u'1'], 1280 "action": [u'', u'delete_selected'], 1281 } 1282 self.client.post('/test_admin/admin/admin_views/person/', data) 1283 1284 self.failUnlessEqual(Person.objects.get(name="John Mauchly").alive, False) 1285 self.failUnlessEqual(Person.objects.get(name="Grace Hopper").gender, 2) 1286 1287 1288 1289 1215 1290 class AdminSearchTest(TestCase): 1216 1291 fixtures = ['admin-views-users','multiple-child-classes'] 1217 1292