Ticket #6903: 6903.3.diff
File 6903.3.diff, 10.6 KB (added by , 12 years ago) |
---|
-
django/contrib/admin/options.py
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index f7bfca4..11aff87 100644
a b from django.contrib.admin.util import (unquote, flatten_fieldsets, get_deleted_o 12 12 model_format_dict, NestedObjects) 13 13 from django.contrib.admin.templatetags.admin_static import static 14 14 from django.contrib import messages 15 from django.utils.http import is_safe_url 15 16 from django.views.decorators.csrf import csrf_protect 16 17 from django.core.exceptions import PermissionDenied, ValidationError 17 18 from django.core.paginator import Paginator … … from django.utils.translation import ugettext as _ 35 36 from django.utils.translation import ungettext 36 37 from django.utils.encoding import force_text 37 38 39 try: 40 from urllib import parse as urllib_parse 41 except ImportError: # Python 2 42 import urllib as urllib_parse 43 import urlparse 44 urllib_parse.urlparse = urlparse.urlparse 45 46 38 47 HORIZONTAL, VERTICAL = 1, 2 39 48 # returns the <ul> class for a given radio_admin field 40 49 get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '') … … class ModelAdmin(BaseModelAdmin): 909 918 when editing an existing object. 910 919 """ 911 920 opts = self.model._meta 921 changelist_url = reverse('admin:%s_%s_changelist' % 922 (opts.app_label, opts.model_name), 923 current_app=self.admin_site.name) 924 925 changelist_filters = request.POST.get('_changelist_filters') 926 if changelist_filters: 927 return HttpResponseRedirect( 928 '%s?%s' % (changelist_url, changelist_filters)) 929 912 930 if self.has_change_permission(request, None): 913 post_url = reverse('admin:%s_%s_changelist' % 914 (opts.app_label, opts.model_name), 915 current_app=self.admin_site.name) 931 post_url = changelist_url 916 932 else: 917 933 post_url = reverse('admin:index', 918 934 current_app=self.admin_site.name) … … class ModelAdmin(BaseModelAdmin): 1095 1111 ModelForm = self.get_form(request, obj) 1096 1112 formsets = [] 1097 1113 inline_instances = self.get_inline_instances(request, obj) 1114 changelist_filters = None 1098 1115 if request.method == 'POST': 1099 1116 form = ModelForm(request.POST, request.FILES, instance=obj) 1100 1117 if form.is_valid(): … … class ModelAdmin(BaseModelAdmin): 1134 1151 queryset=inline.get_queryset(request)) 1135 1152 formsets.append(formset) 1136 1153 1154 referer = request.META.get('HTTP_REFERER') 1155 if referer: 1156 referer = urllib_parse.urlparse(referer) 1157 changelist_url = reverse('admin:%s_%s_changelist' % (opts.app_label, opts.model_name)) 1158 if is_safe_url(url=referer.geturl(), host=request.get_host()) and referer.path.startswith(changelist_url): 1159 changelist_filters = referer.query 1160 1137 1161 adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj), 1138 1162 self.get_prepopulated_fields(request, obj), 1139 1163 self.get_readonly_fields(request, obj), … … class ModelAdmin(BaseModelAdmin): 1160 1184 'inline_admin_formsets': inline_admin_formsets, 1161 1185 'errors': helpers.AdminErrorList(form, formsets), 1162 1186 'app_label': opts.app_label, 1187 'changelist_filters': changelist_filters, 1163 1188 } 1164 1189 context.update(extra_context or {}) 1165 1190 return self.render_change_form(request, context, change=True, obj=obj, form_url=form_url) … … class ModelAdmin(BaseModelAdmin): 1348 1373 1349 1374 self.message_user(request, _('The %(name)s "%(obj)s" was deleted successfully.') % {'name': force_text(opts.verbose_name), 'obj': force_text(obj_display)}) 1350 1375 1376 changelist_url = reverse('admin:%s_%s_changelist' % 1377 (opts.app_label, opts.model_name), 1378 current_app=self.admin_site.name) 1379 1380 changelist_filters = request.POST.get('_changelist_filters') 1381 if changelist_filters: 1382 return HttpResponseRedirect('%s?%s' % (changelist_url, changelist_filters)) 1383 1351 1384 if not self.has_change_permission(request, None): 1352 1385 return HttpResponseRedirect(reverse('admin:index', 1353 1386 current_app=self.admin_site.name)) 1354 return HttpResponseRedirect( reverse('admin:%s_%s_changelist' %1355 (opts.app_label, opts.model_name),1356 current_app=self.admin_site.name))1387 return HttpResponseRedirect(changelist_url) 1388 else: 1389 changelist_filters = request.GET.get('_changelist_filters') 1357 1390 1358 1391 object_name = force_text(opts.verbose_name) 1359 1392 … … class ModelAdmin(BaseModelAdmin): 1371 1404 "protected": protected, 1372 1405 "opts": opts, 1373 1406 "app_label": app_label, 1407 "changelist_filters": changelist_filters, 1374 1408 } 1375 1409 context.update(extra_context or {}) 1376 1410 -
django/contrib/admin/templates/admin/change_form.html
diff --git a/django/contrib/admin/templates/admin/change_form.html b/django/contrib/admin/templates/admin/change_form.html index daf3775..0b2312e 100644
a b 38 38 <form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form">{% csrf_token %}{% block form_top %}{% endblock %} 39 39 <div> 40 40 {% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %} 41 {% if changelist_filters %}<input type="hidden" name="_changelist_filters" value="{{ changelist_filters }}" />{% endif %} 41 42 {% if save_on_top %}{% block submit_buttons_top %}{% submit_row %}{% endblock %}{% endif %} 42 43 {% if errors %} 43 44 <p class="errornote"> -
django/contrib/admin/templates/admin/delete_confirmation.html
diff --git a/django/contrib/admin/templates/admin/delete_confirmation.html b/django/contrib/admin/templates/admin/delete_confirmation.html index c1a7115..065fa14 100644
a b 36 36 <form action="" method="post">{% csrf_token %} 37 37 <div> 38 38 <input type="hidden" name="post" value="yes" /> 39 {% if changelist_filters %}<input type="hidden" name="_changelist_filters" value="{{ changelist_filters }}" />{% endif %} 39 40 <input type="submit" value="{% trans "Yes, I'm sure" %}" /> 40 41 </div> 41 42 </form> -
tests/admin_views/tests.py
diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index bb77932..9199a5e 100644
a b class AdminUserMessageTest(TestCase): 4024 4024 self.assertContains(response, 4025 4025 '<li class="extra_tag info">Test tags</li>', 4026 4026 html=True) 4027 4028 4029 @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',)) 4030 class AdminChangeListRedirectionTests(TestCase): 4031 urls = "admin_views.urls" 4032 fixtures = ['admin-views-users'] 4033 4034 def setUp(self): 4035 Person.objects.create(name="Chris", gender=1, alive=True, age=25) 4036 Person.objects.create(name="Jack", gender=2, alive=False, age=47) 4037 Person.objects.create(name="Bob", gender=1, alive=True, age=36) 4038 4039 self.client.login(username='super', password='secret') 4040 4041 def get_data(self): 4042 return { 4043 'name': 'Joe', 'gender': '1', 'alive': '1', 'age': '58' 4044 } 4045 4046 def tearDown(self): 4047 self.client.logout() 4048 4049 def get_filters_querystring(self): 4050 return urlencode({ 4051 'alive': 1, 4052 }) 4053 4054 def get_filtered_changelist_url(self): 4055 return "%s%s?%s" % ('http://testserver', reverse('admin:admin_views_person_changelist'), self.get_filters_querystring()) 4056 4057 def test_change_redirect(self): 4058 """ 4059 Ensure that we're correctly redirected to the filtered changelist 4060 after an existing object is saved. 4061 Refs #6903. 4062 """ 4063 person = Person.objects.get(name='Chris') 4064 change_url = reverse('admin:admin_views_person_change', args=(person.pk,)) 4065 4066 # Simulate clicking an object from a filtered changelist 4067 response = self.client.get(change_url, HTTP_REFERER=self.get_filtered_changelist_url()) 4068 self.assertContains(response, '_changelist_filters') 4069 self.assertContains(response, self.get_filters_querystring()) 4070 4071 # Save the object 4072 data = self.get_data() 4073 data['_save'] = 1 4074 data['_changelist_filters'] = self.get_filters_querystring() 4075 response = self.client.post(change_url, data) 4076 4077 # Check that we return to the filtered changelist 4078 self.assertRedirects(response, self.get_filtered_changelist_url()) 4079 4080 def test_add_redirect(self): 4081 """ 4082 Ensure that we're correctly redirected to the non-filtered changelist 4083 after a new object is added. 4084 Refs #6903. 4085 """ 4086 add_url = reverse('admin:admin_views_person_add') 4087 4088 response = self.client.get(add_url, HTTP_REFERER=self.get_filtered_changelist_url()) 4089 self.assertNotContains(response, '_changelist_filters') 4090 self.assertNotContains(response, self.get_filters_querystring()) 4091 4092 # Add a new object 4093 data = self.get_data() 4094 data['_save'] = 1 4095 data['_changelist_filters'] = self.get_filters_querystring() 4096 response = self.client.post(add_url, data) 4097 4098 # Check that we return to the non-filtered changelist 4099 self.assertRedirects(response, reverse('admin:admin_views_person_changelist')) 4100 4101 def test_delete_redirect(self): 4102 """ 4103 Ensure that we're correctly redirected to the filtered changelist 4104 after an existing object is deleted. 4105 Refs #6903. 4106 """ 4107 person = Person.objects.get(name='Chris') 4108 delete_url = reverse('admin:admin_views_person_delete', args=(person.pk,)) 4109 4110 response = self.client.get(delete_url + '?_changelist_filters=%s' % self.get_filters_querystring()) 4111 self.assertContains(response, '_changelist_filters') 4112 self.assertContains(response, self.get_filters_querystring()) 4113 4114 # Delete the object 4115 data = {'post': 'yes', '_changelist_filters': self.get_filters_querystring()} 4116 response = self.client.post(delete_url, data) 4117 4118 # Check that we return to the filtered changelist 4119 self.assertRedirects(response, self.get_filtered_changelist_url()) 4120