Exemplo n.º 1
0
def create_imageattachment(files, user, obj):
    """
    Given an uploaded file, a user and an object, it creates an ImageAttachment
    owned by `user` and attached to `obj`.
    """
    up_file = files.values()[0]
    check_file_size(up_file, settings.IMAGE_MAX_FILESIZE)

    (up_file, is_animated) = _image_to_png(up_file)

    image = ImageAttachment(content_object=obj, creator=user)
    image.file.save(up_file.name, File(up_file),
                    save=True)

    # Compress and generate thumbnail off thread
    generate_thumbnail.delay(image, 'file', 'thumbnail')
    if not is_animated:
        compress_image.delay(image, 'file')

    (width, height) = _scale_dimensions(image.file.width, image.file.height)

    # The filename may contain html in it. Escape it.
    name = bleach.clean(up_file.name)

    return {'name': name, 'url': image.file.url,
            'thumbnail_url': image.thumbnail_if_set().url,
            'width': width, 'height': height,
            'delete_url': image.get_delete_url()}
Exemplo n.º 2
0
def create_imageattachment(files, user, obj):
    """
    Given an uploaded file, a user and an object, it creates an ImageAttachment
    owned by `user` and attached to `obj`.
    """
    up_file = files.values()[0]
    check_file_size(up_file, settings.IMAGE_MAX_FILESIZE)

    (up_file, is_animated) = _image_to_png(up_file)

    image = ImageAttachment(content_object=obj, creator=user)
    image.file.save(up_file.name, File(up_file), save=True)

    # Compress and generate thumbnail off thread
    generate_thumbnail.delay(image, 'file', 'thumbnail')
    if not is_animated:
        compress_image.delay(image, 'file')

    (width, height) = _scale_dimensions(image.file.width, image.file.height)

    # The filename may contain html in it. Escape it.
    name = bleach.clean(up_file.name)

    return {
        'name': name,
        'url': image.file.url,
        'thumbnail_url': image.thumbnail_if_set().url,
        'width': width,
        'height': height,
        'delete_url': image.get_delete_url()
    }
Exemplo n.º 3
0
    def test_thumbnail_if_set(self):
        """thumbnail_if_set() returns self.thumbnail if set, or else returns
        self.file"""
        image = ImageAttachment(content_object=self.obj, creator=self.user)
        with open("kitsune/upload/tests/media/test.jpg", "rb") as f:
            up_file = File(f)
            image.file.save(up_file.name, up_file, save=True)

        eq_(image.file, image.thumbnail_if_set())

        generate_thumbnail(image, "file", "thumbnail")
        eq_(image.thumbnail, image.thumbnail_if_set())
Exemplo n.º 4
0
    def test_thumbnail_if_set(self):
        """thumbnail_if_set() returns self.thumbnail if set, or else returns
        self.file"""
        image = ImageAttachment(content_object=self.obj, creator=self.user)
        with open('kitsune/upload/tests/media/test.jpg') as f:
            up_file = File(f)
            image.file.save(up_file.name, up_file, save=True)

        eq_(image.file, image.thumbnail_if_set())

        generate_thumbnail(image, 'file', 'thumbnail')
        eq_(image.thumbnail, image.thumbnail_if_set())
Exemplo n.º 5
0
 def _image_with_thumbnail(self):
     image = ImageAttachment(content_object=self.obj, creator=self.user)
     with open('kitsune/upload/tests/media/test.jpg') as f:
         up_file = File(f)
         image.file.save(up_file.name, up_file, save=True)
     generate_thumbnail(image, 'file', 'thumbnail')
     return image
Exemplo n.º 6
0
 def test_generate_deleted_file(self):
     """generate_thumbnail does not fail if file doesn't actually exist."""
     image = ImageAttachment(content_object=self.obj, creator=self.user)
     with open('kitsune/upload/tests/media/test.jpg') as f:
         up_file = File(f)
         image.file.save(up_file.name, up_file, save=True)
     # The field will be set but the file isn't there.
     os.remove(image.file.path)
     generate_thumbnail(image, 'file', 'thumbnail')
Exemplo n.º 7
0
def create_imageattachment(files, user, obj):
    """
    Given an uploaded file, a user and an object, it creates an ImageAttachment
    owned by `user` and attached to `obj`.
    """
    up_file = list(files.values())[0]
    check_file_size(up_file, settings.IMAGE_MAX_FILESIZE)

    (up_file, is_animated) = _image_to_png(up_file)

    image = ImageAttachment(content_object=obj, creator=user)
    image.file.save(up_file.name, File(up_file), save=True)

    # Compress and generate thumbnail off thread
    generate_thumbnail.delay(image, "file", "thumbnail")
    if not is_animated:
        compress_image.delay(image, "file")

    # Refresh because the image may have been changed by tasks.
    image.refresh_from_db()

    (width, height) = _scale_dimensions(image.file.width, image.file.height)

    # The filename may contain html in it. Escape it.
    name = escape(up_file.name)

    return {
        "name": name,
        "url": image.file.url,
        "thumbnail_url": image.thumbnail_if_set().url,
        "width": width,
        "height": height,
        "delete_url": image.get_delete_url(),
    }
Exemplo n.º 8
0
 def test_compress_no_file(self, call):
     """compress_image does not fail when no file is provided."""
     image = ImageAttachment(content_object=self.obj, creator=self.user)
     compress_image(image, 'file')
     assert not call.called
Exemplo n.º 9
0
 def _uploaded_image(self, testfile="test.png"):
     image = ImageAttachment(content_object=self.obj, creator=self.user)
     with open('kitsune/upload/tests/media/' + testfile) as f:
         up_file = File(f)
         image.file.save(up_file.name, up_file, save=True)
     return image
Exemplo n.º 10
0
 def test_generate_no_file(self):
     """generate_thumbnail does not fail when no file is provided."""
     image = ImageAttachment(content_object=self.obj, creator=self.user)
     generate_thumbnail(image, 'file', 'thumbnail')