Ticket #6903: 6903.2.diff

File 6903.2.diff, 7.3 KB (added by julien, 3 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index ff7873b..9de656c 100644
    a b class ModelAdmin(BaseModelAdmin): 
    884884            post_url = reverse('admin:%s_%s_changelist' %
    885885                               (opts.app_label, opts.model_name),
    886886                               current_app=self.admin_site.name)
     887            post_url += self.changelist_redirect_querystring(request)
    887888        else:
    888889            post_url = reverse('admin:index',
    889890                               current_app=self.admin_site.name)
    class ModelAdmin(BaseModelAdmin): 
    956957            self.message_user(request, msg)
    957958            return None
    958959
     960    def changelist_redirect_querystring(self, request):
     961        return request.session.get(self.changelist_redirect_session_key(request), '')
     962
     963    def changelist_redirect_session_key(self, request):
     964        opts = self.model._meta
     965        return 'admin_%s_%s_changelist_querystring' % (opts.app_label, opts.module_name)
     966
    959967    @csrf_protect_m
    960968    @transaction.commit_on_success
    961969    def add_view(self, request, form_url='', extra_context=None):
    class ModelAdmin(BaseModelAdmin): 
    12821290        }
    12831291        context.update(extra_context or {})
    12841292
     1293        if request.method == 'GET' and not cl.is_popup:
     1294            # Store the querystring in the session so we can return to the
     1295            # same filtered changelist later, for example after an object has
     1296            # been saved or deleted.
     1297            request.session[self.changelist_redirect_session_key(request)] = cl.get_query_string()
     1298
    12851299        return TemplateResponse(request, self.change_list_template or [
    12861300            'admin/%s/%s/change_list.html' % (app_label, opts.model_name),
    12871301            'admin/%s/change_list.html' % app_label,
    class ModelAdmin(BaseModelAdmin): 
    13221336            if not self.has_change_permission(request, None):
    13231337                return HttpResponseRedirect(reverse('admin:index',
    13241338                                                    current_app=self.admin_site.name))
    1325             return HttpResponseRedirect(reverse('admin:%s_%s_changelist' %
     1339            url = (reverse('admin:%s_%s_changelist' %
    13261340                                        (opts.app_label, opts.model_name),
    13271341                                        current_app=self.admin_site.name))
     1342            url += self.changelist_redirect_querystring(request)
     1343            return HttpResponseRedirect(url)
    13281344
    13291345        object_name = force_text(opts.verbose_name)
    13301346
  • tests/regressiontests/admin_changelist/tests.py

    diff --git a/tests/regressiontests/admin_changelist/tests.py b/tests/regressiontests/admin_changelist/tests.py
    index 7a3a5c0..6c98194 100644
    a b class ChangeListTests(TestCase): 
    3535    def _mocked_authenticated_request(self, url, user):
    3636        request = self.factory.get(url)
    3737        request.user = user
     38        request.session = {}
    3839        return request
    3940
    4041    def test_select_related_preserved(self):
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index d3cfaa3..a605841 100644
    a b class AdminUserMessageTest(TestCase): 
    40154015        self.assertContains(response,
    40164016                            '<li class="extra_tag info">Test tags</li>',
    40174017                            html=True)
     4018
     4019
     4020@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
     4021class AdminChangeListRedirectionTests(TestCase):
     4022    urls = "regressiontests.admin_views.urls"
     4023    fixtures = ['admin-views-users']
     4024
     4025    def setUp(self):
     4026        Person.objects.create(name="Chris", gender=1, alive=True, age=25)
     4027        Person.objects.create(name="Jack", gender=2, alive=False, age=47)
     4028        Person.objects.create(name="Bob", gender=1, alive=True, age=36)
     4029
     4030        self.client.login(username='super', password='secret')
     4031
     4032    def get_data(self):
     4033        return {
     4034            'name': 'Joe', 'gender': '1', 'alive': '1', 'age': '58'
     4035        }
     4036
     4037    def tearDown(self):
     4038        self.client.logout()
     4039
     4040    def get_filtered_changelist_url(self):
     4041        querystring = '?' + urlencode({
     4042                'alive': 1,
     4043            })
     4044        return "%s%s" % (reverse('admin:admin_views_person_changelist'), querystring)
     4045
     4046    def test_change_redirect(self):
     4047        """
     4048        Ensure that we're correctly redirected to the filtered changelist
     4049        after an existing object is saved.
     4050        Refs #6903.
     4051        """
     4052        # First open the changelist view with filters
     4053        self.client.get(self.get_filtered_changelist_url())
     4054
     4055        # Save an object
     4056        data = self.get_data()
     4057        data['_save'] = 1
     4058        person = Person.objects.get(name='Chris')
     4059        response = self.client.post(reverse('admin:admin_views_person_change', args=(person.pk,)), data)
     4060
     4061        # Check that we return to the filtered changelist
     4062        self.assertRedirects(response, self.get_filtered_changelist_url())
     4063
     4064    def test_change_popup_redirect(self):
     4065        """
     4066        Ensure that we're correctly redirected to the filtered changelist
     4067        after an existing object is saved, even if the changelist was opened
     4068        in a popup in the meantime.
     4069        Refs #6903.
     4070        """
     4071        # First open the changelist view with filters
     4072        self.client.get(self.get_filtered_changelist_url())
     4073
     4074        # Then open the non-filtered changelist view in a popup
     4075        self.client.get('%s?%s' % (reverse('admin:admin_views_person_changelist'), IS_POPUP_VAR))
     4076
     4077        # Save an object
     4078        data = self.get_data()
     4079        data['_save'] = 1
     4080        person = Person.objects.get(name='Chris')
     4081        response = self.client.post(reverse('admin:admin_views_person_change', args=(person.pk,)), data)
     4082
     4083        # Check that we return to the filtered changelist
     4084        self.assertRedirects(response, self.get_filtered_changelist_url())
     4085
     4086    def test_delete_redirect(self):
     4087        """
     4088        Ensure that we're correctly redirected to the filtered changelist
     4089        after an existing object is deleted.
     4090        Refs #6903.
     4091        """
     4092        # First open the changelist view with filters
     4093        self.client.get(self.get_filtered_changelist_url())
     4094
     4095        # Delete an object
     4096        person = Person.objects.get(name='Chris')
     4097        response = self.client.post(reverse('admin:admin_views_person_delete', args=(person.pk,)), {'post': 'yes'})
     4098
     4099        # Check that we return to the filtered changelist
     4100        self.assertRedirects(response, self.get_filtered_changelist_url())
     4101
     4102    def test_add_redirect(self):
     4103        """
     4104        Ensure that we're correctly redirected to the non-filtered changelist
     4105        after a new object is added.
     4106        Refs #6903.
     4107        """
     4108        # First open the changelist view with filters
     4109        self.client.get(self.get_filtered_changelist_url())
     4110
     4111        # Add a new object
     4112        data = self.get_data()
     4113        data['_save'] = 1
     4114        response = self.client.post(reverse('admin:admin_views_person_add'), data)
     4115
     4116        # Check that we return to the non-filtered changelist
     4117        self.assertRedirects(response, reverse('admin:admin_views_person_changelist'))
Back to Top