示例#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 = _image_to_png(up_file)

    # Finally save the image along with uploading the file.
    image.file.save(os.path.splitext(up_file.name)[0] + '.png',
                    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
    }
示例#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 = _image_to_png(up_file)

    # Finally save the image along with uploading the file.
    image.file.save(os.path.splitext(up_file.name)[0] + '.png',
                    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}