當前位置: 首頁>>代碼示例>>Python>>正文


Python image_truck.ImageTruck類代碼示例

本文整理匯總了Python中catsnap.image_truck.ImageTruck的典型用法代碼示例。如果您正苦於以下問題:Python ImageTruck類的具體用法?Python ImageTruck怎麽用?Python ImageTruck使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了ImageTruck類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: process_image

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)
開發者ID:ErinCall,項目名稱:catsnap,代碼行數:35,代碼來源:tasks.py

示例2: add

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()}
開發者ID:pythonchelle,項目名稱:catsnap,代碼行數:35,代碼來源:image.py

示例3: test_url_for_filename__with_extension

    def test_url_for_filename__with_extension(self, _url, Config):
        config = Mock()
        config.bucket_name.return_value = 'greeble'
        Config.return_value = config

        ImageTruck.url_for_filename('CAFEBABE', extension=True)
        _url.assert_called_once_with('CAFEBABE', 'greeble', extension=True)
開發者ID:hmwalls,項目名稱:catsnap,代碼行數:7,代碼來源:test_image_truck.py

示例4: test_calculate_filename

 def test_calculate_filename(self, hashlib):
     sha = Mock()
     sha.hexdigest.return_value = 'indigestible'
     hashlib.sha1.return_value = sha
     truck = ImageTruck('razors', None, None)
     eq_(truck.calculate_filename(), 'indigestible')
     hashlib.sha1.assert_called_with('razors')
開發者ID:pythonchelle,項目名稱:catsnap,代碼行數:7,代碼來源:test_image_truck.py

示例5: add

def add(request_format):
    url = request.form.get("url")

    if url:
        try:
            trucks = [ImageTruck.new_from_url(url)]
        except RequestException:
            abort(request_format, 400, "That url is no good.")
        except TryHTTPError:
            abort(
                request_format,
                400,
                "Catsnap couldn't establish an HTTPS connection to that "
                "image. An HTTP connection may succeed (this is a problem "
                "on Catsnap's end, not something you did wrong).",
            )
    elif request.files.get("file[]"):
        trucks = [ImageTruck.new_from_stream(data.stream) for data in request.files.getlist("file[]")]
    elif request.files.get("file"):
        data = request.files["file"]
        trucks = [ImageTruck.new_from_stream(data.stream)]
    else:
        abort(request_format, 400, "Please submit either a file or a url.")

    # These loops are sorta awkwardly phrased to avoid lots of round-tripping
    # to the database. I hope you don't consider the optimization premature.
    session = Client().session()
    images = []
    for truck in trucks:
        image = Image(filename=truck.filename, source_url=url)
        album_id = request.form.get("album_id")
        if album_id:
            image.album_id = int(album_id)
        session.add(image)
        images.append(image)

    session.flush()
    contentses = []
    for i in xrange(0, len(images)):
        (truck, image) = trucks[i], images[i]
        contents = ImageContents(image_id=image.image_id, contents=truck.contents, content_type=truck.content_type)
        session.add(contents)
        contentses.append(contents)
    session.flush()

    task_ids = []
    # Hey, this is a loop around a round-trip to redis. Although the Python
    # Redis library offers a way to send multiple requests in one thwack,
    # Celery doesn't appear to offer a way to use it. Nothing to be done.
    for contents in contentses:
        task_ids.append(delay(g.queued_tasks, process_image, contents.image_contents_id))

    if request_format == "html":
        return redirect(url_for("show_image", image_id=image.image_id))
    elif request_format == "json":
        return [
            {"url": trucks[i].url(), "image_id": images[i].image_id, "task_id": task_ids[i]}
            for i in xrange(0, len(trucks))
        ]
開發者ID:ErinCall,項目名稱:catsnap,代碼行數:59,代碼來源:image.py

示例6: test_get_contents_of_nonexistent_filename

    def test_get_contents_of_nonexistent_filename(self, MockClient):
        bucket = Mock()
        bucket.get_key.return_value = None
        client = Mock()
        client.bucket.return_value = bucket
        MockClient.return_value = client

        ImageTruck.contents_of_filename('x')
開發者ID:pythonchelle,項目名稱:catsnap,代碼行數:8,代碼來源:test_image_truck.py

示例7: test_url

    def test_url(self, calculate_filename, Config):
        config = Mock()
        config.bucket_name.return_value = 'tune-carrier'
        Config.return_value = config
        calculate_filename.return_value = 'greensleeves'

        truck = ImageTruck('greensleeves', None, None)
        eq_(truck.url(), 'https://s3.amazonaws.com/tune-carrier/greensleeves')
開發者ID:hmwalls,項目名稱:catsnap,代碼行數:8,代碼來源:test_image_truck.py

示例8: test_new_from_url_reraises_non_sni_ssl_errors

    def test_new_from_url_reraises_non_sni_ssl_errors(self, mock_requests):
        error = requests.exceptions.SSLError(
            urllib3.exceptions.SSLError(
                CertificateError("hostname 'catsinthecity.com' doesn't "
                                 "match 'nossl.edgecastcdn.net'")))

        mock_requests.get.side_effect = error

        ImageTruck.new_from_url('https://catsinthecity.com/image.jpg')
開發者ID:ErinCall,項目名稱:catsnap,代碼行數:9,代碼來源:test_image_truck.py

示例9: add

def add(request_format):
    url = request.form.get('url')

    if url:
        try:
            trucks = [ImageTruck.new_from_url(url)]
        except RequestException:
            abort(request_format, 400, "That url is no good.")
    elif request.files.get('file[]'):
        trucks = [ImageTruck.new_from_stream(data.stream)
                  for data in request.files.getlist('file[]')]
    elif request.files.get('file'):
        data = request.files['file']
        trucks = [ImageTruck.new_from_stream(data.stream)]
    else:
        abort(request_format, 400, "Please submit either a file or a url.")

    # These loops are sorta awkwardly phrased to avoid lots of round-tripping
    # to the database. I hope you don't consider the optimization premature.
    session = Client().session()
    images = []
    for truck in trucks:
        image = Image(filename=truck.filename, source_url=url)
        album_id = request.form.get('album_id')
        if album_id:
            image.album_id = int(album_id)
        session.add(image)
        images.append(image)

    session.flush()
    contentses = []
    for i in xrange(0, len(images)):
        (truck, image) = trucks[i], images[i]
        contents = ImageContents(image_id=image.image_id,
                                 contents=truck.contents,
                                 content_type=truck.content_type)
        session.add(contents)
        contentses.append(contents)
    session.flush()

    task_ids = []
    # Hey, this is a loop around a round-trip to redis. Although the Python
    # Redis library offers a way to send multiple requests in one thwack,
    # Celery doesn't appear to offer a way to use it. Nothing to be done.
    for contents in contentses:
        task_ids.append(delay(g.queued_tasks,
                              process_image,
                              contents.image_contents_id))

    if request_format == 'html':
        return redirect(url_for('show_image', image_id=image.image_id))
    elif request_format == 'json':
        return [{
                'url': trucks[i].url(),
                'image_id': images[i].image_id,
                'task_id': task_ids[i],
            } for i in xrange(0, len(trucks))]
開發者ID:ErinCall,項目名稱:catsnap,代碼行數:57,代碼來源:image.py

示例10: test_new_from_url_raises_usefully_for_sni_trouble

    def test_new_from_url_raises_usefully_for_sni_trouble(self, mock_requests):
        error = requests.exceptions.SSLError(
            urllib3.exceptions.SSLError(
                ssl.SSLError(1, '_ssl.c:503: error:14077410:SSL routines:'
                                'SSL23_GET_SERVER_HELLO:sslv3 alert handshake '
                                'failure')))

        mock_requests.get.side_effect = error

        ImageTruck.new_from_url('https://some.server.using.sni/image.jpg')
開發者ID:ErinCall,項目名稱:catsnap,代碼行數:10,代碼來源:test_image_truck.py

示例11: test_url__with_extension

    def test_url__with_extension(self, calculate_filename, Config, _url):
        config = Mock()
        config.bucket_name.return_value = 'tune-carrier'
        Config.return_value = config
        calculate_filename.return_value = 'greensleeves'
        truck = ImageTruck('greensleeves', None, None)

        truck.url(extension=True)
        _url.assert_called_once_with('greensleeves', 'tune-carrier',
                extension=True)
開發者ID:hmwalls,項目名稱:catsnap,代碼行數:10,代碼來源:test_image_truck.py

示例12: test_get_bucket_creates_bucket_connection

    def test_get_bucket_creates_bucket_connection(self, Config):
        config = Mock()
        Config.return_value = config
        mock_bucket = Mock()
        truck = ImageTruck(None, None, None)
        truck._stored_bucket = mock_bucket

        bucket = truck._bucket()
        eq_(bucket, mock_bucket)
        eq_(config.table.call_count, 0)
開發者ID:hmwalls,項目名稱:catsnap,代碼行數:10,代碼來源:test_image_truck.py

示例13: test_url__with_cloudfront_url

    def test_url__with_cloudfront_url(self, calculate_filename, MockClient):
        client = Mock()
        client.cloudfront_url.return_value = \
            'ggaaghlhaagl.cloudfront.net'
        client.config.return_value = Client().config()
        MockClient.return_value = client

        calculate_filename.return_value = 'chumbawamba'
        truck = ImageTruck('tubthumper', None, None)

        url = truck.url()
        eq_(url, 'https://ggaaghlhaagl.cloudfront.net/chumbawamba')
        client.cloudfront_url.assert_called_with('JEEZAMANDA')
開發者ID:ErinCall,項目名稱:catsnap,代碼行數:13,代碼來源:test_image_truck.py

示例14: test_save__uploads_image

    def test_save__uploads_image(self, calculate_filename):
        bucket = Mock()
        key = Mock()
        bucket.new_key.return_value = key
        calculate_filename.return_value = 'I am the keymaster'

        truck = ImageTruck('Are you the gatekeeper?', 'image/gif', None)
        truck._stored_bucket = bucket
        truck.upload()

        bucket.new_key.assert_called_with('I am the keymaster')
        key.set_contents_from_string.assert_called_with(
                'Are you the gatekeeper?')
        key.set_metadata.assert_called_with('Content-Type', 'image/gif')
        key.make_public.assert_called_with()
開發者ID:hmwalls,項目名稱:catsnap,代碼行數:15,代碼來源:test_image_truck.py

示例15: test_upload_resize

    def test_upload_resize(self, calculate_filename, MockClient):
        bucket = Mock()
        key = Mock()
        bucket.new_key.return_value = key
        client = Mock()
        client.bucket.return_value = bucket
        MockClient.return_value = client
        calculate_filename.return_value = 'faceb00c'

        truck = ImageTruck('contentsoffile', 'image/gif', None)
        truck.upload_resize('resizedcontents', 'small')

        bucket.new_key.assert_called_with('faceb00c_small')
        key.set_contents_from_string.assert_called_with('resizedcontents')
        key.set_metadata.assert_called_with('Content-Type', 'image/gif')
        key.make_public.assert_called_once()
開發者ID:pythonchelle,項目名稱:catsnap,代碼行數:16,代碼來源:test_image_truck.py


注:本文中的catsnap.image_truck.ImageTruck類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。