diff options
| author | Tom Christie | 2013-03-29 06:40:23 -0700 |
|---|---|---|
| committer | Tom Christie | 2013-03-29 06:40:23 -0700 |
| commit | ff3ebd979dab9a358a4708ea1de0fd8ebf121157 (patch) | |
| tree | e19fa6a9d85020be5c8c12906bcb33dd9a0c4b84 /rest_framework/compat.py | |
| parent | 2169c34a6fe0b2d780d374fe0cee5bf30a6d7e0e (diff) | |
| parent | 9c32f048b51ec6852236363932f0ab0dcc7473ac (diff) | |
| download | django-rest-framework-ff3ebd979dab9a358a4708ea1de0fd8ebf121157.tar.bz2 | |
Merge pull request #766 from glic3rinu/master
Refactored urlize_quoted_links based on Dango 1.5 urlize
Diffstat (limited to 'rest_framework/compat.py')
| -rw-r--r-- | rest_framework/compat.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 7b2ef738..f0bb9c08 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -395,6 +395,37 @@ except ImportError: kw = dict((k, int(v)) for k, v in kw.iteritems() if v is not None) return datetime.datetime(**kw) + +# smart_urlquote is new on Django 1.4 +try: + from django.utils.html import smart_urlquote +except ImportError: + try: + from urllib.parse import quote, urlsplit, urlunsplit + except ImportError: # Python 2 + from urllib import quote + from urlparse import urlsplit, urlunsplit + + def smart_urlquote(url): + "Quotes a URL if it isn't already quoted." + # Handle IDN before quoting. + scheme, netloc, path, query, fragment = urlsplit(url) + try: + netloc = netloc.encode('idna').decode('ascii') # IDN -> ACE + except UnicodeError: # invalid domain part + pass + else: + url = urlunsplit((scheme, netloc, path, query, fragment)) + + # An URL is considered unquoted if it contains no % characters or + # contains a % not followed by two hexadecimal digits. See #9655. + if '%' not in url or unquoted_percents_re.search(url): + # See http://bugs.python.org/issue2637 + url = quote(force_bytes(url), safe=b'!*\'();:@&=+$,/?#[]~') + + return force_text(url) + + # Markdown is optional try: import markdown |
