def setUp(self):
     super(ProfileImageViewDeleteTestCase, self).setUp()
     with make_image_file() as image_file:
         create_profile_images(image_file, get_profile_image_names(self.user.username))
         self.check_images()
         set_has_profile_image(self.user.username, True, TEST_UPLOAD_DT)
         # Ignore previous event
         self.reset_tracker()
Пример #2
0
 def create_profile_image(self, user, storage):
     """
     Creates profile image for user and checks that created image exists in storage
     """
     with make_image_file() as image_file:
         create_profile_images(image_file, get_profile_image_names(user.username))
         self.check_images(user, storage)
         set_has_profile_image(user.username, True, self.TEST_PROFILE_IMAGE_UPLOADED_AT)
Пример #3
0
 def setUp(self):
     super(ProfileImageViewDeleteTestCase, self).setUp()
     with make_image_file() as image_file:
         create_profile_images(image_file, get_profile_image_names(self.user.username))
         self.check_images()
         set_has_profile_image(self.user.username, True, TEST_UPLOAD_DT)
         # Ignore previous event
         self.reset_tracker()
Пример #4
0
 def create_profile_image(self, user, storage):
     """
     Creates profile image for user and checks that created image exists in storage
     """
     with make_image_file() as image_file:
         create_profile_images(image_file, get_profile_image_names(user.username))
         self.check_images(user, storage)
         set_has_profile_image(user.username, True, self.TEST_PROFILE_IMAGE_UPLOADED_AT)
Пример #5
0
    def post(self, request, username):
        """
        POST /api/user/v1/accounts/{username}/image
        """

        # validate request:
        # verify that the user's
        # ensure any file was sent
        if 'file' not in request.FILES:
            return Response(
                {
                    "developer_message": u"No file provided for profile image",
                    "user_message": _(u"No file provided for profile image"),
                },
                status=status.HTTP_400_BAD_REQUEST)

        # process the upload.
        uploaded_file = request.FILES['file']
        log.info(
            '---------------------Uploaded file-------------------------------%s',
            uploaded_file)

        # no matter what happens, delete the temporary file when we're done
        with closing(uploaded_file):

            # image file validation.
            try:
                validate_uploaded_image(uploaded_file)
            except ImageValidationError as error:
                return Response(
                    {
                        "developer_message": text_type(error),
                        "user_message": error.user_message
                    },
                    status=status.HTTP_400_BAD_REQUEST,
                )

            # generate profile pic and thumbnails and store them
            profile_image_names = get_profile_image_names(username)
            create_profile_images(uploaded_file, profile_image_names)

            # update the user account to reflect that a profile image is available.
            set_has_profile_image(username, True, _make_upload_dt())

            log.info(
                LOG_MESSAGE_CREATE, {
                    'image_names': list(profile_image_names.values()),
                    'user_id': request.user.id
                })

        # send client response.
        return Response(status=status.HTTP_204_NO_CONTENT)
Пример #6
0
    def post(self, request, username):
        """
        POST /api/user/v1/accounts/{username}/image
        """

        # validate request:
        # verify that the user's
        # ensure any file was sent
        if 'file' not in request.FILES:
            return Response(
                {
                    "developer_message": u"No file provided for profile image",
                    "user_message": _(u"No file provided for profile image"),

                },
                status=status.HTTP_400_BAD_REQUEST
            )

        # process the upload.
        uploaded_file = request.FILES['file']

        # no matter what happens, delete the temporary file when we're done
        with closing(uploaded_file):

            # image file validation.
            try:
                validate_uploaded_image(uploaded_file)
            except ImageValidationError as error:
                return Response(
                    {"developer_message": text_type(error), "user_message": error.user_message},
                    status=status.HTTP_400_BAD_REQUEST,
                )

            # generate profile pic and thumbnails and store them
            profile_image_names = get_profile_image_names(username)
            create_profile_images(uploaded_file, profile_image_names)

            # update the user account to reflect that a profile image is available.
            set_has_profile_image(username, True, _make_upload_dt())

            log.info(
                LOG_MESSAGE_CREATE,
                {'image_names': profile_image_names.values(), 'user_id': request.user.id}
            )

        # send client response.
        return Response(status=status.HTTP_204_NO_CONTENT)
def download_profile_image(auth_entry, strategy, details, user=None, *args, **kwargs):
    if user and not user.profile.profile_image_uploaded_at:
        try:
            username = user.username

            req = urllib2.Request('https://graph.microsoft.com/v1.0/me/photo/$value')
            req.add_header('Authorization', 'Bearer {0}'.format(kwargs['response']['access_token']))
            resp = urllib2.urlopen(req)
            content = resp.read()

            fp = BytesIO()
            fp.write(content)
            image_file = files.File(fp)
            profile_image_names = get_profile_image_names(user.username)

            create_profile_images(image_file, profile_image_names)
            set_has_profile_image(username, True, _make_upload_dt())
        except Exception as e:
            log.exception('Error when downloading user image from Microsoft API', exc_info=True)
Пример #8
0
    def post(self, request, username):  # pylint: disable=unused-argument
        """
        POST /api/profile_images/v1/{username}/remove
        """
        try:
            # update the user account to reflect that the images were removed.
            set_has_profile_image(username, False)

            # remove physical files from storage.
            profile_image_names = get_profile_image_names(username)
            remove_profile_images(profile_image_names)

            log.info(
                LOG_MESSAGE_DELETE,
                {'image_names': profile_image_names.values(), 'user_id': request.user.id}
            )
        except UserNotFound:
            return Response(status=status.HTTP_404_NOT_FOUND)

        # send client response.
        return Response(status=status.HTTP_204_NO_CONTENT)
Пример #9
0
    def delete(self, request, username):  # pylint: disable=unused-argument
        """
        DELETE /api/user/v1/accounts/{username}/image
        """

        try:
            # update the user account to reflect that the images were removed.
            set_has_profile_image(username, False)

            # remove physical files from storage.
            profile_image_names = get_profile_image_names(username)
            remove_profile_images(profile_image_names)

            log.info(
                LOG_MESSAGE_DELETE,
                {'image_names': profile_image_names.values(), 'user_id': request.user.id}
            )
        except UserNotFound:
            return Response(status=status.HTTP_404_NOT_FOUND)

        # send client response.
        return Response(status=status.HTTP_204_NO_CONTENT)
Пример #10
0
    def delete(self, request, username):
        """
        DELETE /api/user/v1/accounts/{username}/image
        """

        try:
            # update the user account to reflect that the images were removed.
            set_has_profile_image(username, False)

            # remove physical files from storage.
            profile_image_names = get_profile_image_names(username)
            remove_profile_images(profile_image_names)

            log.info(
                LOG_MESSAGE_DELETE,
                {'image_names': list(profile_image_names.values()), 'user_id': request.user.id}
            )
        except UserNotFound:
            return Response(status=status.HTTP_404_NOT_FOUND)

        # send client response.
        return Response(status=status.HTTP_204_NO_CONTENT)
Пример #11
0
 def delete_users_profile_images(user):
     set_has_profile_image(user.username, False)
     names_of_profile_images = get_profile_image_names(user.username)
     remove_profile_images(names_of_profile_images)
Пример #12
0
 def delete_users_profile_images(user):
     set_has_profile_image(user.username, False)
     names_of_profile_images = get_profile_image_names(user.username)
     remove_profile_images(names_of_profile_images)