예제 #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}
예제 #3
0
def create_video(files, user):
    """Given an uploaded file, a user, and other data, it creates a Video"""
    # Async uploads fallback to these defaults.
    title = get_draft_title(user)
    description = u'Autosaved draft.'
    # Use default locale to make sure a user can only have one draft
    locale = settings.WIKI_DEFAULT_LANGUAGE
    try:
        vid = Video.objects.get(title=title, locale=locale)
    except Video.DoesNotExist:
        vid = Video(title=title, creator=user, description=description,
                    locale=locale)
    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}
예제 #4
0
파일: forms.py 프로젝트: klrmn/kitsune
 def clean_avatar(self):
     if not ("avatar" in self.cleaned_data and self.cleaned_data["avatar"]):
         return self.cleaned_data["avatar"]
     try:
         check_file_size(self.cleaned_data["avatar"], settings.MAX_AVATAR_FILE_SIZE)
     except FileTooLargeError as e:
         raise forms.ValidationError(e.args[0])
     clean_image_extension(self.cleaned_data.get("avatar"))
     return self.cleaned_data["avatar"]
예제 #5
0
파일: forms.py 프로젝트: swznd/kuma
 def clean_avatar(self):
     if not ('avatar' in self.cleaned_data and self.cleaned_data['avatar']):
         return self.cleaned_data['avatar']
     try:
         check_file_size(self.cleaned_data['avatar'],
                         settings.MAX_AVATAR_FILE_SIZE)
     except FileTooLargeError as e:
         raise forms.ValidationError(e.args[0])
     clean_image_extension(self.cleaned_data.get('avatar'))
     return self.cleaned_data['avatar']
예제 #6
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)

    # Async uploads fallback to these defaults.
    title = get_draft_title(user)
    description = u'Autosaved draft.'
    # Use default locale to make sure a user can only have one draft
    locale = settings.WIKI_DEFAULT_LANGUAGE

    image = Image(title=title, creator=user, locale=locale,
                  description=description)
    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}
예제 #7
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
    }
예제 #8
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}
예제 #9
0
 def test_check_file_size_over(self):
     """FileTooLargeError should be raised"""
     with open('apps/upload/tests/media/test.jpg') as f:
         up_file = File(f)
         fn = lambda: check_file_size(up_file, 0)
         self.assertRaises(FileTooLargeError, fn)
예제 #10
0
 def test_check_file_size_under(self):
     """No exception should be raised"""
     with open('apps/upload/tests/media/test.jpg') as f:
         up_file = File(f)
         check_file_size(up_file, settings.IMAGE_MAX_FILESIZE)
예제 #11
0
 def test_check_file_size_over(self):
     """FileTooLargeError should be raised"""
     with open('apps/upload/tests/media/test.jpg') as f:
         up_file = File(f)
         fn = lambda: check_file_size(up_file, 0)
         self.assertRaises(FileTooLargeError, fn)
예제 #12
0
 def test_check_file_size_under(self):
     """No exception should be raised"""
     with open('apps/upload/tests/media/test.jpg') as f:
         up_file = File(f)
         check_file_size(up_file, settings.IMAGE_MAX_FILESIZE)