Ticket #12241: preserve_query_string_with_tests.diff

File preserve_query_string_with_tests.diff, 4.1 KB (added by Batiste Bieler, 15 years ago)

Preserve query string, with tests

  • django/contrib/admin/options.py

     
    634634            "admin/change_form.html"
    635635        ], context, context_instance=context_instance)
    636636
     637    def handle_continue_redirect(self, request, url_continue, msg):
     638        self.message_user(request, msg + ' ' + _("You may edit it again below."))
     639        if request.POST.has_key("_popup"):
     640            url_continue += ("?_popup=1" + request.META.get('QUERY_STRING'))
     641        else:
     642            url_continue += ("?" + request.META.get('QUERY_STRING'))
     643        return url_continue
     644
    637645    def response_add(self, request, obj, post_url_continue='../%s/'):
    638646        """
    639647        Determines the HttpResponse for the add_view stage.
     
    645653        # Here, we distinguish between different save types by checking for
    646654        # the presence of keys in request.POST.
    647655        if request.POST.has_key("_continue"):
    648             self.message_user(request, msg + ' ' + _("You may edit it again below."))
    649             if request.POST.has_key("_popup"):
    650                 post_url_continue += "?_popup=1"
    651             return HttpResponseRedirect(post_url_continue % pk_value)
     656            url = self.handle_continue_redirect(request, post_url_continue, msg)
     657            return HttpResponseRedirect(url % pk_value)
    652658
    653659        if request.POST.has_key("_popup"):
    654660            return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \
     
    678684
    679685        msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj)}
    680686        if request.POST.has_key("_continue"):
    681             self.message_user(request, msg + ' ' + _("You may edit it again below."))
    682             if request.REQUEST.has_key('_popup'):
    683                 return HttpResponseRedirect(request.path + "?_popup=1")
    684             else:
    685                 return HttpResponseRedirect(request.path)
     687            url = self.handle_continue_redirect(request, request.path, msg)
     688            return HttpResponseRedirect(url)
    686689        elif request.POST.has_key("_saveasnew"):
    687690            msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_unicode(opts.verbose_name), 'obj': obj}
    688691            self.message_user(request, msg)
  • tests/regressiontests/admin_views/tests.py

     
    15131513            else:
    15141514                self.assertEqual(response.status_code, 404)
    15151515
     1516class AdminResponseChangeTest(TestCase):
     1517    fixtures = ['admin-views-users.xml']
     1518
     1519    def setUp(self):
     1520        self.client.login(username='super', password='secret')
     1521        self.data = {'title':"I Could Go Anywhere", 'content':"Versatile", 'date':datetime.datetime.now()}
     1522        article = Article.objects.create(**self.data)
     1523        self.pk = article.id
     1524
     1525    def test_response_change(self):
     1526        """Regression test for #12241: ModelAdmin.response_change and
     1527        ModelAdmin.response_add should preserve the request
     1528        query string after the redirection if the "save and continue" button
     1529        is used within the admin."""
     1530        self.data['_continue'] = True
     1531        self.data['date_0'] = u"2008-03-18"
     1532        self.data['date_1'] = u"11:54:58"
     1533        response = self.client.post('/test_admin/admin/admin_views/article/%d/?test=1' % self.pk, self.data)
     1534        self.assertEqual(response.status_code, 302)
     1535        self.assertTrue("?test=1" in response['Location'])
     1536        response = self.client.post('/test_admin/admin/admin_views/article/add/?test=1', self.data)
     1537        self.assertEqual(response.status_code, 302)
     1538        self.assertTrue("?test=1" in response['Location'])
     1539
     1540
    15161541class AdminInlineFileUploadTest(TestCase):
    15171542    fixtures = ['admin-views-users.xml', 'admin-views-actions.xml']
    15181543    urlbit = 'admin'
Back to Top