Ticket #12241: preserve_query_string_with_tests_4.diff

File preserve_query_string_with_tests_4.diff, 6.9 KB (added by batiste, 5 years ago)

Simplification of the tests

  • django/contrib/admin/options.py

     
    2424from django.utils.translation import ungettext, ugettext_lazy
    2525from django.utils.encoding import force_unicode
    2626
     27import urllib
     28import urlparse
     29
    2730HORIZONTAL, VERTICAL = 1, 2
    2831# returns the <ul> class for a given radio_admin field
    2932get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '')
     
    633636            "admin/change_form.html"
    634637        ], context, context_instance=context_instance)
    635638
     639    def redirect_with_query_string(self, request, url_continue, msg):
     640        """
     641        Handle a redirect URL and preserve the query string.
     642        """
     643        self.message_user(request, msg + ' ' + _("You may edit it again below."))
     644        string_dict = urlparse.parse_qs(request.META.get('QUERY_STRING'))
     645        if request.POST.has_key("_popup"):
     646            string_dict['_popup'] = 1
     647
     648        if string_dict:
     649            return url_continue + '?' + urllib.urlencode(string_dict)
     650       
     651        return url_continue
     652
    636653    def response_add(self, request, obj, post_url_continue='../%s/'):
    637654        """
    638655        Determines the HttpResponse for the add_view stage.
     
    644661        # Here, we distinguish between different save types by checking for
    645662        # the presence of keys in request.POST.
    646663        if request.POST.has_key("_continue"):
    647             self.message_user(request, msg + ' ' + _("You may edit it again below."))
    648             if request.POST.has_key("_popup"):
    649                 post_url_continue += "?_popup=1"
    650             return HttpResponseRedirect(post_url_continue % pk_value)
     664            url = post_url_continue % pk_value
     665            url = self.redirect_with_query_string(request, url, msg)
     666            return HttpResponseRedirect(url)
    651667
    652         if request.POST.has_key("_popup"):
     668        if request.POST.has_key('_popup'):
    653669            return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \
    654670                # escape() calls force_unicode.
    655671                (escape(pk_value), escape(obj)))
     
    677693
    678694        msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj)}
    679695        if request.POST.has_key("_continue"):
    680             self.message_user(request, msg + ' ' + _("You may edit it again below."))
    681             if request.REQUEST.has_key('_popup'):
    682                 return HttpResponseRedirect(request.path + "?_popup=1")
    683             else:
    684                 return HttpResponseRedirect(request.path)
     696            url = self.redirect_with_query_string(request, request.path, msg)
     697            return HttpResponseRedirect(url)
    685698        elif request.POST.has_key("_saveasnew"):
    686699            msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_unicode(opts.verbose_name), 'obj': obj}
    687700            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.
     
    16231624            else:
    16241625                self.assertEqual(response.status_code, 404)
    16251626
     1627class AdminResponseChangeTest(TestCase):
     1628    fixtures = ['admin-views-users.xml']
     1629
     1630    def setUp(self):
     1631        self.client.login(username='super', password='secret')
     1632        self.data = {'title':"I Could Go Anywhere", 'content':"Versatile", 'date':datetime.datetime.now()}
     1633        article = Article.objects.create(**self.data)
     1634        self.pk = article.id
     1635
     1636    def check_redirect(self, response):
     1637        self.assertEqual(response.status_code, 302)
     1638        location = urlparse.urlparse(response['Location'])
     1639        return urlparse.parse_qs(location.query)
     1640
     1641    def test_response_add(self):
     1642        """Add test for #12241: ModelAdmin.response_add should preserve the request
     1643        query string after the redirection if the "save and continue" button
     1644        is used within the admin."""
     1645       
     1646        self.data['_continue'] = True
     1647        self.data['date_0'] = u"2008-03-18"
     1648        self.data['date_1'] = u"11:54:58"
     1649        response = self.client.post('/test_admin/admin/admin_views/article/%d/?test=1' % self.pk, self.data)
     1650        parsed_query_string = self.check_redirect(response)
     1651        self.assertTrue('test' in parsed_query_string.keys())
     1652
     1653        response = self.client.post('/test_admin/admin/admin_views/article/add/?test=1', self.data)
     1654        parsed_query_string = self.check_redirect(response)
     1655        self.assertTrue('test' in parsed_query_string.keys())
     1656
     1657        # test the popup redirection
     1658        self.data['_popup'] = 1
     1659        response = self.client.post('/test_admin/admin/admin_views/article/add/', self.data)
     1660        parsed_query_string = self.check_redirect(response)
     1661        self.assertTrue('_popup' in parsed_query_string.keys())
     1662
     1663        # test the popup redirection and another GET parameter
     1664        response = self.client.post('/test_admin/admin/admin_views/article/add/?test=1', self.data)
     1665        parsed_query_string = self.check_redirect(response)
     1666        self.assertTrue('test' in parsed_query_string.keys())
     1667        self.assertTrue('_popup' in parsed_query_string.keys())
     1668
     1669        # without anything
     1670        del self.data['_popup']
     1671        response = self.client.post('/test_admin/admin/admin_views/article/add/', self.data)
     1672        parsed_query_string = self.check_redirect(response)
     1673        self.assertFalse('test' in parsed_query_string.keys())
     1674        self.assertFalse('_popup' in parsed_query_string.keys())
     1675
     1676    def test_response_change(self):
     1677        """Change test for issue #12241. ModelAdmin.response_change should
     1678        preserve the query string."""
     1679
     1680        self.data['date_0'] = u"2008-03-18"
     1681        self.data['date_1'] = u"11:54:58"
     1682        self.data['_continue'] = True
     1683
     1684        response = self.client.post('/test_admin/admin/admin_views/article/%d/?test=1' % self.pk, self.data)
     1685        parsed_query_string = self.check_redirect(response)
     1686        self.assertTrue('test' in parsed_query_string.keys())
     1687       
     1688        response = self.client.post('/test_admin/admin/admin_views/article/%d/' % self.pk, self.data)
     1689        parsed_query_string = self.check_redirect(response)
     1690        self.assertFalse('test' in parsed_query_string.keys())
     1691
     1692
     1693
    16261694class AdminInlineFileUploadTest(TestCase):
    16271695    fixtures = ['admin-views-users.xml', 'admin-views-actions.xml']
    16281696    urlbit = 'admin'
Back to Top