diff --git a/openedx/core/djangoapps/profile_images/exceptions.py b/openedx/core/djangoapps/profile_images/exceptions.py index 2977a759004b50cb6d9ac1a7eca44e6b9ab4cd3f..53013b18c93905b9e2339b2568a012bf9564d8bc 100644 --- a/openedx/core/djangoapps/profile_images/exceptions.py +++ b/openedx/core/djangoapps/profile_images/exceptions.py @@ -1,6 +1,8 @@ """ Exceptions related to the handling of profile images. """ +from __future__ import absolute_import + from six import text_type diff --git a/openedx/core/djangoapps/profile_images/images.py b/openedx/core/djangoapps/profile_images/images.py index 15d7c5c44bde8138f1535d877060cff8a0e071f9..d067dcbf8fd8060dbd49997b7da6c38b4d79a44e 100644 --- a/openedx/core/djangoapps/profile_images/images.py +++ b/openedx/core/djangoapps/profile_images/images.py @@ -1,11 +1,14 @@ """ Image file manipulation functions related to profile images. """ +from __future__ import absolute_import + from collections import namedtuple from contextlib import closing from cStringIO import StringIO import piexif +import six from django.conf import settings from django.core.files.base import ContentFile from django.utils.translation import ugettext as _ @@ -106,7 +109,7 @@ def validate_uploaded_image(uploaded_file): raise ImageValidationError(file_upload_too_small) # check the file extension looks acceptable - filename = unicode(uploaded_file.name).lower() + filename = six.text_type(uploaded_file.name).lower() filetype = [ft for ft in IMAGE_TYPES if any(filename.endswith(ext) for ext in IMAGE_TYPES[ft].extensions)] if not filetype: file_upload_bad_type = _( diff --git a/openedx/core/djangoapps/profile_images/tests/helpers.py b/openedx/core/djangoapps/profile_images/tests/helpers.py index a3c826bc5e64da4437252d9c1d19b0026724c1f1..08693b47d92d0fc725c6739b2b2ac1b86652d7ad 100644 --- a/openedx/core/djangoapps/profile_images/tests/helpers.py +++ b/openedx/core/djangoapps/profile_images/tests/helpers.py @@ -1,6 +1,7 @@ """ Helper methods for use in profile image tests. """ +from __future__ import absolute_import from contextlib import contextmanager import os from tempfile import NamedTemporaryFile @@ -8,6 +9,7 @@ from tempfile import NamedTemporaryFile from django.core.files.uploadedfile import UploadedFile import piexif from PIL import Image +from six.moves import range @contextmanager @@ -29,7 +31,7 @@ def make_image_file(dimensions=(320, 240), prefix='tmp', extension='.jpeg', forc image = Image.new('RGB', dimensions, "green") image_file = NamedTemporaryFile(prefix=prefix, suffix=extension) try: - if orientation and orientation in xrange(1, 9): + if orientation and orientation in range(1, 9): exif_bytes = piexif.dump({'0th': {piexif.ImageIFD.Orientation: orientation}}) image.save(image_file, exif=exif_bytes) else: @@ -40,9 +42,9 @@ def make_image_file(dimensions=(320, 240), prefix='tmp', extension='.jpeg', forc # write in hunks of 256 bytes hunk, byte_ = bytearray([0] * 256), bytearray([0]) num_hunks, remainder = divmod(bytes_to_pad, 256) - for _ in xrange(num_hunks): + for _ in range(num_hunks): image_file.write(hunk) - for _ in xrange(remainder): + for _ in range(remainder): image_file.write(byte_) image_file.flush() image_file.seek(0) diff --git a/openedx/core/djangoapps/profile_images/tests/test_images.py b/openedx/core/djangoapps/profile_images/tests/test_images.py index a58ffcf9d4459271b631c51e074810621288e80b..56c40476f0a429d9caf8b78516fac08e0257cdaa 100644 --- a/openedx/core/djangoapps/profile_images/tests/test_images.py +++ b/openedx/core/djangoapps/profile_images/tests/test_images.py @@ -1,6 +1,7 @@ """ Test cases for image processing functions in the profile image package. """ +from __future__ import absolute_import from contextlib import closing from itertools import product import os @@ -243,5 +244,5 @@ class TestRemoveProfileImages(TestCase): ): remove_profile_images(requested_sizes) deleted_names = [v[0][0] for v in mock_storage.delete.call_args_list] - self.assertEqual(requested_sizes.values(), deleted_names) + self.assertEqual(list(requested_sizes.values()), deleted_names) mock_storage.save.reset_mock() diff --git a/openedx/core/djangoapps/profile_images/tests/test_views.py b/openedx/core/djangoapps/profile_images/tests/test_views.py index a79c08bd57ead6a7ba1b6837f5b89ca07cc50096..3a6d18741ca14deb5aa76e931e6af7e520c74e1c 100644 --- a/openedx/core/djangoapps/profile_images/tests/test_views.py +++ b/openedx/core/djangoapps/profile_images/tests/test_views.py @@ -1,6 +1,7 @@ """ Test cases for the HTTP endpoints of the profile image api. """ +from __future__ import absolute_import from contextlib import closing import datetime from pytz import UTC @@ -178,7 +179,7 @@ class ProfileImageViewPostTestCase(ProfileImageEndpointMixin, APITestCase): self.check_has_profile_image() mock_log.info.assert_called_once_with( LOG_MESSAGE_CREATE, - {'image_names': get_profile_image_names(self.user.username).values(), 'user_id': self.user.id} + {'image_names': list(get_profile_image_names(self.user.username).values()), 'user_id': self.user.id} ) self.check_upload_event_emitted() @@ -217,7 +218,7 @@ class ProfileImageViewPostTestCase(ProfileImageEndpointMixin, APITestCase): self.check_has_profile_image() mock_log.info.assert_called_once_with( LOG_MESSAGE_CREATE, - {'image_names': get_profile_image_names(self.user.username).values(), 'user_id': self.user.id} + {'image_names': list(get_profile_image_names(self.user.username).values()), 'user_id': self.user.id} ) self.check_upload_event_emitted() @@ -400,7 +401,7 @@ class ProfileImageViewDeleteTestCase(ProfileImageEndpointMixin, APITestCase): self.check_has_profile_image(False) mock_log.info.assert_called_once_with( LOG_MESSAGE_DELETE, - {'image_names': get_profile_image_names(self.user.username).values(), 'user_id': self.user.id} + {'image_names': list(get_profile_image_names(self.user.username).values()), 'user_id': self.user.id} ) self.check_remove_event_emitted() @@ -435,7 +436,7 @@ class ProfileImageViewDeleteTestCase(ProfileImageEndpointMixin, APITestCase): self.check_has_profile_image(False) mock_log.info.assert_called_once_with( LOG_MESSAGE_DELETE, - {'image_names': get_profile_image_names(self.user.username).values(), 'user_id': self.user.id} + {'image_names': list(get_profile_image_names(self.user.username).values()), 'user_id': self.user.id} ) self.check_remove_event_emitted() diff --git a/openedx/core/djangoapps/profile_images/urls.py b/openedx/core/djangoapps/profile_images/urls.py index 06e6d0529f61ad518e59587a6059f901a4c52da5..d2bc6e00787afbf0e06fa64cdb0e234f98a724ca 100644 --- a/openedx/core/djangoapps/profile_images/urls.py +++ b/openedx/core/djangoapps/profile_images/urls.py @@ -8,6 +8,8 @@ NOTE: These views are deprecated. These routes are superseded by """ # pylint: enable=unicode-format-string +from __future__ import absolute_import + from django.conf import settings from django.conf.urls import url diff --git a/openedx/core/djangoapps/profile_images/views.py b/openedx/core/djangoapps/profile_images/views.py index 2d5512e168a499990cc63b90e01352701ac9fadc..8cee2b2588e7bac89d0475b0c2c4f46d39f4724a 100644 --- a/openedx/core/djangoapps/profile_images/views.py +++ b/openedx/core/djangoapps/profile_images/views.py @@ -1,15 +1,17 @@ """ This module implements the upload and remove endpoints of the profile image api. """ +from __future__ import absolute_import + import datetime import itertools import logging from contextlib import closing -from pytz import UTC from django.utils.translation import ugettext as _ from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication from edx_rest_framework_extensions.auth.session.authentication import SessionAuthenticationAllowInactiveUser +from pytz import UTC from rest_framework import permissions, status from rest_framework.parsers import FormParser, MultiPartParser from rest_framework.response import Response @@ -162,7 +164,7 @@ class ProfileImageView(DeveloperErrorViewMixin, APIView): log.info( LOG_MESSAGE_CREATE, - {'image_names': profile_image_names.values(), 'user_id': request.user.id} + {'image_names': list(profile_image_names.values()), 'user_id': request.user.id} ) # send client response. @@ -183,7 +185,7 @@ class ProfileImageView(DeveloperErrorViewMixin, APIView): log.info( LOG_MESSAGE_DELETE, - {'image_names': profile_image_names.values(), 'user_id': request.user.id} + {'image_names': list(profile_image_names.values()), 'user_id': request.user.id} ) except UserNotFound: return Response(status=status.HTTP_404_NOT_FOUND)