Took advantage of django.utils.six.moves.urllib.*.

This commit is contained in:
Aymeric Augustin 2013-09-05 14:38:59 -05:00
parent ed9cd4fd8b
commit 6a6428a36f
31 changed files with 50 additions and 152 deletions

View File

@ -1,13 +1,10 @@
try:
from urllib.parse import urlparse
except ImportError: # Python 2
from urlparse import urlparse
from functools import wraps
from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.core.exceptions import PermissionDenied
from django.utils.decorators import available_attrs
from django.utils.encoding import force_str
from django.utils.six.moves.urllib.parse import urlparse
from django.shortcuts import resolve_url

View File

@ -1,10 +1,6 @@
import itertools
import os
import re
try:
from urllib.parse import urlparse, ParseResult
except ImportError: # Python 2
from urlparse import urlparse, ParseResult
from django.conf import global_settings, settings
from django.contrib.sites.models import Site, RequestSite
@ -15,6 +11,7 @@ from django.core.urlresolvers import reverse, NoReverseMatch
from django.http import QueryDict, HttpRequest
from django.utils.encoding import force_text
from django.utils.http import urlquote
from django.utils.six.moves.urllib.parse import urlparse, ParseResult
from django.utils._os import upath
from django.test import TestCase
from django.test.utils import override_settings, patch_logger

View File

@ -1,14 +1,10 @@
try:
from urllib.parse import urlparse, urlunparse
except ImportError: # Python 2
from urlparse import urlparse, urlunparse
from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, QueryDict
from django.template.response import TemplateResponse
from django.utils.http import is_safe_url, urlsafe_base64_decode
from django.utils.translation import ugettext as _
from django.utils.six.moves.urllib.parse import urlparse, urlunparse
from django.shortcuts import resolve_url
from django.views.decorators.debug import sensitive_post_parameters
from django.views.decorators.cache import never_cache

View File

@ -3,10 +3,6 @@ A few bits of helper functions for comment views.
"""
import textwrap
try:
from urllib.parse import urlencode
except ImportError: # Python 2
from urllib import urlencode
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response, resolve_url
@ -14,6 +10,7 @@ from django.template import RequestContext
from django.core.exceptions import ObjectDoesNotExist
from django.contrib import comments
from django.utils.http import is_safe_url
from django.utils.six.moves.urllib.parse import urlencode
def next_redirect(request, fallback, **get_kwargs):
"""

View File

@ -1,11 +1,8 @@
from django.contrib.sites.models import Site
from django.core import urlresolvers, paginator
from django.core.exceptions import ImproperlyConfigured
try:
from urllib.parse import urlencode
from urllib.request import urlopen
except ImportError: # Python 2
from urllib import urlencode, urlopen
from django.utils.six.moves.urllib.parse import urlencode
from django.utils.six.moves.urllib.request import urlopen
PING_URL = "http://www.google.com/webmasters/tools/ping"

View File

@ -1,13 +1,8 @@
try:
from urllib.parse import urlparse
from urllib.request import url2pathname
except ImportError: # Python 2
from urllib import url2pathname
from urlparse import urlparse
from django.conf import settings
from django.core.handlers.base import get_path_info
from django.core.handlers.wsgi import WSGIHandler
from django.utils.six.moves.urllib.parse import urlparse
from django.utils.six.moves.urllib.request import url2pathname
from django.contrib.staticfiles import utils
from django.contrib.staticfiles.views import serve

View File

@ -5,11 +5,6 @@ from importlib import import_module
import os
import posixpath
import re
try:
from urllib.parse import unquote, urlsplit, urlunsplit, urldefrag
except ImportError: # Python 2
from urllib import unquote
from urlparse import urlsplit, urlunsplit, urldefrag
from django.conf import settings
from django.core.cache import (get_cache, InvalidCacheBackendError,
@ -19,6 +14,7 @@ from django.core.files.base import ContentFile
from django.core.files.storage import FileSystemStorage, get_storage_class
from django.utils.encoding import force_bytes, force_text
from django.utils.functional import LazyObject
from django.utils.six.moves.urllib.parse import unquote, urlsplit, urlunsplit, urldefrag
from django.utils._os import upath
from django.contrib.staticfiles.utils import check_settings, matches_patterns

View File

@ -5,13 +5,10 @@ development, and SHOULD NOT be used in a production setting.
"""
import os
import posixpath
try:
from urllib.parse import unquote
except ImportError: # Python 2
from urllib import unquote
from django.conf import settings
from django.http import Http404
from django.utils.six.moves.urllib.parse import unquote
from django.views import static
from django.contrib.staticfiles import finders

View File

@ -15,10 +15,6 @@ cache class.
See docs/topics/cache.txt for information on the public API.
"""
import importlib
try:
from urllib.parse import parse_qsl
except ImportError: # Python 2
from urlparse import parse_qsl
from django.conf import settings
from django.core import signals
@ -26,6 +22,7 @@ from django.core.cache.backends.base import (
InvalidCacheBackendError, CacheKeyWarning, BaseCache)
from django.core.exceptions import ImproperlyConfigured
from django.utils.module_loading import import_by_path
from django.utils.six.moves.urllib.parse import parse_qsl
__all__ = [

View File

@ -1,9 +1,5 @@
import os
import errno
try:
from urllib.parse import urljoin
except ImportError: # Python 2
from urlparse import urljoin
import itertools
from datetime import datetime
@ -14,6 +10,7 @@ from django.core.files.move import file_move_safe
from django.utils.encoding import force_text, filepath_to_uri
from django.utils.functional import LazyObject
from django.utils.module_loading import import_by_path
from django.utils.six.moves.urllib.parse import urljoin
from django.utils.text import get_valid_filename
from django.utils._os import safe_join, abspathu

View File

@ -8,10 +8,6 @@ import shutil
import stat
import sys
import tempfile
try:
from urllib.request import urlretrieve
except ImportError: # Python 2
from urllib import urlretrieve
from optparse import make_option
from os import path
@ -19,6 +15,7 @@ from os import path
import django
from django.template import Template, Context
from django.utils import archive
from django.utils.six.moves.urllib.request import urlretrieve
from django.utils._os import rmtree_errorhandler
from django.core.management.base import BaseCommand, CommandError
from django.core.management.utils import handle_extensions

View File

@ -13,11 +13,6 @@ from io import BytesIO
import socket
import sys
import traceback
try:
from urllib.parse import urljoin
except ImportError: # Python 2
from urlparse import urljoin
from django.utils.six.moves import socketserver
from wsgiref import simple_server
from wsgiref.util import FileWrapper # for backwards compatibility
@ -25,6 +20,8 @@ from django.core.management.color import color_style
from django.core.wsgi import get_wsgi_application
from django.utils.module_loading import import_by_path
from django.utils import six
from django.utils.six.moves.urllib.parse import urljoin
from django.utils.six.moves import socketserver
__all__ = ('WSGIServer', 'WSGIRequestHandler', 'MAX_SOCKET_CHUNK_SIZE')

View File

@ -1,16 +1,14 @@
from __future__ import unicode_literals
import re
try:
from urllib.parse import urlsplit, urlunsplit
except ImportError: # Python 2
from urlparse import urlsplit, urlunsplit
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _, ungettext_lazy
from django.utils.encoding import force_text
from django.utils.ipv6 import is_valid_ipv6_address
from django.utils import six
from django.utils.six.moves.urllib.parse import urlsplit, urlunsplit
# These values, if given to validate(), will trigger the self.required check.
EMPTY_VALUES = (None, '', [], (), {})

View File

@ -9,10 +9,6 @@ import datetime
import os
import re
import sys
try:
from urllib.parse import urlsplit, urlunsplit
except ImportError: # Python 2
from urlparse import urlsplit, urlunsplit
from decimal import Decimal, DecimalException
from io import BytesIO
@ -29,6 +25,7 @@ from django.utils import formats
from django.utils.encoding import smart_text, force_str, force_text
from django.utils.ipv6 import clean_ipv6_address
from django.utils import six
from django.utils.six.moves.urllib.parse import urlsplit, urlunsplit
from django.utils.translation import ugettext_lazy as _, ungettext_lazy
# Provide this import for backwards compatibility.

View File

@ -6,10 +6,6 @@ from __future__ import unicode_literals
import copy
from itertools import chain
try:
from urllib.parse import urljoin
except ImportError: # Python 2
from urlparse import urljoin
import warnings
from django.conf import settings
@ -20,6 +16,7 @@ from django.utils.translation import ugettext_lazy
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.safestring import mark_safe
from django.utils import datetime_safe, formats, six
from django.utils.six.moves.urllib.parse import urljoin
__all__ = (
'Media', 'MediaDefiningClass', 'Widget', 'TextInput',

View File

@ -1,11 +1,7 @@
try:
from urllib.parse import urljoin
except ImportError: # Python 2
from urlparse import urljoin
from django import template
from django.template.base import Node
from django.utils.encoding import iri_to_uri
from django.utils.six.moves.urllib.parse import urljoin
register = template.Library()

View File

@ -7,11 +7,6 @@ import mimetypes
from copy import copy
from importlib import import_module
from io import BytesIO
try:
from urllib.parse import unquote, urlparse, urlsplit
except ImportError: # Python 2
from urllib import unquote
from urlparse import urlparse, urlsplit
from django.conf import settings
from django.contrib.auth import authenticate, login, logout, get_user_model
@ -28,6 +23,7 @@ from django.utils.encoding import force_bytes, force_str
from django.utils.http import urlencode
from django.utils.itercompat import is_iterable
from django.utils import six
from django.utils.six.moves.urllib.parse import unquote, urlparse, urlsplit
from django.test.utils import ContextList
__all__ = ('Client', 'RequestFactory', 'encode_file', 'encode_multipart')

View File

@ -13,12 +13,6 @@ import threading
import unittest
from unittest import skipIf # Imported here for backward compatibility
from unittest.util import safe_repr
try:
from urllib.parse import urlsplit, urlunsplit, urlparse, unquote
from urllib.request import url2pathname
except ImportError: # Python 2
from urlparse import urlsplit, urlunsplit, urlparse
from urllib import url2pathname, unquote
from django.conf import settings
from django.core import mail
@ -40,8 +34,10 @@ from django.test.html import HTMLParseError, parse_html
from django.test.signals import template_rendered
from django.test.utils import (CaptureQueriesContext, ContextList,
override_settings, compare_xml)
from django.utils import six
from django.utils.encoding import force_text
from django.utils import six
from django.utils.six.moves.urllib.parse import urlsplit, urlunsplit, urlparse, unquote
from django.utils.six.moves.urllib.request import url2pathname
from django.views.static import serve

View File

@ -4,13 +4,10 @@ import codecs
import datetime
from decimal import Decimal
import locale
try:
from urllib.parse import quote
except ImportError: # Python 2
from urllib import quote
from django.utils.functional import Promise
from django.utils import six
from django.utils.six.moves.urllib.parse import quote
class DjangoUnicodeDecodeError(UnicodeDecodeError):
def __init__(self, obj, *args):

View File

@ -24,15 +24,12 @@ http://web.archive.org/web/20110718035220/http://diveintomark.org/archives/2004/
from __future__ import unicode_literals
import datetime
try:
from urllib.parse import urlparse
except ImportError: # Python 2
from urlparse import urlparse
from django.utils.xmlutils import SimplerXMLGenerator
from django.utils.encoding import force_text, iri_to_uri
from django.utils import datetime_safe
from django.utils import six
from django.utils.six import StringIO
from django.utils.six.moves.urllib.parse import urlparse
from django.utils.timezone import is_aware
def rfc2822_date(date):

View File

@ -3,16 +3,12 @@
from __future__ import unicode_literals
import re
try:
from urllib.parse import quote, unquote, urlsplit, urlunsplit
except ImportError: # Python 2
from urllib import quote, unquote
from urlparse import urlsplit, urlunsplit
from django.utils.safestring import SafeData, mark_safe
from django.utils.encoding import force_text, force_str
from django.utils.functional import allow_lazy
from django.utils import six
from django.utils.six.moves.urllib.parse import quote, unquote, urlsplit, urlunsplit
from django.utils.text import normalize_newlines
from .html_parser import HTMLParser, HTMLParseError

View File

@ -5,12 +5,6 @@ import calendar
import datetime
import re
import sys
try:
from urllib import parse as urllib_parse
except ImportError: # Python 2
import urllib as urllib_parse
import urlparse
urllib_parse.urlparse = urlparse.urlparse
from binascii import Error as BinasciiError
from email.utils import formatdate
@ -19,6 +13,9 @@ from django.utils.datastructures import MultiValueDict
from django.utils.encoding import force_str, force_text
from django.utils.functional import allow_lazy
from django.utils import six
from django.utils.six.moves.urllib.parse import (
quote, quote_plus, unquote, unquote_plus, urlparse,
urlencode as original_urlencode)
ETAG_MATCH = re.compile(r'(?:W/)?"((?:\\.|[^"])*)"')
@ -40,7 +37,7 @@ def urlquote(url, safe='/'):
can safely be used as part of an argument to a subsequent iri_to_uri() call
without double-quoting occurring.
"""
return force_text(urllib_parse.quote(force_str(url), force_str(safe)))
return force_text(quote(force_str(url), force_str(safe)))
urlquote = allow_lazy(urlquote, six.text_type)
def urlquote_plus(url, safe=''):
@ -50,7 +47,7 @@ def urlquote_plus(url, safe=''):
returned string can safely be used as part of an argument to a subsequent
iri_to_uri() call without double-quoting occurring.
"""
return force_text(urllib_parse.quote_plus(force_str(url), force_str(safe)))
return force_text(quote_plus(force_str(url), force_str(safe)))
urlquote_plus = allow_lazy(urlquote_plus, six.text_type)
def urlunquote(quoted_url):
@ -58,7 +55,7 @@ def urlunquote(quoted_url):
A wrapper for Python's urllib.unquote() function that can operate on
the result of django.utils.http.urlquote().
"""
return force_text(urllib_parse.unquote(force_str(quoted_url)))
return force_text(unquote(force_str(quoted_url)))
urlunquote = allow_lazy(urlunquote, six.text_type)
def urlunquote_plus(quoted_url):
@ -66,7 +63,7 @@ def urlunquote_plus(quoted_url):
A wrapper for Python's urllib.unquote_plus() function that can operate on
the result of django.utils.http.urlquote_plus().
"""
return force_text(urllib_parse.unquote_plus(force_str(quoted_url)))
return force_text(unquote_plus(force_str(quoted_url)))
urlunquote_plus = allow_lazy(urlunquote_plus, six.text_type)
def urlencode(query, doseq=0):
@ -79,7 +76,7 @@ def urlencode(query, doseq=0):
query = query.lists()
elif hasattr(query, 'items'):
query = query.items()
return urllib_parse.urlencode(
return original_urlencode(
[(force_str(k),
[force_str(i) for i in v] if isinstance(v, (list,tuple)) else force_str(v))
for k, v in query],
@ -243,7 +240,7 @@ def same_origin(url1, url2):
"""
Checks if two URLs are 'same-origin'
"""
p1, p2 = urllib_parse.urlparse(url1), urllib_parse.urlparse(url2)
p1, p2 = urlparse(url1), urlparse(url2)
try:
return (p1.scheme, p1.hostname, p1.port) == (p2.scheme, p2.hostname, p2.port)
except ValueError:
@ -258,6 +255,6 @@ def is_safe_url(url, host=None):
"""
if not url:
return False
url_info = urllib_parse.urlparse(url)
url_info = urlparse(url)
return (not url_info.netloc or url_info.netloc == host) and \
(not url_info.scheme or url_info.scheme in ['http', 'https'])

View File

@ -9,15 +9,12 @@ import os
import stat
import posixpath
import re
try:
from urllib.parse import unquote
except ImportError: # Python 2
from urllib import unquote
from django.http import (Http404, HttpResponse, HttpResponseRedirect,
HttpResponseNotModified, StreamingHttpResponse)
from django.template import loader, Template, Context, TemplateDoesNotExist
from django.utils.http import http_date, parse_http_date
from django.utils.six.moves.urllib.parse import unquote
from django.utils.translation import ugettext as _, ugettext_noop
def serve(request, path, document_root=None, show_indexes=False):

View File

@ -367,15 +367,6 @@ Moved modules
Some modules were renamed in Python 3. The :mod:`django.utils.six.moves
<six.moves>` module provides a compatible location to import them.
The ``urllib``, ``urllib2`` and ``urlparse`` modules were reworked in depth
and :mod:`django.utils.six.moves <six.moves>` doesn't handle them. Django
explicitly tries both locations, as follows::
try:
from urllib.parse import urlparse, urlunparse
except ImportError: # Python 2
from urlparse import urlparse, urlunparse
PY2
~~~

View File

@ -5,10 +5,6 @@ import os
import re
import datetime
import unittest
try:
from urllib.parse import urljoin
except ImportError: # Python 2
from urlparse import urljoin
from django.conf import settings, global_settings
from django.core import mail
@ -30,15 +26,16 @@ from django.forms.util import ErrorList
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.utils import patch_logger
from django.test.utils import override_settings
from django.utils import formats
from django.utils import translation
from django.utils.cache import get_max_age
from django.utils.encoding import iri_to_uri, force_bytes
from django.utils.html import escape
from django.utils.http import urlencode, urlquote
from django.utils.six.moves.urllib.parse import urljoin
from django.utils._os import upath
from django.utils import six
from django.test.utils import override_settings
# local test models
from .models import (Article, BarAccount, CustomArticle, EmptyModel, FooAccount,

View File

@ -8,10 +8,6 @@ import sys
import tempfile
import time
import unittest
try:
from urllib.request import urlopen
except ImportError: # Python 2
from urllib2 import urlopen
import zlib
from datetime import datetime, timedelta
from io import BytesIO
@ -28,9 +24,10 @@ from django.core.files.images import get_image_dimensions
from django.core.files.storage import FileSystemStorage, get_storage_class
from django.core.files.uploadedfile import UploadedFile
from django.test import LiveServerTestCase, SimpleTestCase
from django.utils import six
from django.utils._os import upath
from django.test.utils import override_settings
from django.utils import six
from django.utils.six.moves.urllib.request import urlopen
from django.utils._os import upath
try:
from django.utils.image import Image

View File

@ -18,6 +18,7 @@ from django.test.client import FakePayload
from django.test.utils import override_settings, str_prefix
from django.utils import six
from django.utils.http import cookie_date, urlencode
from django.utils.six.moves.urllib.parse import urlencode as original_urlencode
from django.utils.timezone import utc
@ -279,8 +280,7 @@ class RequestsTests(SimpleTestCase):
"""
Test a POST with non-utf-8 payload encoding.
"""
from django.utils.http import urllib_parse
payload = FakePayload(urllib_parse.urlencode({'key': 'España'.encode('latin-1')}))
payload = FakePayload(original_urlencode({'key': 'España'.encode('latin-1')}))
request = WSGIRequest({
'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH': len(payload),

View File

@ -5,16 +5,14 @@ Tests for django.core.servers.
from __future__ import unicode_literals
import os
try:
from urllib.request import urlopen, HTTPError
except ImportError: # Python 2
from urllib2 import urlopen, HTTPError
from django.core.exceptions import ImproperlyConfigured
from django.test import LiveServerTestCase
from django.core.servers.basehttp import WSGIServerException
from django.test.utils import override_settings
from django.utils.http import urlencode
from django.utils.six.moves.urllib.error import HTTPError
from django.utils.six.moves.urllib.request import urlopen
from django.utils._os import upath
from .models import Person

View File

@ -5,13 +5,10 @@ django.test.LiveServerTestCase.
"""
import os
try:
from urllib.request import urlopen
except ImportError: # Python 2
from urllib2 import urlopen
from django.core.exceptions import ImproperlyConfigured
from django.test.utils import override_settings
from django.utils.six.moves.urllib.request import urlopen
from django.utils._os import upath
from django.contrib.staticfiles.testing import StaticLiveServerCase

View File

@ -13,10 +13,6 @@ import os
import sys
import traceback
import unittest
try:
from urllib.parse import urljoin
except ImportError: # Python 2
from urlparse import urljoin
import warnings
from django import template
@ -33,6 +29,7 @@ from django.utils._os import upath
from django.utils.translation import activate, deactivate
from django.utils.safestring import mark_safe
from django.utils import six
from django.utils.six.moves.urllib.parse import urljoin
from i18n import TransRealMixin

View File

@ -1,7 +1,3 @@
try:
from urllib.parse import urlencode
except ImportError: # Python 2
from urllib import urlencode
from xml.dom.minidom import parseString
from django.contrib.auth.decorators import login_required, permission_required
@ -13,6 +9,7 @@ from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
from django.shortcuts import render_to_response
from django.template import Context, Template
from django.utils.decorators import method_decorator
from django.utils.six.moves.urllib.parse import urlencode
def get_view(request):
"A simple view that expects a GET request, and returns a rendered template"