diff options
| author | Mark Aaron Shirley | 2013-04-16 09:55:47 -0700 |
|---|---|---|
| committer | Mark Aaron Shirley | 2013-04-16 09:55:47 -0700 |
| commit | c7e000e46e831a254689faac44ea44ebafe3cd61 (patch) | |
| tree | f3b3ed8c258b82b3dfb1939404f00f5fb16b7c1c /rest_framework/compat.py | |
| parent | bda25479aa7e73c90bc77b7c7219eaa411af138e (diff) | |
| parent | 23289b023db230f73e4a5bfae24a56c79e3fcd4b (diff) | |
| download | django-rest-framework-c7e000e46e831a254689faac44ea44ebafe3cd61.tar.bz2 | |
Merge remote-tracking branch 'upstream/master' into writable-nested-modelserializer
Conflicts:
docs/api-guide/serializers.md
Diffstat (limited to 'rest_framework/compat.py')
| -rw-r--r-- | rest_framework/compat.py | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 7b2ef738..067e9018 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -395,6 +395,41 @@ 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: + import re + from django.utils.encoding import smart_str + try: + from urllib.parse import quote, urlsplit, urlunsplit + except ImportError: # Python 2 + from urllib import quote + from urlparse import urlsplit, urlunsplit + + unquoted_percents_re = re.compile(r'%(?![0-9A-Fa-f]{2})') + + 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(smart_str(url), safe=b'!*\'();:@&=+$,/?#[]~') + + return force_text(url) + + # Markdown is optional try: import markdown @@ -445,14 +480,12 @@ except ImportError: # OAuth 2 support is optional try: import provider.oauth2 as oauth2_provider - from provider.oauth2 import backends as oauth2_provider_backends from provider.oauth2 import models as oauth2_provider_models from provider.oauth2 import forms as oauth2_provider_forms from provider import scope as oauth2_provider_scope from provider import constants as oauth2_constants except ImportError: oauth2_provider = None - oauth2_provider_backends = None oauth2_provider_models = None oauth2_provider_forms = None oauth2_provider_scope = None |
