Ticket #12241: preserve_query_string_with_tests_5.diff
File preserve_query_string_with_tests_5.diff, 6.9 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 27 29 HORIZONTAL, VERTICAL = 1, 2 28 30 # returns the <ul> class for a given radio_admin field 29 31 get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '') … … 639 641 "admin/change_form.html" 640 642 ], context, context_instance=context_instance) 641 643 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 642 658 def response_add(self, request, obj, post_url_continue='../%s/'): 643 659 """ 644 660 Determines the HttpResponse for the add_view stage. … … 650 666 # Here, we distinguish between different save types by checking for 651 667 # the presence of keys in request.POST. 652 668 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) 657 672 658 if request.POST.has_key( "_popup"):673 if request.POST.has_key('_popup'): 659 674 return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \ 660 675 # escape() calls force_unicode. 661 676 (escape(pk_value), escape(obj))) … … 683 698 684 699 msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj)} 685 700 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) 691 703 elif request.POST.has_key("_saveasnew"): 692 704 msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_unicode(opts.verbose_name), 'obj': obj} 693 705 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. … … 1660 1661 else: 1661 1662 self.assertEqual(response.status_code, 404) 1662 1663 1664 class 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 1663 1732 class AdminInlineFileUploadTest(TestCase): 1664 1733 fixtures = ['admin-views-users.xml', 'admin-views-actions.xml'] 1665 1734 urlbit = 'admin'