Permalink
Browse files

[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.
  • Loading branch information...
1 parent bdca5ea commit 0d914d08a0d7b5a1521f498a8047971fe6cd61e7 @aaugustin aaugustin committed Jul 20, 2012
@@ -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 django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
@@ -21,9 +24,8 @@ def _wrapped_view(request, *args, **kwargs):
path = request.build_absolute_uri()
# If the login url is the same scheme and net location then just
# use the path as the "next" url.
- login_scheme, login_netloc = urlparse.urlparse(login_url or
- settings.LOGIN_URL)[:2]
- current_scheme, current_netloc = urlparse.urlparse(path)[:2]
+ login_scheme, login_netloc = urlparse(login_url or settings.LOGIN_URL)[:2]
+ current_scheme, current_netloc = urlparse(path)[:2]
if ((not login_scheme or login_scheme == current_scheme) and
(not login_netloc or login_netloc == current_netloc)):
path = request.get_full_path()
@@ -1,13 +1,11 @@
from __future__ import unicode_literals
-import urllib
-
from django.core.exceptions import ImproperlyConfigured
from django.core.mail import send_mail
from django.db import models
from django.db.models.manager import EmptyManager
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.translation import ugettext_lazy as _
from django.utils import timezone
@@ -268,7 +266,7 @@ def natural_key(self):
return (self.username,)
def get_absolute_url(self):
- return "/users/%s/" % urllib.quote(smart_str(self.username))
+ return "/users/%s/" % urlquote(self.username)
def is_anonymous(self):
"""
@@ -1,6 +1,5 @@
import os
import re
-import urllib
from django.conf import settings
from django.contrib.sites.models import Site, RequestSite
@@ -10,6 +9,7 @@
from django.http import QueryDict
from django.utils.encoding import force_unicode
from django.utils.html import escape
+from django.utils.http import urlquote
from django.test import TestCase
from django.test.utils import override_settings
@@ -256,7 +256,7 @@ def test_security_check(self, password='password'):
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
'url': login_url,
'next': REDIRECT_FIELD_NAME,
- 'bad_url': urllib.quote(bad_url),
+ 'bad_url': urlquote(bad_url),
}
response = self.client.post(nasty_url, {
'username': 'testclient',
@@ -277,7 +277,7 @@ def test_security_check(self, password='password'):
safe_url = '%(url)s?%(next)s=%(good_url)s' % {
'url': login_url,
'next': REDIRECT_FIELD_NAME,
- 'good_url': urllib.quote(good_url),
+ 'good_url': urlquote(good_url),
}
response = self.client.post(safe_url, {
'username': 'testclient',
@@ -412,7 +412,7 @@ def test_security_check(self, password='password'):
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
'url': logout_url,
'next': REDIRECT_FIELD_NAME,
- 'bad_url': urllib.quote(bad_url),
+ 'bad_url': urlquote(bad_url),
}
self.login()
response = self.client.get(nasty_url)
@@ -432,7 +432,7 @@ def test_security_check(self, password='password'):
safe_url = '%(url)s?%(next)s=%(good_url)s' % {
'url': logout_url,
'next': REDIRECT_FIELD_NAME,
- 'good_url': urllib.quote(good_url),
+ 'good_url': urlquote(good_url),
}
self.login()
response = self.client.get(safe_url)
@@ -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.core.urlresolvers import reverse
@@ -34,7 +37,7 @@ def login(request, template_name='registration/login.html',
if request.method == "POST":
form = authentication_form(data=request.POST)
if form.is_valid():
- netloc = urlparse.urlparse(redirect_to)[1]
+ netloc = urlparse(redirect_to)[1]
# Use default setting if redirect_to is empty
if not redirect_to:
@@ -80,7 +83,7 @@ def logout(request, next_page=None,
auth_logout(request)
redirect_to = request.REQUEST.get(redirect_field_name, '')
if redirect_to:
- netloc = urlparse.urlparse(redirect_to)[1]
+ netloc = urlparse(redirect_to)[1]
# Security check -- don't allow redirection to a different host.
if not (netloc and netloc != request.get_host()):
return HttpResponseRedirect(redirect_to)
@@ -116,13 +119,13 @@ def redirect_to_login(next, login_url=None,
if not 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:
querystring = QueryDict(login_url_parts[4], mutable=True)
querystring[redirect_field_name] = next
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:
# - password_reset sends the mail
@@ -2,8 +2,12 @@
A few bits of helper functions for comment views.
"""
-import urllib
import textwrap
+try:
+ from urllib.parse import urlencode
+except ImportError: # Python 2
+ from urllib import urlencode
+
from django.http import HttpResponseRedirect
from django.core import urlresolvers
from django.shortcuts import render_to_response
@@ -33,7 +37,7 @@ def next_redirect(data, default, default_view, **get_kwargs):
anchor = ''
joiner = ('?' in next) and '&' or '?'
- next += joiner + urllib.urlencode(get_kwargs) + anchor
+ next += joiner + urlencode(get_kwargs) + anchor
return HttpResponseRedirect(next)
def confirmation_view(template, doc="Display a confirmation view."):
@@ -1,14 +1,12 @@
from __future__ import unicode_literals
-import urllib
-
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.views import shortcut
from django.contrib.sites.models import Site
from django.http import HttpRequest, Http404
from django.test import TestCase
-from django.utils.encoding import smart_str
+from django.utils.http import urlquote
from django.utils import six
@@ -36,7 +34,7 @@ class FooWithUrl(FooWithoutUrl):
"""
def get_absolute_url(self):
- return "/users/%s/" % urllib.quote(smart_str(self.name))
+ return "/users/%s/" % urlquote(self.name)
class FooWithBrokenAbsoluteUrl(FooWithoutUrl):
"""
@@ -6,9 +6,10 @@
from django.contrib.databrowse.sites import DatabrowsePlugin
from django.shortcuts import render_to_response
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.encoding import smart_str, force_unicode
-import urllib
+from django.utils.encoding import force_unicode
+
class FieldChoicePlugin(DatabrowsePlugin):
def __init__(self, field_filter=None):
@@ -38,11 +39,10 @@ def model_index_html(self, request, model, site):
def urls(self, plugin_name, easy_instance_field):
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/' % (
easy_instance_field.model.url(),
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):
self.model, self.site = model_databrowse.model, model_databrowse.site
@@ -1,14 +1,18 @@
+try:
+ from urllib.parse import urljoin
+except ImportError: # Python 2
+ from urlparse import urljoin
+
from django import http
from django.contrib.databrowse.datastructures import EasyModel
from django.contrib.databrowse.sites import DatabrowsePlugin
from django.shortcuts import render_to_response
-import urlparse
class ObjectDetailPlugin(DatabrowsePlugin):
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 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)
obj = easy_model.object_by_pk(url)
return render_to_response('databrowse/object_detail.html', {'object': obj, 'root_url': model_databrowse.site.root_url})
@@ -1,7 +1,11 @@
from django.contrib.sites.models import Site
from django.core import urlresolvers, paginator
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"
@@ -32,8 +36,8 @@ def ping_google(sitemap_url=None, ping_url=PING_URL):
from django.contrib.sites.models import Site
current_site = Site.objects.get_current()
url = "http://%s%s" % (current_site.domain, sitemap_url)
- params = urllib.urlencode({'sitemap':url})
- urllib.urlopen("%s?%s" % (ping_url, params))
+ params = urlencode({'sitemap':url})
+ urlopen("%s?%s" % (ping_url, params))
class Sitemap(object):
# This limit is defined by Google. See the index documentation at
@@ -1,5 +1,9 @@
-import urllib
-from urlparse import urlparse
+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.wsgi import WSGIHandler
@@ -42,7 +46,7 @@ def file_path(self, url):
Returns the relative path to the media file on disk for the given URL.
"""
relative_url = url[len(self.base_url[2]):]
- return urllib.url2pathname(relative_url)
+ return url2pathname(relative_url)
def serve(self, request):
"""
@@ -3,8 +3,11 @@
import os
import posixpath
import re
-from urllib import unquote
-from urlparse import urlsplit, urlunsplit, urldefrag
+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,
@@ -5,7 +5,10 @@
"""
import os
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.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 "
"debug mode or if the the --insecure "
"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)
if not absolute_path:
if path.endswith('/') or path == '':
@@ -14,7 +14,10 @@
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.core import signals
@@ -1,6 +1,9 @@
import os
import errno
-import urlparse
+try:
+ from urllib.parse import urljoin
+except ImportError: # Python 2
+ from urlparse import urljoin
import itertools
from datetime import datetime
@@ -252,7 +255,7 @@ def size(self, name):
def url(self, name):
if self.base_url is None:
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):
return datetime.fromtimestamp(os.path.getatime(self.path(name)))
@@ -8,7 +8,10 @@
import stat
import sys
import tempfile
-import urllib
+try:
+ from urllib.request import urlretrieve
+except ImportError: # Python 2
+ from urllib import urlretrieve
from optparse import make_option
from os import path
@@ -227,8 +230,7 @@ def cleanup_url(url):
if self.verbosity >= 2:
self.stdout.write("Downloading %s\n" % display_url)
try:
- the_path, info = urllib.urlretrieve(url,
- path.join(tempdir, filename))
+ the_path, info = urlretrieve(url, path.join(tempdir, filename))
except IOError as e:
raise CommandError("couldn't download URL %s to %s: %s" %
(url, filename, e))
@@ -11,8 +11,11 @@
import socket
import sys
import traceback
-import urllib
-import urlparse
+try:
+ from urllib.parse import unquote, urljoin
+except ImportError: # Python 2
+ from urllib import unquote
+ from urlparse import urljoin
from SocketServer import ThreadingMixIn
from wsgiref import simple_server
from wsgiref.util import FileWrapper # for backwards compatibility
@@ -127,7 +130,7 @@ class WSGIRequestHandler(simple_server.WSGIRequestHandler, object):
def __init__(self, *args, **kwargs):
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
# requests (like "OPTIONS").
self.path = ''
@@ -143,7 +146,7 @@ def get_environ(self):
else:
path,query = self.path,''
- env['PATH_INFO'] = urllib.unquote(path)
+ env['PATH_INFO'] = unquote(path)
env['QUERY_STRING'] = query
env['REMOTE_ADDR'] = self.client_address[0]
env['CONTENT_TYPE'] = self.headers.get('content-type', 'text/plain')
Oops, something went wrong.

0 comments on commit 0d914d0

Please sign in to comment.