diff --git a/django/test/client.py b/django/test/client.py index 848cdce9f0..96db58fe10 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -10,10 +10,11 @@ from django.core.handlers.base import BaseHandler from django.core.handlers.wsgi import WSGIRequest from django.core.signals import got_request_exception from django.dispatch import dispatcher -from django.http import urlencode, SimpleCookie, HttpRequest +from django.http import SimpleCookie, HttpRequest from django.test import signals from django.utils.functional import curry from django.utils.encoding import smart_str +from django.utils.http import urlencode BOUNDARY = 'BoUnDaRyStRiNg' MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY diff --git a/django/utils/html.py b/django/utils/html.py index 015bc757bd..1dde298c29 100644 --- a/django/utils/html.py +++ b/django/utils/html.py @@ -125,22 +125,3 @@ def clean_html(text): return text clean_html = allow_lazy(clean_html, unicode) -def urlquote(url, safe='/'): - """ - A version of Python's urllib.quote() function that can operate on unicode - strings. The url is first UTF-8 encoded before quoting. The returned string - can safely be used as part of an argument to a subsequent iri_to_uri() call - without double-quoting occurring. - """ - return force_unicode(urllib.quote(smart_str(url))) -urlquote = allow_lazy(urlquote, unicode) - -def urlquote_plus(url, safe=''): - """ - A version of Python's urllib.quote_plus() function that can operate on - unicode strings. The url is first UTF-8 encoded before quoting. The - returned string can safely be used as part of an argument to a subsequent - iri_to_uri() call without double-quoting occurring. - """ - return force_unicode(urllib.quote_plus(smart_str(url), safe)) -urlquote_plus = allow_lazy(urlquote_plus, unicode) diff --git a/django/utils/http.py b/django/utils/http.py new file mode 100644 index 0000000000..5a0c18d1c0 --- /dev/null +++ b/django/utils/http.py @@ -0,0 +1,35 @@ +import urllib +from django.utils.encoding import smart_str, force_unicode +from django.utils.functional import allow_lazy + +def urlquote(url, safe='/'): + """ + A version of Python's urllib.quote() function that can operate on unicode + strings. The url is first UTF-8 encoded before quoting. The returned string + can safely be used as part of an argument to a subsequent iri_to_uri() call + without double-quoting occurring. + """ + return force_unicode(urllib.quote(smart_str(url))) +urlquote = allow_lazy(urlquote, unicode) + +def urlquote_plus(url, safe=''): + """ + A version of Python's urllib.quote_plus() function that can operate on + unicode strings. The url is first UTF-8 encoded before quoting. The + returned string can safely be used as part of an argument to a subsequent + iri_to_uri() call without double-quoting occurring. + """ + return force_unicode(urllib.quote_plus(smart_str(url), safe)) +urlquote_plus = allow_lazy(urlquote_plus, unicode) + +def urlencode(query, doseq=0): + """ + A version of Python's urllib.urlencode() function that can operate on + unicode strings. The parameters are first case to UTF-8 encoded strings and + then encoded as per normal. + """ + if hasattr(query, 'items'): + query = query.items() + return urllib.urlencode([(smart_str(k), smart_str(v)) for k, + v in query], doseq) + diff --git a/docs/unicode.txt b/docs/unicode.txt index 70be4a99ee..92b73ec240 100644 --- a/docs/unicode.txt +++ b/docs/unicode.txt @@ -149,8 +149,8 @@ a little tricky, so Django provides some assistance. * The function ``django.utils.encoding.iri_to_uri()`` implements the conversion from IRI to URI as required by `the specification`_. - * The functions ``django.utils.html.urlquote()`` and - ``django.utils.html.urlquote_plus()`` are versions of Python's standard + * The functions ``django.utils.http.urlquote()`` and + ``django.utils.http.urlquote_plus()`` are versions of Python's standard ``urllib.quote()`` and ``urllib.quote_plus()`` that work with non-ASCII characters (the data is converted to UTF-8 prior to encoding). @@ -235,7 +235,7 @@ encoding yourself. Normally, this would involve a combination of the example:: from django.utils.encoding import iri_to_uri - from django.utils.html import urlquote + from django.utils.http import urlquote def get_absolute_url(self): url = u'/person/%s/?x=0&y=0' % urlquote(self.location) diff --git a/tests/modeltests/test_client/models.py b/tests/modeltests/test_client/models.py index 5ebf29678c..06e574590f 100644 --- a/tests/modeltests/test_client/models.py +++ b/tests/modeltests/test_client/models.py @@ -1,3 +1,4 @@ +# coding: utf-8 """ 38. Testing using the Test Client @@ -27,11 +28,14 @@ class ClientTest(TestCase): def test_get_view(self): "GET a view" - response = self.client.get('/test_client/get_view/') + # The data is ignored, but let's check it doesn't crash the system + # anyway. + data = {'var': u'\xf2'} + response = self.client.get('/test_client/get_view/', data) # Check some response details self.assertContains(response, 'This is a test') - self.assertEqual(response.context['var'], 42) + self.assertEqual(response.context['var'], u'\xf2') self.assertEqual(response.template.name, 'GET Template') def test_get_post_view(self): @@ -281,4 +285,4 @@ class ClientTest(TestCase): self.assertEqual(mail.outbox[1].from_email, 'from@example.com') self.assertEqual(mail.outbox[1].to[0], 'second@example.com') self.assertEqual(mail.outbox[1].to[1], 'third@example.com') - \ No newline at end of file + diff --git a/tests/modeltests/test_client/views.py b/tests/modeltests/test_client/views.py index 9bdf213b35..21f577d758 100644 --- a/tests/modeltests/test_client/views.py +++ b/tests/modeltests/test_client/views.py @@ -10,7 +10,7 @@ from django.shortcuts import render_to_response def get_view(request): "A simple view that expects a GET request, and returns a rendered template" t = Template('This is a test. {{ var }} is the value.', name='GET Template') - c = Context({'var': 42}) + c = Context({'var': request.GET.get('var', 42)}) return HttpResponse(t.render(c)) diff --git a/tests/regressiontests/text/tests.py b/tests/regressiontests/text/tests.py index 8cc8e4eeac..d0e0294cae 100644 --- a/tests/regressiontests/text/tests.py +++ b/tests/regressiontests/text/tests.py @@ -17,7 +17,7 @@ r""" friends' ### urlquote ############################################################# ->>> from django.utils.html import urlquote, urlquote_plus +>>> from django.utils.http import urlquote, urlquote_plus >>> urlquote(u'Paris & Orl\xe9ans') u'Paris%20%26%20Orl%C3%A9ans' >>> urlquote_plus(u'Paris & Orl\xe9ans')