Move the urlparse lib compatibility to the compat file.

This commit is contained in:
Xavier Ordoquy 2013-01-02 19:06:55 +01:00
parent c95fa81cb2
commit 4b77b3c5ad
3 changed files with 13 additions and 10 deletions

View File

@ -30,6 +30,13 @@ except ImportError:
from six import BytesIO from six import BytesIO
# urlparse compat import (Required because it changed in python 3.x)
try:
from urllib import parse as urlparse
except ImportError:
import urlparse as urlparse
# Try to import PIL in either of the two ways it can end up installed. # Try to import PIL in either of the two ways it can end up installed.
try: try:
from PIL import Image from PIL import Image
@ -109,7 +116,6 @@ else:
import re import re
import random import random
import logging import logging
import urlparse
from django.conf import settings from django.conf import settings
from django.core.urlresolvers import get_callable from django.core.urlresolvers import get_callable

View File

@ -12,7 +12,7 @@ except ImportError:
from django.utils.encoding import smart_unicode as smart_text from django.utils.encoding import smart_unicode as smart_text
from rest_framework.fields import Field, WritableField from rest_framework.fields import Field, WritableField
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from urlparse import urlparse from rest_framework.compat import urlparse
##### Relational fields ##### ##### Relational fields #####
@ -360,7 +360,7 @@ class HyperlinkedRelatedField(RelatedField):
if value.startswith('http:') or value.startswith('https:'): if value.startswith('http:') or value.startswith('https:'):
# If needed convert absolute URLs to relative path # If needed convert absolute URLs to relative path
value = urlparse(value).path value = urlparse.urlparse(value).path
prefix = get_script_prefix() prefix = get_script_prefix()
if value.startswith(prefix): if value.startswith(prefix):
value = '/' + value[len(prefix):] value = '/' + value[len(prefix):]

View File

@ -1,4 +1,4 @@
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
import six import six
from django import template from django import template
@ -10,10 +10,7 @@ except ImportError:
from django.utils.encoding import force_unicode as force_text from django.utils.encoding import force_unicode as force_text
from django.utils.html import escape from django.utils.html import escape
from django.utils.safestring import SafeData, mark_safe from django.utils.safestring import SafeData, mark_safe
try: from rest_framework.compat import urlparse
from urllib.parse import urlsplit, urlunsplit
except ImportError:
from urlparse import urlsplit, urlunsplit
import re import re
import string import string
@ -108,11 +105,11 @@ def replace_query_param(url, key, val):
Given a URL and a key/val pair, set or replace an item in the query Given a URL and a key/val pair, set or replace an item in the query
parameters of the URL, and return the new URL. parameters of the URL, and return the new URL.
""" """
(scheme, netloc, path, query, fragment) = urlsplit(url) (scheme, netloc, path, query, fragment) = urlparse.urlsplit(url)
query_dict = QueryDict(query).copy() query_dict = QueryDict(query).copy()
query_dict[key] = val query_dict[key] = val
query = query_dict.urlencode() query = query_dict.urlencode()
return urlunsplit((scheme, netloc, path, query, fragment)) return urlparse.urlunsplit((scheme, netloc, path, query, fragment))
# Regex for adding classes to html snippets # Regex for adding classes to html snippets