# HG changeset patch # User Gustavo Andres Morero # Date 1525708193 10800 # Mon May 07 12:49:53 2018 -0300 # Node ID 29de1fcd2e5cf37549f221a84fd511db24f83092 # Parent 585fd9173adeacfb81e3fe8809135224aa970788 updating urllib imports and related. diff --git a/webutils/aws/secures3.py b/webutils/aws/secures3.py --- a/webutils/aws/secures3.py +++ b/webutils/aws/secures3.py @@ -1,12 +1,19 @@ import hmac import time import base64 -import urllib import hashlib try: from urlparse import urlparse except ImportError: from urllib.parse import urlparse +try: + from urllib import quote_plus +except ImportError: + from urllib.parse import quote_plus +try: + from urllib import urlencode +except ImportError: + from urllib.parse import urlencode class SecureS3(object): @@ -17,9 +24,9 @@ def gen_signature(self, string_to_sign): return base64.encodestring( hmac.new( - self.secret_key, + self.secret_key.encode('utf-8'), string_to_sign, - hashlib.sha1 + hashlib.sha1, ).digest() ).strip() @@ -27,10 +34,10 @@ ''' Returns bucket name and file from an S3 URL ''' amazon_host = 's3.amazonaws.com' - s = urlparse.urlparse(url) + s = urlparse(url) if not s.path or not s.path[1:]: raise ValueError('Invalid S3 file passed.') - + if s.netloc == amazon_host: # s3.amazonaws.com/bucket/file... bucket = s.path[1:].split('/')[0] @@ -57,7 +64,7 @@ expires: Seconds from NOW the link expires timestamp: Epoch timestamp. If present, "expires" will not be used. ''' - filename = urllib.quote_plus(filename) + filename = quote_plus(filename) filename = filename.replace('%2F', '/') path = '/%s/%s' % (bucket, filename) @@ -75,8 +82,8 @@ } return '%s://s3.amazonaws.com/%s/%s?%s' % ( - scheme, bucket, filename, urllib.urlencode(params)) - + scheme, bucket, filename, urlencode(params)) + def get_easy_auth_link(self, url, expires=600): ''' url should be the full URL to the secure file hosted on S3. examples: diff --git a/webutils/djtools/decorators.py b/webutils/djtools/decorators.py --- a/webutils/djtools/decorators.py +++ b/webutils/djtools/decorators.py @@ -1,4 +1,9 @@ import urlparse + +try: + from urlparse import urljoin +except ImportError: + from urllib.parse import urljoin from django.conf import settings from django.http import HttpResponseRedirect @@ -7,11 +12,11 @@ def _checkssl(request, *args, **kwargs): if not settings.DEBUG and not request.is_secure(): if 'HTTP_X_FORWARDED_PROTO' not in request.META: - # This checks for X_FORWARDED_PROTO header. Usually + # This checks for X_FORWARDED_PROTO header. Usually # passed when SSL is being proxied upstream. # This should avoid a redirect loop. if hasattr(settings, 'SSL_DOMAIN'): - url_str = urlparse.urljoin( + url_str = urljoin( settings.SSL_DOMAIN, request.get_full_path() ) diff --git a/webutils/djtools/templatetags/djtools_image_tags.py b/webutils/djtools/templatetags/djtools_image_tags.py --- a/webutils/djtools/templatetags/djtools_image_tags.py +++ b/webutils/djtools/templatetags/djtools_image_tags.py @@ -1,5 +1,4 @@ import os -import urlparse from django import template from django.core.files.storage import get_storage_class from django.core.files.base import ContentFile @@ -29,7 +28,7 @@ image = Image.open(file) if image.size[0] < x and image.size[1] < y: - # New size is bigger than original's size! Don't + # New size is bigger than original's size! Don't # create new image. miniature_url = file.url else: diff --git a/webutils/restlib/deprecated.py b/webutils/restlib/deprecated.py --- a/webutils/restlib/deprecated.py +++ b/webutils/restlib/deprecated.py @@ -15,7 +15,10 @@ import urllib import urllib2 import hashlib -import urlparse +try: + from urlparse import urlparse +except ImportError: + from urllib.parse import urlparse from webutils.helpers import encode_dict @@ -27,7 +30,7 @@ class APIClient(object): def __init__(self, url, key, debug=False): - url_scheme = urlparse.urlparse(url) + url_scheme = urlparse(url) self.url = url self.key = key self.scheme = url_scheme.scheme @@ -71,4 +74,4 @@ } return False, hash - return True, data \ No newline at end of file + return True, data diff --git a/webutils/restlib/helpers.py b/webutils/restlib/helpers.py --- a/webutils/restlib/helpers.py +++ b/webutils/restlib/helpers.py @@ -1,13 +1,16 @@ -import urlparse +try: + from urlparse import urlparse +except ImportError: + from urllib.parse import urlparse from restlib import BaseClient from socket import _GLOBAL_DEFAULT_TIMEOUT as default_timeout def url_to_base_client(url, filters=[], timeout=default_timeout, debug=False): - ''' Takes a full URL and returns a dict with appropriate + ''' Takes a full URL and returns a dict with appropriate data, including a "ready" BaseClient instance. ''' - url_scheme = urlparse.urlparse(url) + url_scheme = urlparse(url) host = url_scheme.netloc port = url_scheme.port is_secure = (url_scheme.scheme == 'https') @@ -25,4 +28,4 @@ if field == 'path' and res[field] == '': res[field] = '/' bc.url_data = res - return bc \ No newline at end of file + return bc