Exemplo n.º 1
0
    def file_type_test(self,
                       bucket_method,
                       test_file,
                       content_type,
                       resized_size):
        bucket = Mock()
        bucket_method.return_value = bucket
        new_key = Mock()
        bucket.new_key.return_value = new_key
        image_handler = ImageHandler(filename=test_file)
        truck = ImageTruck.new_from_file(test_file)
        session = Client().session()
        image = ImageTable(filename='badcafe')
        session.add(image)
        session.flush()
        after_upload = Mock()

        ResizeImage._resize_image(image,
                                  image_handler,
                                  truck,
                                  'thumbnail',
                                  after_upload)

        new_key.set_metadata.assert_called_with('Content-Type', content_type)
        resized_contents = new_key.set_contents_from_string.call_args[0][0]

        image_handler = ImageHandler(blob=resized_contents)
        eq_(image_handler.size, resized_size)

        after_upload.assert_called_once_with('thumbnail')
Exemplo n.º 2
0
def process_image(self, image_contents_id):
    session = Client().session()
    contents = session.query(ImageContents).\
        filter(ImageContents.image_contents_id == image_contents_id).\
        one()

    image = session.query(Image).\
        filter(Image.image_id == contents.image_id).\
        one()

    truck = ImageTruck(
        contents.contents, contents.content_type, image.source_url)
    truck.filename = image.filename
    metadata = ImageMetadata.image_metadata(truck.contents)
    truck.contents = ReorientImage.reorient_image(truck.contents)
    def after_upload(size):
        redis.publish(REDIS_CHANNEL, json.dumps({
            'task_id': self.request.id,
            'suffix': size,
        }))
    ResizeImage.make_resizes(image, truck, after_upload)

    print "uploading original image"
    truck.upload()
    redis.publish(REDIS_CHANNEL, json.dumps({
        'task_id': self.request.id,
        'suffix': '',
    }))

    delay(queued_tasks, Invalidate(), image.image_id)

    for attr, value in metadata.iteritems():
        setattr(image, attr, value)
    session.add(image)
    session.delete(contents)
Exemplo n.º 3
0
def add(request_format):
    tag_names = request.form['tags'].split(' ')
    url = request.form['url']

    if url:
        print 'fetching from remote url'
        truck = ImageTruck.new_from_url(url)
    elif request.files['file']:
        image = request.files['file']
        truck = ImageTruck.new_from_stream(image.stream, image.mimetype)
    else:
        abort(400)
    metadata = ImageMetadata.image_metadata(truck.contents)
    print 'potentially reorienting'
    truck.contents = ReorientImage.reorient_image(truck.contents)
    print 'uploading to s3'
    truck.upload()
    session = Client().session()
    image = Image(filename=truck.calculate_filename(),
                  source_url=url,
                  description=request.form.get('description'),
                  title=request.form.get('title'),
                  **metadata)
    album_id = request.form['album']
    if album_id:
        image.album_id = album_id
    session.add(image)
    image.add_tags(tag_names)

    ResizeImage.make_resizes(image, truck)

    if request_format == 'html':
        return redirect(url_for('show_image', image_id=image.image_id))
    elif request_format == 'json':
        return {'url': truck.url()}
Exemplo n.º 4
0
    def file_type_test(self,
                       bucket_method,
                       test_file_name,
                       content_type,
                       resized_size):
        bucket = Mock()
        bucket_method.return_value = bucket
        new_key = Mock()
        bucket.new_key.return_value = new_key
        test_file = os.path.join(os.path.dirname(__file__), test_file_name)
        image_handler = ImageHandler.open(test_file)
        with open(test_file, 'r') as fh:
            truck = ImageTruck.new_from_stream(fh, content_type)
        session = Client().session()
        image = ImageTable(filename='badcafe')
        session.add(image)
        session.flush()

        ResizeImage._resize_image(image, image_handler, truck, 'thumbnail')

        new_key.set_metadata.assert_called_with('Content-Type', content_type)
        resized_contents = new_key.set_contents_from_string.call_args[0][0]

        image_handler = ImageHandler.open(StringIO.StringIO(resized_contents))
        eq_(image_handler.size, resized_size)
Exemplo n.º 5
0
    def test_resize_a_portrait_image(self, MockImage):
        session = Client().session()
        image_handler = Mock()
        image_handler.size = (427, 640)
        image_handler.format = 'JPEG'
        image = ImageTable(filename='badcafe')
        session.add(image)
        session.flush()

        ResizeImage._resize_image(image, image_handler, Mock(), 'medium')

        resizes = session.query(ImageResize).all()
        eq_(len(resizes), 1)
        eq_(resizes[0].height, 500)
        eq_(resizes[0].width, 333)
Exemplo n.º 6
0
    def test_only_scales_images_down_not_up(self,
                                            resize_image_method,
                                            MockImage):
        image_handler = Mock()
        image_handler.size = (360, 360)
        MockImage.open.return_value = image_handler

        session = Client().session()
        image = ImageTable(filename='faded')
        session.add(image)
        session.flush()

        truck = Mock()
        ResizeImage.make_resizes(image, truck)

        resize_image_method.assert_has_calls([
            call(image, image_handler, truck, 'thumbnail'),
            call(image, image_handler, truck, 'small'),
            ], any_order=True)
Exemplo n.º 7
0
    def test_creates_various_resizes(self, resize_image_method, MockImage):
        session = Client().session()
        image = ImageTable(filename='faded')
        session.add(image)
        session.flush()

        truck = ImageTruck('contents', None, None)

        image_handler = Mock()
        image_handler.size = (3648, 2736)
        MockImage.open.return_value = image_handler

        ResizeImage.make_resizes(image, truck)

        resize_image_method.assert_has_calls([
            call(image, image_handler, truck, 'thumbnail'),
            call(image, image_handler, truck, 'small'),
            call(image, image_handler, truck, 'medium'),
            call(image, image_handler, truck, 'large'),
            ], any_order=True)
Exemplo n.º 8
0
    def test_resize_an_image(self):
        test_file = os.path.join(os.path.dirname(__file__),
                                 'test_image_640x427.jpg')
        image_handler = ImageHandler.open(test_file)
        truck = Mock()
        session = Client().session()
        image = ImageTable(filename='badcafe')
        session.add(image)
        session.flush()

        ResizeImage._resize_image(image, image_handler, truck, 'thumbnail')

        resizes = session.query(ImageResize).all()
        eq_(len(resizes), 1)
        eq_(resizes[0].width, 100)
        eq_(resizes[0].height, 66)
        eq_(resizes[0].suffix, 'thumbnail')

        (args, kwargs) = truck.upload_resize.call_args
        eq_(args[1], 'thumbnail')
Exemplo n.º 9
0
    def test_resize_an_image(self):
        image_handler = ImageHandler(filename=SMALL_JPG)
        truck = Mock()
        session = Client().session()
        image = ImageTable(filename='badcafe')
        session.add(image)
        session.flush()

        after_upload = Mock()
        ResizeImage._resize_image(image,
                                  image_handler,
                                  truck,
                                  'thumbnail',
                                  after_upload)

        resizes = session.query(ImageResize).all()
        eq_(len(resizes), 1)
        eq_(resizes[0].width, 100)
        eq_(resizes[0].height, 66)
        eq_(resizes[0].suffix, 'thumbnail')

        (args, kwargs) = truck.upload_resize.call_args
        eq_(args[1], 'thumbnail')
        after_upload.assert_called_once_with('thumbnail')