summaryrefslogtreecommitdiff
path: root/django/utils/encoding.py
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-05-17 18:19:40 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-05-17 18:19:40 +0000
commitc0b451fcfa516aa24ec4126171ed39c624e12ca7 (patch)
tree1ac5b8db550a34ea97ff767a41afc0704369bfb4 /django/utils/encoding.py
parent52b0051523cb6608bf401aff9a42bc88567606cc (diff)
unicode: Added iri_to_uri() utility function and use it in the databrowse app.
Should make getting this stuff right a bit easier. git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5278 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/encoding.py')
-rw-r--r--django/utils/encoding.py24
1 files changed, 19 insertions, 5 deletions
diff --git a/django/utils/encoding.py b/django/utils/encoding.py
index d77269bde8..7466e02300 100644
--- a/django/utils/encoding.py
+++ b/django/utils/encoding.py
@@ -1,7 +1,17 @@
import types
+import urllib
from django.conf import settings
from django.utils.functional import Promise
+class StrAndUnicode(object):
+ """
+ A class whose __str__ returns its __unicode__ as a UTF-8 bytestring.
+
+ Useful as a mix-in.
+ """
+ def __str__(self):
+ return self.__unicode__().encode('utf-8')
+
def smart_unicode(s, encoding='utf-8', errors='strict'):
"""
Returns a unicode object representing 's'. Treats bytestrings using the
@@ -45,12 +55,16 @@ def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
else:
return s
-class StrAndUnicode(object):
+def iri_to_uri(iri):
"""
- A class whose __str__ returns its __unicode__ as a UTF-8 bytestring.
+ Convert an Internationalized Resource Identifier (IRI) portion to a URI
+ portion that is suitable for inclusion in a URL.
- Useful as a mix-in.
+ This is the algorithm from section 3.1 of RFC 3987. However, since we are
+ assuming input is either UTF-8 or unicode already, we can simplify things a
+ little from the full method.
+
+ Returns an ASCII string containing the encoded result.
"""
- def __str__(self):
- return self.__unicode__().encode('utf-8')
+ return urllib.quote(smart_str(iri), safe='/#%[]')