1
0
mirror of https://github.com/django/django.git synced 2025-05-30 18:56:32 +00:00

[py3] Updated urllib/urllib2/urlparse imports.

Lots of functions were moved. Use explicit imports in all cases
to keey it easy to identify where the functions come from.
This commit is contained in:
Aymeric Augustin 2012-07-20 15:36:52 +02:00
parent bdca5ea345
commit 0d914d08a0
32 changed files with 181 additions and 96 deletions

View File

@ -1,4 +1,7 @@
import urlparse try:
from urllib.parse import urlparse
except ImportError: # Python 2
from urlparse import urlparse
from functools import wraps from functools import wraps
from django.conf import settings from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth import REDIRECT_FIELD_NAME
@ -21,9 +24,8 @@ def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIE
path = request.build_absolute_uri() path = request.build_absolute_uri()
# If the login url is the same scheme and net location then just # If the login url is the same scheme and net location then just
# use the path as the "next" url. # use the path as the "next" url.
login_scheme, login_netloc = urlparse.urlparse(login_url or login_scheme, login_netloc = urlparse(login_url or settings.LOGIN_URL)[:2]
settings.LOGIN_URL)[:2] current_scheme, current_netloc = urlparse(path)[:2]
current_scheme, current_netloc = urlparse.urlparse(path)[:2]
if ((not login_scheme or login_scheme == current_scheme) and if ((not login_scheme or login_scheme == current_scheme) and
(not login_netloc or login_netloc == current_netloc)): (not login_netloc or login_netloc == current_netloc)):
path = request.get_full_path() path = request.get_full_path()

View File

@ -1,13 +1,11 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import urllib
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.core.mail import send_mail from django.core.mail import send_mail
from django.db import models from django.db import models
from django.db.models.manager import EmptyManager from django.db.models.manager import EmptyManager
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
from django.utils.encoding import smart_str from django.utils.http import urlquote
from django.utils import six from django.utils import six
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils import timezone from django.utils import timezone
@ -268,7 +266,7 @@ class User(models.Model):
return (self.username,) return (self.username,)
def get_absolute_url(self): def get_absolute_url(self):
return "/users/%s/" % urllib.quote(smart_str(self.username)) return "/users/%s/" % urlquote(self.username)
def is_anonymous(self): def is_anonymous(self):
""" """

View File

@ -1,6 +1,5 @@
import os import os
import re import re
import urllib
from django.conf import settings from django.conf import settings
from django.contrib.sites.models import Site, RequestSite from django.contrib.sites.models import Site, RequestSite
@ -10,6 +9,7 @@ from django.core.urlresolvers import reverse, NoReverseMatch
from django.http import QueryDict from django.http import QueryDict
from django.utils.encoding import force_unicode from django.utils.encoding import force_unicode
from django.utils.html import escape from django.utils.html import escape
from django.utils.http import urlquote
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings from django.test.utils import override_settings
@ -256,7 +256,7 @@ class LoginTest(AuthViewsTestCase):
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % { nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
'url': login_url, 'url': login_url,
'next': REDIRECT_FIELD_NAME, 'next': REDIRECT_FIELD_NAME,
'bad_url': urllib.quote(bad_url), 'bad_url': urlquote(bad_url),
} }
response = self.client.post(nasty_url, { response = self.client.post(nasty_url, {
'username': 'testclient', 'username': 'testclient',
@ -277,7 +277,7 @@ class LoginTest(AuthViewsTestCase):
safe_url = '%(url)s?%(next)s=%(good_url)s' % { safe_url = '%(url)s?%(next)s=%(good_url)s' % {
'url': login_url, 'url': login_url,
'next': REDIRECT_FIELD_NAME, 'next': REDIRECT_FIELD_NAME,
'good_url': urllib.quote(good_url), 'good_url': urlquote(good_url),
} }
response = self.client.post(safe_url, { response = self.client.post(safe_url, {
'username': 'testclient', 'username': 'testclient',
@ -412,7 +412,7 @@ class LogoutTest(AuthViewsTestCase):
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % { nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
'url': logout_url, 'url': logout_url,
'next': REDIRECT_FIELD_NAME, 'next': REDIRECT_FIELD_NAME,
'bad_url': urllib.quote(bad_url), 'bad_url': urlquote(bad_url),
} }
self.login() self.login()
response = self.client.get(nasty_url) response = self.client.get(nasty_url)
@ -432,7 +432,7 @@ class LogoutTest(AuthViewsTestCase):
safe_url = '%(url)s?%(next)s=%(good_url)s' % { safe_url = '%(url)s?%(next)s=%(good_url)s' % {
'url': logout_url, 'url': logout_url,
'next': REDIRECT_FIELD_NAME, 'next': REDIRECT_FIELD_NAME,
'good_url': urllib.quote(good_url), 'good_url': urlquote(good_url),
} }
self.login() self.login()
response = self.client.get(safe_url) response = self.client.get(safe_url)

View File

@ -1,4 +1,7 @@
import urlparse try:
from urllib.parse import urlparse, urlunparse
except ImportError: # Python 2
from urlparse import urlparse, urlunparse
from django.conf import settings from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
@ -34,7 +37,7 @@ def login(request, template_name='registration/login.html',
if request.method == "POST": if request.method == "POST":
form = authentication_form(data=request.POST) form = authentication_form(data=request.POST)
if form.is_valid(): if form.is_valid():
netloc = urlparse.urlparse(redirect_to)[1] netloc = urlparse(redirect_to)[1]
# Use default setting if redirect_to is empty # Use default setting if redirect_to is empty
if not redirect_to: if not redirect_to:
@ -80,7 +83,7 @@ def logout(request, next_page=None,
auth_logout(request) auth_logout(request)
redirect_to = request.REQUEST.get(redirect_field_name, '') redirect_to = request.REQUEST.get(redirect_field_name, '')
if redirect_to: if redirect_to:
netloc = urlparse.urlparse(redirect_to)[1] netloc = urlparse(redirect_to)[1]
# Security check -- don't allow redirection to a different host. # Security check -- don't allow redirection to a different host.
if not (netloc and netloc != request.get_host()): if not (netloc and netloc != request.get_host()):
return HttpResponseRedirect(redirect_to) return HttpResponseRedirect(redirect_to)
@ -116,13 +119,13 @@ def redirect_to_login(next, login_url=None,
if not login_url: if not login_url:
login_url = settings.LOGIN_URL login_url = settings.LOGIN_URL
login_url_parts = list(urlparse.urlparse(login_url)) login_url_parts = list(urlparse(login_url))
if redirect_field_name: if redirect_field_name:
querystring = QueryDict(login_url_parts[4], mutable=True) querystring = QueryDict(login_url_parts[4], mutable=True)
querystring[redirect_field_name] = next querystring[redirect_field_name] = next
login_url_parts[4] = querystring.urlencode(safe='/') login_url_parts[4] = querystring.urlencode(safe='/')
return HttpResponseRedirect(urlparse.urlunparse(login_url_parts)) return HttpResponseRedirect(urlunparse(login_url_parts))
# 4 views for password reset: # 4 views for password reset:
# - password_reset sends the mail # - password_reset sends the mail

View File

@ -2,8 +2,12 @@
A few bits of helper functions for comment views. A few bits of helper functions for comment views.
""" """
import urllib
import textwrap import textwrap
try:
from urllib.parse import urlencode
except ImportError: # Python 2
from urllib import urlencode
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.core import urlresolvers from django.core import urlresolvers
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
@ -33,7 +37,7 @@ def next_redirect(data, default, default_view, **get_kwargs):
anchor = '' anchor = ''
joiner = ('?' in next) and '&' or '?' joiner = ('?' in next) and '&' or '?'
next += joiner + urllib.urlencode(get_kwargs) + anchor next += joiner + urlencode(get_kwargs) + anchor
return HttpResponseRedirect(next) return HttpResponseRedirect(next)
def confirmation_view(template, doc="Display a confirmation view."): def confirmation_view(template, doc="Display a confirmation view."):

View File

@ -1,14 +1,12 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import urllib
from django.db import models from django.db import models
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.views import shortcut from django.contrib.contenttypes.views import shortcut
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.http import HttpRequest, Http404 from django.http import HttpRequest, Http404
from django.test import TestCase from django.test import TestCase
from django.utils.encoding import smart_str from django.utils.http import urlquote
from django.utils import six from django.utils import six
@ -36,7 +34,7 @@ class FooWithUrl(FooWithoutUrl):
""" """
def get_absolute_url(self): def get_absolute_url(self):
return "/users/%s/" % urllib.quote(smart_str(self.name)) return "/users/%s/" % urlquote(self.name)
class FooWithBrokenAbsoluteUrl(FooWithoutUrl): class FooWithBrokenAbsoluteUrl(FooWithoutUrl):
""" """

View File

@ -6,9 +6,10 @@ from django.contrib.databrowse.datastructures import EasyModel
from django.contrib.databrowse.sites import DatabrowsePlugin from django.contrib.databrowse.sites import DatabrowsePlugin
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from django.utils.html import format_html, format_html_join from django.utils.html import format_html, format_html_join
from django.utils.http import urlquote
from django.utils.text import capfirst from django.utils.text import capfirst
from django.utils.encoding import smart_str, force_unicode from django.utils.encoding import force_unicode
import urllib
class FieldChoicePlugin(DatabrowsePlugin): class FieldChoicePlugin(DatabrowsePlugin):
def __init__(self, field_filter=None): def __init__(self, field_filter=None):
@ -38,11 +39,10 @@ class FieldChoicePlugin(DatabrowsePlugin):
def urls(self, plugin_name, easy_instance_field): def urls(self, plugin_name, easy_instance_field):
if easy_instance_field.field in self.field_dict(easy_instance_field.model.model).values(): if easy_instance_field.field in self.field_dict(easy_instance_field.model.model).values():
field_value = smart_str(easy_instance_field.raw_value)
return ['%s%s/%s/%s/' % ( return ['%s%s/%s/%s/' % (
easy_instance_field.model.url(), easy_instance_field.model.url(),
plugin_name, easy_instance_field.field.name, plugin_name, easy_instance_field.field.name,
urllib.quote(field_value, safe=''))] urlquote(easy_instance_field.raw_value, safe=''))]
def model_view(self, request, model_databrowse, url): def model_view(self, request, model_databrowse, url):
self.model, self.site = model_databrowse.model, model_databrowse.site self.model, self.site = model_databrowse.model, model_databrowse.site

View File

@ -1,14 +1,18 @@
try:
from urllib.parse import urljoin
except ImportError: # Python 2
from urlparse import urljoin
from django import http from django import http
from django.contrib.databrowse.datastructures import EasyModel from django.contrib.databrowse.datastructures import EasyModel
from django.contrib.databrowse.sites import DatabrowsePlugin from django.contrib.databrowse.sites import DatabrowsePlugin
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
import urlparse
class ObjectDetailPlugin(DatabrowsePlugin): class ObjectDetailPlugin(DatabrowsePlugin):
def model_view(self, request, model_databrowse, url): def model_view(self, request, model_databrowse, url):
# If the object ID wasn't provided, redirect to the model page, which is one level up. # If the object ID wasn't provided, redirect to the model page, which is one level up.
if url is None: if url is None:
return http.HttpResponseRedirect(urlparse.urljoin(request.path, '../')) return http.HttpResponseRedirect(urljoin(request.path, '../'))
easy_model = EasyModel(model_databrowse.site, model_databrowse.model) easy_model = EasyModel(model_databrowse.site, model_databrowse.model)
obj = easy_model.object_by_pk(url) obj = easy_model.object_by_pk(url)
return render_to_response('databrowse/object_detail.html', {'object': obj, 'root_url': model_databrowse.site.root_url}) return render_to_response('databrowse/object_detail.html', {'object': obj, 'root_url': model_databrowse.site.root_url})

View File

@ -1,7 +1,11 @@
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core import urlresolvers, paginator from django.core import urlresolvers, paginator
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
import urllib try:
from urllib.parse import urlencode
from urllib.request import urlopen
except ImportError: # Python 2
from urllib import urlencode, urlopen
PING_URL = "http://www.google.com/webmasters/tools/ping" PING_URL = "http://www.google.com/webmasters/tools/ping"
@ -32,8 +36,8 @@ def ping_google(sitemap_url=None, ping_url=PING_URL):
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
current_site = Site.objects.get_current() current_site = Site.objects.get_current()
url = "http://%s%s" % (current_site.domain, sitemap_url) url = "http://%s%s" % (current_site.domain, sitemap_url)
params = urllib.urlencode({'sitemap':url}) params = urlencode({'sitemap':url})
urllib.urlopen("%s?%s" % (ping_url, params)) urlopen("%s?%s" % (ping_url, params))
class Sitemap(object): class Sitemap(object):
# This limit is defined by Google. See the index documentation at # This limit is defined by Google. See the index documentation at

View File

@ -1,5 +1,9 @@
import urllib try:
from urlparse import urlparse 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.conf import settings
from django.core.handlers.wsgi import WSGIHandler from django.core.handlers.wsgi import WSGIHandler
@ -42,7 +46,7 @@ class StaticFilesHandler(WSGIHandler):
Returns the relative path to the media file on disk for the given URL. Returns the relative path to the media file on disk for the given URL.
""" """
relative_url = url[len(self.base_url[2]):] relative_url = url[len(self.base_url[2]):]
return urllib.url2pathname(relative_url) return url2pathname(relative_url)
def serve(self, request): def serve(self, request):
""" """

View File

@ -3,8 +3,11 @@ import hashlib
import os import os
import posixpath import posixpath
import re import re
from urllib import unquote try:
from urlparse import urlsplit, urlunsplit, urldefrag 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.conf import settings
from django.core.cache import (get_cache, InvalidCacheBackendError, from django.core.cache import (get_cache, InvalidCacheBackendError,

View File

@ -5,7 +5,10 @@ development, and SHOULD NOT be used in a production setting.
""" """
import os import os
import posixpath import posixpath
import urllib try:
from urllib.parse import unquote
except ImportError: # Python 2
from urllib import unquote
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
@ -31,7 +34,7 @@ def serve(request, path, document_root=None, insecure=False, **kwargs):
raise ImproperlyConfigured("The staticfiles view can only be used in " raise ImproperlyConfigured("The staticfiles view can only be used in "
"debug mode or if the the --insecure " "debug mode or if the the --insecure "
"option of 'runserver' is used") "option of 'runserver' is used")
normalized_path = posixpath.normpath(urllib.unquote(path)).lstrip('/') normalized_path = posixpath.normpath(unquote(path)).lstrip('/')
absolute_path = finders.find(normalized_path) absolute_path = finders.find(normalized_path)
if not absolute_path: if not absolute_path:
if path.endswith('/') or path == '': if path.endswith('/') or path == '':

View File

@ -14,7 +14,10 @@ cache class.
See docs/topics/cache.txt for information on the public API. See docs/topics/cache.txt for information on the public API.
""" """
from urlparse import parse_qsl try:
from urllib.parse import parse_qsl
except ImportError: # Python 2
from urlparse import parse_qsl
from django.conf import settings from django.conf import settings
from django.core import signals from django.core import signals

View File

@ -1,6 +1,9 @@
import os import os
import errno import errno
import urlparse try:
from urllib.parse import urljoin
except ImportError: # Python 2
from urlparse import urljoin
import itertools import itertools
from datetime import datetime from datetime import datetime
@ -252,7 +255,7 @@ class FileSystemStorage(Storage):
def url(self, name): def url(self, name):
if self.base_url is None: if self.base_url is None:
raise ValueError("This file is not accessible via a URL.") raise ValueError("This file is not accessible via a URL.")
return urlparse.urljoin(self.base_url, filepath_to_uri(name)) return urljoin(self.base_url, filepath_to_uri(name))
def accessed_time(self, name): def accessed_time(self, name):
return datetime.fromtimestamp(os.path.getatime(self.path(name))) return datetime.fromtimestamp(os.path.getatime(self.path(name)))

View File

@ -8,7 +8,10 @@ import shutil
import stat import stat
import sys import sys
import tempfile import tempfile
import urllib try:
from urllib.request import urlretrieve
except ImportError: # Python 2
from urllib import urlretrieve
from optparse import make_option from optparse import make_option
from os import path from os import path
@ -227,8 +230,7 @@ class TemplateCommand(BaseCommand):
if self.verbosity >= 2: if self.verbosity >= 2:
self.stdout.write("Downloading %s\n" % display_url) self.stdout.write("Downloading %s\n" % display_url)
try: try:
the_path, info = urllib.urlretrieve(url, the_path, info = urlretrieve(url, path.join(tempdir, filename))
path.join(tempdir, filename))
except IOError as e: except IOError as e:
raise CommandError("couldn't download URL %s to %s: %s" % raise CommandError("couldn't download URL %s to %s: %s" %
(url, filename, e)) (url, filename, e))

View File

@ -11,8 +11,11 @@ import os
import socket import socket
import sys import sys
import traceback import traceback
import urllib try:
import urlparse from urllib.parse import unquote, urljoin
except ImportError: # Python 2
from urllib import unquote
from urlparse import urljoin
from SocketServer import ThreadingMixIn from SocketServer import ThreadingMixIn
from wsgiref import simple_server from wsgiref import simple_server
from wsgiref.util import FileWrapper # for backwards compatibility from wsgiref.util import FileWrapper # for backwards compatibility
@ -127,7 +130,7 @@ class WSGIRequestHandler(simple_server.WSGIRequestHandler, object):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
from django.conf import settings from django.conf import settings
self.admin_static_prefix = urlparse.urljoin(settings.STATIC_URL, 'admin/') self.admin_static_prefix = urljoin(settings.STATIC_URL, 'admin/')
# We set self.path to avoid crashes in log_message() on unsupported # We set self.path to avoid crashes in log_message() on unsupported
# requests (like "OPTIONS"). # requests (like "OPTIONS").
self.path = '' self.path = ''
@ -143,7 +146,7 @@ class WSGIRequestHandler(simple_server.WSGIRequestHandler, object):
else: else:
path,query = self.path,'' path,query = self.path,''
env['PATH_INFO'] = urllib.unquote(path) env['PATH_INFO'] = unquote(path)
env['QUERY_STRING'] = query env['QUERY_STRING'] = query
env['REMOTE_ADDR'] = self.client_address[0] env['REMOTE_ADDR'] = self.client_address[0]
env['CONTENT_TYPE'] = self.headers.get('content-type', 'text/plain') env['CONTENT_TYPE'] = self.headers.get('content-type', 'text/plain')

View File

@ -1,7 +1,10 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import re import re
import urlparse try:
from urllib.parse import urlsplit, urlunsplit
except ImportError: # Python 2
from urlparse import urlsplit, urlunsplit
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -52,12 +55,12 @@ class URLValidator(RegexValidator):
# Trivial case failed. Try for possible IDN domain # Trivial case failed. Try for possible IDN domain
if value: if value:
value = smart_unicode(value) value = smart_unicode(value)
scheme, netloc, path, query, fragment = urlparse.urlsplit(value) scheme, netloc, path, query, fragment = urlsplit(value)
try: try:
netloc = netloc.encode('idna') # IDN -> ACE netloc = netloc.encode('idna') # IDN -> ACE
except UnicodeError: # invalid domain part except UnicodeError: # invalid domain part
raise e raise e
url = urlparse.urlunsplit((scheme, netloc, path, query, fragment)) url = urlunsplit((scheme, netloc, path, query, fragment))
super(URLValidator, self).__call__(url) super(URLValidator, self).__call__(url)
else: else:
raise raise

View File

@ -8,7 +8,10 @@ import copy
import datetime import datetime
import os import os
import re import re
import urlparse try:
from urllib.parse import urlsplit, urlunsplit
except ImportError: # Python 2
from urlparse import urlsplit, urlunsplit
from decimal import Decimal, DecimalException from decimal import Decimal, DecimalException
from io import BytesIO from io import BytesIO
@ -599,7 +602,7 @@ class URLField(CharField):
``ValidationError`` exception for certain). ``ValidationError`` exception for certain).
""" """
try: try:
return list(urlparse.urlsplit(url)) return list(urlsplit(url))
except ValueError: except ValueError:
# urlparse.urlsplit can raise a ValueError with some # urlparse.urlsplit can raise a ValueError with some
# misformatted URLs. # misformatted URLs.
@ -618,11 +621,11 @@ class URLField(CharField):
url_fields[2] = '' url_fields[2] = ''
# Rebuild the url_fields list, since the domain segment may now # Rebuild the url_fields list, since the domain segment may now
# contain the path too. # contain the path too.
url_fields = split_url(urlparse.urlunsplit(url_fields)) url_fields = split_url(urlunsplit(url_fields))
if not url_fields[2]: if not url_fields[2]:
# the path portion may need to be added before query params # the path portion may need to be added before query params
url_fields[2] = '/' url_fields[2] = '/'
value = urlparse.urlunsplit(url_fields) value = urlunsplit(url_fields)
return value return value
class BooleanField(Field): class BooleanField(Field):

View File

@ -7,7 +7,10 @@ from __future__ import absolute_import, unicode_literals
import copy import copy
import datetime import datetime
from itertools import chain from itertools import chain
from urlparse import urljoin try:
from urllib.parse import urljoin
except ImportError: # Python 2
from urlparse import urljoin
from django.conf import settings from django.conf import settings
from django.forms.util import flatatt, to_current_timezone from django.forms.util import flatatt, to_current_timezone

View File

@ -10,8 +10,11 @@ import warnings
from io import BytesIO from io import BytesIO
from pprint import pformat from pprint import pformat
from urllib import urlencode, quote try:
from urlparse import urljoin, parse_qsl from urllib.parse import quote, parse_qsl, urlencode, urljoin
except ImportError: # Python 2
from urllib import quote, urlencode
from urlparse import parse_qsl, urljoin
import Cookie import Cookie
# Some versions of Python 2.7 and later won't need this encoding bug fix: # Some versions of Python 2.7 and later won't need this encoding bug fix:

View File

@ -1,4 +1,8 @@
from urlparse import urljoin try:
from urllib.parse import urljoin
except ImportError: # Python 2
from urlparse import urljoin
from django import template from django import template
from django.template.base import Node from django.template.base import Node
from django.utils.encoding import iri_to_uri from django.utils.encoding import iri_to_uri

View File

@ -1,11 +1,14 @@
import urllib
import sys import sys
import os import os
import re import re
import mimetypes import mimetypes
from copy import copy from copy import copy
from io import BytesIO from io import BytesIO
from urlparse import urlparse, urlsplit 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.conf import settings
from django.contrib.auth import authenticate, login from django.contrib.auth import authenticate, login
@ -222,9 +225,9 @@ class RequestFactory(object):
def _get_path(self, parsed): def _get_path(self, parsed):
# If there are parameters, add them # If there are parameters, add them
if parsed[3]: if parsed[3]:
return urllib.unquote(parsed[2] + ";" + parsed[3]) return unquote(parsed[2] + ";" + parsed[3])
else: else:
return urllib.unquote(parsed[2]) return unquote(parsed[2])
def get(self, path, data={}, **extra): def get(self, path, data={}, **extra):
"Construct a GET request." "Construct a GET request."

View File

@ -7,7 +7,10 @@ import re
import sys import sys
from copy import copy from copy import copy
from functools import wraps from functools import wraps
from urlparse import urlsplit, urlunsplit try:
from urllib.parse import urlsplit, urlunsplit
except ImportError: # Python 2
from urlparse import urlsplit, urlunsplit
from xml.dom.minidom import parseString, Node from xml.dom.minidom import parseString, Node
import select import select
import socket import socket

View File

@ -1,10 +1,13 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import urllib
import locale
import datetime
import codecs import codecs
import datetime
from decimal import Decimal 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.functional import Promise
from django.utils import six from django.utils import six
@ -165,7 +168,7 @@ def iri_to_uri(iri):
# converted. # converted.
if iri is None: if iri is None:
return iri return iri
return urllib.quote(smart_str(iri), safe=b"/#%[]=:;$&()+,!?*@'~") return quote(smart_str(iri), safe=b"/#%[]=:;$&()+,!?*@'~")
def filepath_to_uri(path): def filepath_to_uri(path):
"""Convert an file system path to a URI portion that is suitable for """Convert an file system path to a URI portion that is suitable for
@ -184,7 +187,7 @@ def filepath_to_uri(path):
return path return path
# I know about `os.sep` and `os.altsep` but I want to leave # I know about `os.sep` and `os.altsep` but I want to leave
# some flexibility for hardcoding separators. # some flexibility for hardcoding separators.
return urllib.quote(smart_str(path).replace("\\", "/"), safe=b"/~!*()'") return quote(smart_str(path).replace("\\", "/"), safe=b"/~!*()'")
# The encoding of the default system locale but falls back to the # The encoding of the default system locale but falls back to the
# given fallback encoding if the encoding is unsupported by python or could # given fallback encoding if the encoding is unsupported by python or could

View File

@ -24,7 +24,10 @@ http://web.archive.org/web/20110718035220/http://diveintomark.org/archives/2004/
from __future__ import unicode_literals from __future__ import unicode_literals
import datetime import datetime
import urlparse try:
from urllib.parse import urlparse
except ImportError: # Python 2
from urlparse import urlparse
from django.utils.xmlutils import SimplerXMLGenerator from django.utils.xmlutils import SimplerXMLGenerator
from django.utils.encoding import force_unicode, iri_to_uri from django.utils.encoding import force_unicode, iri_to_uri
from django.utils import datetime_safe from django.utils import datetime_safe
@ -67,7 +70,7 @@ def get_tag_uri(url, date):
See http://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id See http://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id
""" """
bits = urlparse.urlparse(url) bits = urlparse(url)
d = '' d = ''
if date is not None: if date is not None:
d = ',%s' % datetime_safe.new_datetime(date).strftime('%Y-%m-%d') d = ',%s' % datetime_safe.new_datetime(date).strftime('%Y-%m-%d')

View File

@ -4,8 +4,11 @@ from __future__ import unicode_literals
import re import re
import string import string
import urllib try:
import urlparse from urllib.parse import quote, urlsplit, urlunsplit
except ImportError: # Python 2
from urllib import quote
from urlparse import urlsplit, urlunsplit
from django.utils.safestring import SafeData, mark_safe from django.utils.safestring import SafeData, mark_safe
from django.utils.encoding import smart_str, force_unicode from django.utils.encoding import smart_str, force_unicode
@ -138,19 +141,19 @@ fix_ampersands = allow_lazy(fix_ampersands, six.text_type)
def smart_urlquote(url): def smart_urlquote(url):
"Quotes a URL if it isn't already quoted." "Quotes a URL if it isn't already quoted."
# Handle IDN before quoting. # Handle IDN before quoting.
scheme, netloc, path, query, fragment = urlparse.urlsplit(url) scheme, netloc, path, query, fragment = urlsplit(url)
try: try:
netloc = netloc.encode('idna') # IDN -> ACE netloc = netloc.encode('idna') # IDN -> ACE
except UnicodeError: # invalid domain part except UnicodeError: # invalid domain part
pass pass
else: else:
url = urlparse.urlunsplit((scheme, netloc, path, query, fragment)) url = urlunsplit((scheme, netloc, path, query, fragment))
# An URL is considered unquoted if it contains no % characters or # An URL is considered unquoted if it contains no % characters or
# contains a % not followed by two hexadecimal digits. See #9655. # contains a % not followed by two hexadecimal digits. See #9655.
if '%' not in url or unquoted_percents_re.search(url): if '%' not in url or unquoted_percents_re.search(url):
# See http://bugs.python.org/issue2637 # See http://bugs.python.org/issue2637
url = urllib.quote(smart_str(url), safe=b'!*\'();:@&=+$,/?#[]~') url = quote(smart_str(url), safe=b'!*\'();:@&=+$,/?#[]~')
return force_unicode(url) return force_unicode(url)

View File

@ -2,8 +2,14 @@ import calendar
import datetime import datetime
import re import re
import sys import sys
import urllib try:
import urlparse from urllib import parse as urllib_parse
except ImportError: # Python 2
import urllib as urllib_parse
import urlparse
urllib_parse.urlparse = urlparse.urlparse
from email.utils import formatdate from email.utils import formatdate
from django.utils.datastructures import MultiValueDict from django.utils.datastructures import MultiValueDict
@ -31,7 +37,7 @@ def urlquote(url, safe='/'):
can safely be used as part of an argument to a subsequent iri_to_uri() call can safely be used as part of an argument to a subsequent iri_to_uri() call
without double-quoting occurring. without double-quoting occurring.
""" """
return force_unicode(urllib.quote(smart_str(url), smart_str(safe))) return force_unicode(urllib_parse.quote(smart_str(url), smart_str(safe)))
urlquote = allow_lazy(urlquote, six.text_type) urlquote = allow_lazy(urlquote, six.text_type)
def urlquote_plus(url, safe=''): def urlquote_plus(url, safe=''):
@ -41,7 +47,7 @@ def urlquote_plus(url, safe=''):
returned string can safely be used as part of an argument to a subsequent returned string can safely be used as part of an argument to a subsequent
iri_to_uri() call without double-quoting occurring. iri_to_uri() call without double-quoting occurring.
""" """
return force_unicode(urllib.quote_plus(smart_str(url), smart_str(safe))) return force_unicode(urllib_parse.quote_plus(smart_str(url), smart_str(safe)))
urlquote_plus = allow_lazy(urlquote_plus, six.text_type) urlquote_plus = allow_lazy(urlquote_plus, six.text_type)
def urlunquote(quoted_url): def urlunquote(quoted_url):
@ -49,7 +55,7 @@ def urlunquote(quoted_url):
A wrapper for Python's urllib.unquote() function that can operate on A wrapper for Python's urllib.unquote() function that can operate on
the result of django.utils.http.urlquote(). the result of django.utils.http.urlquote().
""" """
return force_unicode(urllib.unquote(smart_str(quoted_url))) return force_unicode(urllib_parse.unquote(smart_str(quoted_url)))
urlunquote = allow_lazy(urlunquote, six.text_type) urlunquote = allow_lazy(urlunquote, six.text_type)
def urlunquote_plus(quoted_url): def urlunquote_plus(quoted_url):
@ -57,7 +63,7 @@ def urlunquote_plus(quoted_url):
A wrapper for Python's urllib.unquote_plus() function that can operate on A wrapper for Python's urllib.unquote_plus() function that can operate on
the result of django.utils.http.urlquote_plus(). the result of django.utils.http.urlquote_plus().
""" """
return force_unicode(urllib.unquote_plus(smart_str(quoted_url))) return force_unicode(urllib_parse.unquote_plus(smart_str(quoted_url)))
urlunquote_plus = allow_lazy(urlunquote_plus, six.text_type) urlunquote_plus = allow_lazy(urlunquote_plus, six.text_type)
def urlencode(query, doseq=0): def urlencode(query, doseq=0):
@ -70,7 +76,7 @@ def urlencode(query, doseq=0):
query = query.lists() query = query.lists()
elif hasattr(query, 'items'): elif hasattr(query, 'items'):
query = query.items() query = query.items()
return urllib.urlencode( return urllib_parse.urlencode(
[(smart_str(k), [(smart_str(k),
[smart_str(i) for i in v] if isinstance(v, (list,tuple)) else smart_str(v)) [smart_str(i) for i in v] if isinstance(v, (list,tuple)) else smart_str(v))
for k, v in query], for k, v in query],
@ -212,5 +218,5 @@ def same_origin(url1, url2):
""" """
Checks if two URLs are 'same-origin' Checks if two URLs are 'same-origin'
""" """
p1, p2 = urlparse.urlparse(url1), urlparse.urlparse(url2) p1, p2 = urllib_parse.urlparse(url1), urllib_parse.urlparse(url2)
return (p1.scheme, p1.hostname, p1.port) == (p2.scheme, p2.hostname, p2.port) return (p1.scheme, p1.hostname, p1.port) == (p2.scheme, p2.hostname, p2.port)

View File

@ -9,7 +9,10 @@ import os
import stat import stat
import posixpath import posixpath
import re import re
import urllib try:
from urllib.parse import unquote
except ImportError: # Python 2
from urllib import unquote
from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseNotModified from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseNotModified
from django.template import loader, Template, Context, TemplateDoesNotExist from django.template import loader, Template, Context, TemplateDoesNotExist
@ -30,7 +33,7 @@ def serve(request, path, document_root=None, show_indexes=False):
but if you'd like to override it, you can create a template called but if you'd like to override it, you can create a template called
``static/directory_index.html``. ``static/directory_index.html``.
""" """
path = posixpath.normpath(urllib.unquote(path)) path = posixpath.normpath(unquote(path))
path = path.lstrip('/') path = path.lstrip('/')
newpath = '' newpath = ''
for part in path.split('/'): for part in path.split('/'):

View File

@ -1,3 +1,7 @@
try:
from urllib.parse import urlencode
except ImportError: # Python 2
from urllib import urlencode
from xml.dom.minidom import parseString from xml.dom.minidom import parseString
from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.decorators import login_required, permission_required
@ -9,7 +13,6 @@ from django.shortcuts import render_to_response
from django.template import Context, Template from django.template import Context, Template
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
def get_view(request): def get_view(request):
"A simple view that expects a GET request, and returns a rendered template" "A simple view that expects a GET request, and returns a rendered template"
t = Template('This is a test. {{ var }} is the value.', name='GET Template') t = Template('This is a test. {{ var }} is the value.', name='GET Template')
@ -58,7 +61,6 @@ def raw_post_view(request):
def redirect_view(request): def redirect_view(request):
"A view that redirects all requests to the GET view" "A view that redirects all requests to the GET view"
if request.GET: if request.GET:
from urllib import urlencode
query = '?' + urlencode(request.GET, True) query = '?' + urlencode(request.GET, True)
else: else:
query = '' query = ''

View File

@ -4,7 +4,10 @@ from __future__ import absolute_import, unicode_literals
import os import os
import re import re
import datetime import datetime
import urlparse try:
from urllib.parse import urljoin
except ImportError: # Python 2
from urlparse import urljoin
from django.conf import settings, global_settings from django.conf import settings, global_settings
from django.core import mail from django.core import mail
@ -3199,7 +3202,7 @@ class RawIdFieldsTest(TestCase):
popup_url = m.groups()[0].replace("&", "&") popup_url = m.groups()[0].replace("&", "&")
# Handle relative links # Handle relative links
popup_url = urlparse.urljoin(response.request['PATH_INFO'], popup_url) popup_url = urljoin(response.request['PATH_INFO'], popup_url)
# Get the popup # Get the popup
response2 = self.client.get(popup_url) response2 = self.client.get(popup_url)
self.assertContains(response2, "Spain") self.assertContains(response2, "Spain")

View File

@ -2,7 +2,10 @@
Tests for django.core.servers. Tests for django.core.servers.
""" """
import os import os
import urllib2 try:
from urllib.request import urlopen, HTTPError
except ImportError: # Python 2
from urllib2 import urlopen, HTTPError
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.test import LiveServerTestCase from django.test import LiveServerTestCase
@ -39,7 +42,7 @@ class LiveServerBase(LiveServerTestCase):
super(LiveServerBase, cls).tearDownClass() super(LiveServerBase, cls).tearDownClass()
def urlopen(self, url): def urlopen(self, url):
return urllib2.urlopen(self.live_server_url + url) return urlopen(self.live_server_url + url)
class LiveServerAddress(LiveServerBase): class LiveServerAddress(LiveServerBase):
@ -102,7 +105,7 @@ class LiveServerViews(LiveServerBase):
""" """
try: try:
self.urlopen('/') self.urlopen('/')
except urllib2.HTTPError as err: except HTTPError as err:
self.assertEqual(err.code, 404, 'Expected 404 response') self.assertEqual(err.code, 404, 'Expected 404 response')
else: else:
self.fail('Expected 404 response') self.fail('Expected 404 response')

View File

@ -13,7 +13,10 @@ import time
import os import os
import sys import sys
import traceback import traceback
from urlparse import urljoin try:
from urllib.parse import urljoin
except ImportError: # Python 2
from urlparse import urljoin
from django import template from django import template
from django.template import base as template_base, RequestContext, Template, Context from django.template import base as template_base, RequestContext, Template, Context