Ticket #12241: preserve_query_string_with_tests_3.diff
File preserve_query_string_with_tests_3.diff, 7.0 KB (added by , 14 years ago) |
---|
-
django/contrib/admin/options.py
24 24 from django.utils.translation import ungettext, ugettext_lazy 25 25 from django.utils.encoding import force_unicode 26 26 27 import urllib 28 import urlparse 29 27 30 HORIZONTAL, VERTICAL = 1, 2 28 31 # returns the <ul> class for a given radio_admin field 29 32 get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '') … … 633 636 "admin/change_form.html" 634 637 ], context, context_instance=context_instance) 635 638 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 636 653 def response_add(self, request, obj, post_url_continue='../%s/'): 637 654 """ 638 655 Determines the HttpResponse for the add_view stage. … … 644 661 # Here, we distinguish between different save types by checking for 645 662 # the presence of keys in request.POST. 646 663 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) 651 667 652 if request.POST.has_key( "_popup"):668 if request.POST.has_key('_popup'): 653 669 return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \ 654 670 # escape() calls force_unicode. 655 671 (escape(pk_value), escape(obj))) … … 677 693 678 694 msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj)} 679 695 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) 685 698 elif request.POST.has_key("_saveasnew"): 686 699 msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_unicode(opts.verbose_name), 'obj': obj} 687 700 self.message_user(request, msg) -
tests/regressiontests/admin_views/tests.py
2 2 3 3 import re 4 4 import datetime 5 import urlparse 5 6 from django.conf import settings 6 7 from django.core.files import temp as tempfile 7 8 from django.contrib.auth import admin # Register auth models with the admin. … … 1623 1624 else: 1624 1625 self.assertEqual(response.status_code, 404) 1625 1626 1627 class 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 self.assertFalse('_popup' in parsed_query_string.keys()) 1688 1689 response = self.client.post('/test_admin/admin/admin_views/article/%d/' % self.pk, self.data) 1690 parsed_query_string = self.check_redirect(response) 1691 self.assertFalse('test' in parsed_query_string.keys()) 1692 self.assertFalse('_popup' in parsed_query_string.keys()) 1693 1694 1695 1626 1696 class AdminInlineFileUploadTest(TestCase): 1627 1697 fixtures = ['admin-views-users.xml', 'admin-views-actions.xml'] 1628 1698 urlbit = 'admin'