Beispiel #1
0
def create_image(files, user):
    """Given an uploaded file, a user, and other data, it creates an Image"""
    up_file = files.values()[0]
    check_file_size(up_file, settings.IMAGE_MAX_FILESIZE)

    try:
        image = Image.objects.filter(creator=user, is_draft=True)
        # Delete other drafts, if any:
        image.exclude(pk=image[0].pk).delete()
        image = image[0]
    except IndexError:  # No drafts, create one
        image = Image(creator=user, is_draft=True)

    # Async uploads fallback to these defaults.
    image.title = get_draft_title(user)
    image.description = u'Autosaved draft.'
    image.locale = settings.WIKI_DEFAULT_LANGUAGE

    (up_file, is_animated) = _image_to_png(up_file)

    # Finally save the image along with uploading the file.
    image.file.save(up_file.name, File(up_file), save=True)

    (width, height) = _scale_dimensions(image.file.width, image.file.height)
    delete_url = reverse('gallery.delete_media', args=['image', image.id])
    return {
        'name': up_file.name,
        'url': image.get_absolute_url(),
        'thumbnail_url': image.thumbnail_url_if_set(),
        'width': width,
        'height': height,
        'delete_url': delete_url
    }
Beispiel #2
0
def create_image(files, user):
    """Given an uploaded file, a user, and other data, it creates an Image"""
    up_file = files.values()[0]
    check_file_size(up_file, settings.IMAGE_MAX_FILESIZE)

    try:
        image = Image.objects.filter(creator=user, is_draft=True)
        # Delete other drafts, if any:
        image.exclude(pk=image[0].pk).delete()
        image = image[0]
    except IndexError:  # No drafts, create one
        image = Image(creator=user, is_draft=True)

    # Async uploads fallback to these defaults.
    image.title = get_draft_title(user)
    image.description = u'Autosaved draft.'
    image.locale = settings.WIKI_DEFAULT_LANGUAGE

    (up_file, is_animated) = _image_to_png(up_file)

    # Finally save the image along with uploading the file.
    image.file.save(up_file.name,
                    File(up_file), save=True)

    (width, height) = _scale_dimensions(image.file.width, image.file.height)
    delete_url = reverse('gallery.delete_media', args=['image', image.id])
    return {'name': up_file.name, 'url': image.get_absolute_url(),
            'thumbnail_url': image.thumbnail_url_if_set(),
            'width': width, 'height': height,
            'delete_url': delete_url}
Beispiel #3
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(),
    }
Beispiel #4
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()}
Beispiel #5
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()
    }
Beispiel #6
0
def create_video(files, user):
    """Given an uploaded file, a user, and other data, it creates a Video"""
    try:
        vid = Video.objects.filter(creator=user, is_draft=True)
        # Delete other drafts, if any:
        vid.exclude(pk=vid[0].pk).delete()
        vid = vid[0]
    except IndexError:  # No drafts, create one
        vid = Video(creator=user, is_draft=True)
    # Async uploads fallback to these defaults.
    vid.title = get_draft_title(user)
    vid.description = u'Autosaved draft.'
    vid.locale = settings.WIKI_DEFAULT_LANGUAGE

    for name in files:
        up_file = files[name]
        check_file_size(up_file, settings.VIDEO_MAX_FILESIZE)
        # name is in (webm, ogv, flv) sent from upload_video(), below
        getattr(vid, name).save(up_file.name, up_file, save=False)

        if 'thumbnail' == name:
            # Save poster too, we shrink it later.
            vid.poster.save(up_file.name, up_file, save=False)

    vid.save()
    if 'thumbnail' in files:
        thumb = vid.thumbnail
        (width, height) = _scale_dimensions(thumb.width, thumb.height)
    else:
        width = settings.THUMBNAIL_PROGRESS_WIDTH
        height = settings.THUMBNAIL_PROGRESS_HEIGHT
    delete_url = reverse('gallery.delete_media', args=['video', vid.id])
    return {'name': up_file.name, 'url': vid.get_absolute_url(),
            'thumbnail_url': vid.thumbnail_url_if_set(),
            'width': width,
            'height': height,
            'delete_url': delete_url}
Beispiel #7
0
 def test_large_height(self):
     """An image with large height is scaled to height=MAX."""
     ts = 150
     (width, height) = _scale_dimensions(ts - 2, ts * 2 + 9, ts)
     eq_(71, width)
     eq_(ts, height)
Beispiel #8
0
 def test_width_large(self):
     """An image with large width is scaled to width=MAX."""
     ts = 120
     (width, height) = _scale_dimensions(ts * 3 + 10, ts - 1, ts)
     eq_(ts, width)
     eq_(38, height)
Beispiel #9
0
 def test_small(self):
     """A small image is not scaled."""
     ts = settings.THUMBNAIL_SIZE / 2
     (width, height) = _scale_dimensions(ts, ts)
     eq_(ts, width)
     eq_(ts, height)
Beispiel #10
0
 def test_scale_dimensions_default(self):
     """A square image of exact size is not scaled."""
     ts = settings.THUMBNAIL_SIZE
     (width, height) = _scale_dimensions(ts, ts, ts)
     eq_(ts, width)
     eq_(ts, height)
Beispiel #11
0
 def test_large_both_width(self):
     """An image with both large is scaled to the largest - width."""
     ts = 150
     (width, height) = _scale_dimensions(ts * 20 + 8, ts * 4 + 36, ts)
     eq_(ts, width)
     eq_(31, height)
Beispiel #12
0
 def test_large_height(self):
     """An image with large height is scaled to height=MAX."""
     ts = 150
     (width, height) = _scale_dimensions(ts - 2, ts * 2 + 9, ts)
     eq_(71, width)
     eq_(ts, height)
Beispiel #13
0
 def test_width_large(self):
     """An image with large width is scaled to width=MAX."""
     ts = 120
     (width, height) = _scale_dimensions(ts * 3 + 10, ts - 1, ts)
     eq_(ts, width)
     eq_(38, height)
Beispiel #14
0
 def test_small(self):
     """A small image is not scaled."""
     ts = settings.THUMBNAIL_SIZE / 2
     (width, height) = _scale_dimensions(ts, ts)
     eq_(ts, width)
     eq_(ts, height)
Beispiel #15
0
 def test_scale_dimensions_default(self):
     """A square image of exact size is not scaled."""
     ts = settings.THUMBNAIL_SIZE
     (width, height) = _scale_dimensions(ts, ts, ts)
     eq_(ts, width)
     eq_(ts, height)
Beispiel #16
0
 def test_large_both_height(self):
     """An image with both large is scaled to the largest - height."""
     ts = 150
     (width, height) = _scale_dimensions(ts * 2 + 13, ts * 5 + 30, ts)
     eq_(60, width)
     eq_(ts, height)
Beispiel #17
0
 def test_large_both_width(self):
     """An image with both large is scaled to the largest - width."""
     ts = 150
     (width, height) = _scale_dimensions(ts * 20 + 8, ts * 4 + 36, ts)
     eq_(ts, width)
     eq_(31, height)
Beispiel #18
0
 def test_large_both_height(self):
     """An image with both large is scaled to the largest - height."""
     ts = 150
     (width, height) = _scale_dimensions(ts * 2 + 13, ts * 5 + 30, ts)
     eq_(60, width)
     eq_(ts, height)