Skip to content

Commit 0d914d0

Browse files
committed
[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.
1 parent bdca5ea commit 0d914d0

File tree

32 files changed

+181
-96
lines changed

32 files changed

+181
-96
lines changed

django/contrib/auth/decorators.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import urlparse
1+
try:
2+
from urllib.parse import urlparse
3+
except ImportError: # Python 2
4+
from urlparse import urlparse
25
from functools import wraps
36
from django.conf import settings
47
from django.contrib.auth import REDIRECT_FIELD_NAME
@@ -21,9 +24,8 @@ def _wrapped_view(request, *args, **kwargs):
2124
path = request.build_absolute_uri()
2225
# If the login url is the same scheme and net location then just
2326
# use the path as the "next" url.
24-
login_scheme, login_netloc = urlparse.urlparse(login_url or
25-
settings.LOGIN_URL)[:2]
26-
current_scheme, current_netloc = urlparse.urlparse(path)[:2]
27+
login_scheme, login_netloc = urlparse(login_url or settings.LOGIN_URL)[:2]
28+
current_scheme, current_netloc = urlparse(path)[:2]
2729
if ((not login_scheme or login_scheme == current_scheme) and
2830
(not login_netloc or login_netloc == current_netloc)):
2931
path = request.get_full_path()

django/contrib/auth/models.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from __future__ import unicode_literals
22

3-
import urllib
4-
53
from django.core.exceptions import ImproperlyConfigured
64
from django.core.mail import send_mail
75
from django.db import models
86
from django.db.models.manager import EmptyManager
97
from django.utils.crypto import get_random_string
10-
from django.utils.encoding import smart_str
8+
from django.utils.http import urlquote
119
from django.utils import six
1210
from django.utils.translation import ugettext_lazy as _
1311
from django.utils import timezone
@@ -268,7 +266,7 @@ def natural_key(self):
268266
return (self.username,)
269267

270268
def get_absolute_url(self):
271-
return "/users/%s/" % urllib.quote(smart_str(self.username))
269+
return "/users/%s/" % urlquote(self.username)
272270

273271
def is_anonymous(self):
274272
"""

django/contrib/auth/tests/views.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import re
3-
import urllib
43

54
from django.conf import settings
65
from django.contrib.sites.models import Site, RequestSite
@@ -10,6 +9,7 @@
109
from django.http import QueryDict
1110
from django.utils.encoding import force_unicode
1211
from django.utils.html import escape
12+
from django.utils.http import urlquote
1313
from django.test import TestCase
1414
from django.test.utils import override_settings
1515

@@ -256,7 +256,7 @@ def test_security_check(self, password='password'):
256256
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
257257
'url': login_url,
258258
'next': REDIRECT_FIELD_NAME,
259-
'bad_url': urllib.quote(bad_url),
259+
'bad_url': urlquote(bad_url),
260260
}
261261
response = self.client.post(nasty_url, {
262262
'username': 'testclient',
@@ -277,7 +277,7 @@ def test_security_check(self, password='password'):
277277
safe_url = '%(url)s?%(next)s=%(good_url)s' % {
278278
'url': login_url,
279279
'next': REDIRECT_FIELD_NAME,
280-
'good_url': urllib.quote(good_url),
280+
'good_url': urlquote(good_url),
281281
}
282282
response = self.client.post(safe_url, {
283283
'username': 'testclient',
@@ -412,7 +412,7 @@ def test_security_check(self, password='password'):
412412
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
413413
'url': logout_url,
414414
'next': REDIRECT_FIELD_NAME,
415-
'bad_url': urllib.quote(bad_url),
415+
'bad_url': urlquote(bad_url),
416416
}
417417
self.login()
418418
response = self.client.get(nasty_url)
@@ -432,7 +432,7 @@ def test_security_check(self, password='password'):
432432
safe_url = '%(url)s?%(next)s=%(good_url)s' % {
433433
'url': logout_url,
434434
'next': REDIRECT_FIELD_NAME,
435-
'good_url': urllib.quote(good_url),
435+
'good_url': urlquote(good_url),
436436
}
437437
self.login()
438438
response = self.client.get(safe_url)

django/contrib/auth/views.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import urlparse
1+
try:
2+
from urllib.parse import urlparse, urlunparse
3+
except ImportError: # Python 2
4+
from urlparse import urlparse, urlunparse
25

36
from django.conf import settings
47
from django.core.urlresolvers import reverse
@@ -34,7 +37,7 @@ def login(request, template_name='registration/login.html',
3437
if request.method == "POST":
3538
form = authentication_form(data=request.POST)
3639
if form.is_valid():
37-
netloc = urlparse.urlparse(redirect_to)[1]
40+
netloc = urlparse(redirect_to)[1]
3841

3942
# Use default setting if redirect_to is empty
4043
if not redirect_to:
@@ -80,7 +83,7 @@ def logout(request, next_page=None,
8083
auth_logout(request)
8184
redirect_to = request.REQUEST.get(redirect_field_name, '')
8285
if redirect_to:
83-
netloc = urlparse.urlparse(redirect_to)[1]
86+
netloc = urlparse(redirect_to)[1]
8487
# Security check -- don't allow redirection to a different host.
8588
if not (netloc and netloc != request.get_host()):
8689
return HttpResponseRedirect(redirect_to)
@@ -116,13 +119,13 @@ def redirect_to_login(next, login_url=None,
116119
if not login_url:
117120
login_url = settings.LOGIN_URL
118121

119-
login_url_parts = list(urlparse.urlparse(login_url))
122+
login_url_parts = list(urlparse(login_url))
120123
if redirect_field_name:
121124
querystring = QueryDict(login_url_parts[4], mutable=True)
122125
querystring[redirect_field_name] = next
123126
login_url_parts[4] = querystring.urlencode(safe='/')
124127

125-
return HttpResponseRedirect(urlparse.urlunparse(login_url_parts))
128+
return HttpResponseRedirect(urlunparse(login_url_parts))
126129

127130
# 4 views for password reset:
128131
# - password_reset sends the mail

django/contrib/comments/views/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
A few bits of helper functions for comment views.
33
"""
44

5-
import urllib
65
import textwrap
6+
try:
7+
from urllib.parse import urlencode
8+
except ImportError: # Python 2
9+
from urllib import urlencode
10+
711
from django.http import HttpResponseRedirect
812
from django.core import urlresolvers
913
from django.shortcuts import render_to_response
@@ -33,7 +37,7 @@ def next_redirect(data, default, default_view, **get_kwargs):
3337
anchor = ''
3438

3539
joiner = ('?' in next) and '&' or '?'
36-
next += joiner + urllib.urlencode(get_kwargs) + anchor
40+
next += joiner + urlencode(get_kwargs) + anchor
3741
return HttpResponseRedirect(next)
3842

3943
def confirmation_view(template, doc="Display a confirmation view."):

django/contrib/contenttypes/tests.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
from __future__ import unicode_literals
22

3-
import urllib
4-
53
from django.db import models
64
from django.contrib.contenttypes.models import ContentType
75
from django.contrib.contenttypes.views import shortcut
86
from django.contrib.sites.models import Site
97
from django.http import HttpRequest, Http404
108
from django.test import TestCase
11-
from django.utils.encoding import smart_str
9+
from django.utils.http import urlquote
1210
from django.utils import six
1311

1412

@@ -36,7 +34,7 @@ class FooWithUrl(FooWithoutUrl):
3634
"""
3735

3836
def get_absolute_url(self):
39-
return "/users/%s/" % urllib.quote(smart_str(self.name))
37+
return "/users/%s/" % urlquote(self.name)
4038

4139
class FooWithBrokenAbsoluteUrl(FooWithoutUrl):
4240
"""

django/contrib/databrowse/plugins/fieldchoices.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
from django.contrib.databrowse.sites import DatabrowsePlugin
77
from django.shortcuts import render_to_response
88
from django.utils.html import format_html, format_html_join
9+
from django.utils.http import urlquote
910
from django.utils.text import capfirst
10-
from django.utils.encoding import smart_str, force_unicode
11-
import urllib
11+
from django.utils.encoding import force_unicode
12+
1213

1314
class FieldChoicePlugin(DatabrowsePlugin):
1415
def __init__(self, field_filter=None):
@@ -38,11 +39,10 @@ def model_index_html(self, request, model, site):
3839

3940
def urls(self, plugin_name, easy_instance_field):
4041
if easy_instance_field.field in self.field_dict(easy_instance_field.model.model).values():
41-
field_value = smart_str(easy_instance_field.raw_value)
4242
return ['%s%s/%s/%s/' % (
4343
easy_instance_field.model.url(),
4444
plugin_name, easy_instance_field.field.name,
45-
urllib.quote(field_value, safe=''))]
45+
urlquote(easy_instance_field.raw_value, safe=''))]
4646

4747
def model_view(self, request, model_databrowse, url):
4848
self.model, self.site = model_databrowse.model, model_databrowse.site
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
try:
2+
from urllib.parse import urljoin
3+
except ImportError: # Python 2
4+
from urlparse import urljoin
5+
16
from django import http
27
from django.contrib.databrowse.datastructures import EasyModel
38
from django.contrib.databrowse.sites import DatabrowsePlugin
49
from django.shortcuts import render_to_response
5-
import urlparse
610

711
class ObjectDetailPlugin(DatabrowsePlugin):
812
def model_view(self, request, model_databrowse, url):
913
# If the object ID wasn't provided, redirect to the model page, which is one level up.
1014
if url is None:
11-
return http.HttpResponseRedirect(urlparse.urljoin(request.path, '../'))
15+
return http.HttpResponseRedirect(urljoin(request.path, '../'))
1216
easy_model = EasyModel(model_databrowse.site, model_databrowse.model)
1317
obj = easy_model.object_by_pk(url)
1418
return render_to_response('databrowse/object_detail.html', {'object': obj, 'root_url': model_databrowse.site.root_url})

django/contrib/sitemaps/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from django.contrib.sites.models import Site
22
from django.core import urlresolvers, paginator
33
from django.core.exceptions import ImproperlyConfigured
4-
import urllib
4+
try:
5+
from urllib.parse import urlencode
6+
from urllib.request import urlopen
7+
except ImportError: # Python 2
8+
from urllib import urlencode, urlopen
59

610
PING_URL = "http://www.google.com/webmasters/tools/ping"
711

@@ -32,8 +36,8 @@ def ping_google(sitemap_url=None, ping_url=PING_URL):
3236
from django.contrib.sites.models import Site
3337
current_site = Site.objects.get_current()
3438
url = "http://%s%s" % (current_site.domain, sitemap_url)
35-
params = urllib.urlencode({'sitemap':url})
36-
urllib.urlopen("%s?%s" % (ping_url, params))
39+
params = urlencode({'sitemap':url})
40+
urlopen("%s?%s" % (ping_url, params))
3741

3842
class Sitemap(object):
3943
# This limit is defined by Google. See the index documentation at

django/contrib/staticfiles/handlers.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import urllib
2-
from urlparse import urlparse
1+
try:
2+
from urllib.parse import urlparse
3+
from urllib.request import url2pathname
4+
except ImportError: # Python 2
5+
from urllib import url2pathname
6+
from urlparse import urlparse
37

48
from django.conf import settings
59
from django.core.handlers.wsgi import WSGIHandler
@@ -42,7 +46,7 @@ def file_path(self, url):
4246
Returns the relative path to the media file on disk for the given URL.
4347
"""
4448
relative_url = url[len(self.base_url[2]):]
45-
return urllib.url2pathname(relative_url)
49+
return url2pathname(relative_url)
4650

4751
def serve(self, request):
4852
"""

0 commit comments

Comments
 (0)