Ticket #12241: preserve_query_string_with_tests_5.diff

File preserve_query_string_with_tests_5.diff, 6.9 KB (added by Batiste Bieler, 14 years ago)

Remove the urlparse call and use request.GET.copy instead

  • django/contrib/admin/options.py

     
    2424from django.utils.translation import ungettext, ugettext_lazy
    2525from django.utils.encoding import force_unicode
    2626
     27import urllib
     28
    2729HORIZONTAL, VERTICAL = 1, 2
    2830# returns the <ul> class for a given radio_admin field
    2931get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '')
     
    639641            "admin/change_form.html"
    640642        ], context, context_instance=context_instance)
    641643
     644    def redirect_with_query_string(self, request, url_continue, msg):
     645        """
     646        Handle a redirect URL and preserve the query string.
     647        """
     648        self.message_user(request, msg + ' ' + _("You may edit it again below."))
     649        string_dict = request.GET.copy()
     650        if request.POST.has_key("_popup"):
     651            string_dict['_popup'] = 1
     652
     653        if string_dict:
     654            return url_continue + '?' + urllib.urlencode(string_dict)
     655
     656        return url_continue
     657
    642658    def response_add(self, request, obj, post_url_continue='../%s/'):
    643659        """
    644660        Determines the HttpResponse for the add_view stage.
     
    650666        # Here, we distinguish between different save types by checking for
    651667        # the presence of keys in request.POST.
    652668        if request.POST.has_key("_continue"):
    653             self.message_user(request, msg + ' ' + _("You may edit it again below."))
    654             if request.POST.has_key("_popup"):
    655                 post_url_continue += "?_popup=1"
    656             return HttpResponseRedirect(post_url_continue % pk_value)
     669            url = post_url_continue % pk_value
     670            url = self.redirect_with_query_string(request, url, msg)
     671            return HttpResponseRedirect(url)
    657672
    658         if request.POST.has_key("_popup"):
     673        if request.POST.has_key('_popup'):
    659674            return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \
    660675                # escape() calls force_unicode.
    661676                (escape(pk_value), escape(obj)))
     
    683698
    684699        msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj)}
    685700        if request.POST.has_key("_continue"):
    686             self.message_user(request, msg + ' ' + _("You may edit it again below."))
    687             if request.REQUEST.has_key('_popup'):
    688                 return HttpResponseRedirect(request.path + "?_popup=1")
    689             else:
    690                 return HttpResponseRedirect(request.path)
     701            url = self.redirect_with_query_string(request, request.path, msg)
     702            return HttpResponseRedirect(url)
    691703        elif request.POST.has_key("_saveasnew"):
    692704            msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_unicode(opts.verbose_name), 'obj': obj}
    693705            self.message_user(request, msg)
  • tests/regressiontests/admin_views/tests.py

     
    22
    33import re
    44import datetime
     5import urlparse
    56from django.conf import settings
    67from django.core.files import temp as tempfile
    78from django.contrib.auth import admin # Register auth models with the admin.
     
    16601661            else:
    16611662                self.assertEqual(response.status_code, 404)
    16621663
     1664class AdminResponseChangeTest(TestCase):
     1665    fixtures = ['admin-views-users.xml']
     1666
     1667    def setUp(self):
     1668        self.client.login(username='super', password='secret')
     1669        self.data = {'title':"I Could Go Anywhere", 'content':"Versatile", 'date':datetime.datetime.now()}
     1670        article = Article.objects.create(**self.data)
     1671        self.pk = article.id
     1672
     1673    def check_redirect(self, response):
     1674        self.assertEqual(response.status_code, 302)
     1675        location = urlparse.urlparse(response['Location'])
     1676        return urlparse.parse_qs(location.query)
     1677
     1678    def test_response_add(self):
     1679        """Add test for #12241: ModelAdmin.response_add should preserve the request
     1680        query string after the redirection if the "save and continue" button
     1681        is used within the admin."""
     1682
     1683        self.data['_continue'] = True
     1684        self.data['date_0'] = u"2008-03-18"
     1685        self.data['date_1'] = u"11:54:58"
     1686        response = self.client.post('/test_admin/admin/admin_views/article/%d/?test=1' % self.pk, self.data)
     1687        parsed_query_string = self.check_redirect(response)
     1688        self.assertTrue('test' in parsed_query_string.keys())
     1689
     1690        response = self.client.post('/test_admin/admin/admin_views/article/add/?test=1', self.data)
     1691        parsed_query_string = self.check_redirect(response)
     1692        self.assertTrue('test' in parsed_query_string.keys())
     1693
     1694        # test the popup redirection
     1695        self.data['_popup'] = 1
     1696        response = self.client.post('/test_admin/admin/admin_views/article/add/', self.data)
     1697        parsed_query_string = self.check_redirect(response)
     1698        self.assertTrue('_popup' in parsed_query_string.keys())
     1699
     1700        # test the popup redirection and 2 other GET parameters
     1701        response = self.client.post('/test_admin/admin/admin_views/article/add/?test=1&extra=1', self.data)
     1702        parsed_query_string = self.check_redirect(response)
     1703        self.assertTrue('test' in parsed_query_string.keys())
     1704        self.assertTrue('extra' in parsed_query_string.keys())
     1705        self.assertTrue('_popup' in parsed_query_string.keys())
     1706
     1707        # without anything
     1708        del self.data['_popup']
     1709        response = self.client.post('/test_admin/admin/admin_views/article/add/', self.data)
     1710        parsed_query_string = self.check_redirect(response)
     1711        self.assertFalse('test' in parsed_query_string.keys())
     1712        self.assertFalse('_popup' in parsed_query_string.keys())
     1713
     1714    def test_response_change(self):
     1715        """Change test for issue #12241. ModelAdmin.response_change should
     1716        preserve the query string."""
     1717
     1718        self.data['date_0'] = u"2008-03-18"
     1719        self.data['date_1'] = u"11:54:58"
     1720        self.data['_continue'] = True
     1721
     1722        response = self.client.post('/test_admin/admin/admin_views/article/%d/?test=1' % self.pk, self.data)
     1723        parsed_query_string = self.check_redirect(response)
     1724        self.assertTrue('test' in parsed_query_string.keys())
     1725
     1726        response = self.client.post('/test_admin/admin/admin_views/article/%d/' % self.pk, self.data)
     1727        parsed_query_string = self.check_redirect(response)
     1728        self.assertFalse('test' in parsed_query_string.keys())
     1729
     1730
     1731
    16631732class AdminInlineFileUploadTest(TestCase):
    16641733    fixtures = ['admin-views-users.xml', 'admin-views-actions.xml']
    16651734    urlbit = 'admin'
Back to Top