Esempio n. 1
0
 def validate_image(self, value):
     imgdata = BytesIO(base64.b64decode(value))
     try:
         img = Image.save_or_get(imgdata)
     except ImageError as ex:
         raise serializers.ValidationError(ex.message)
     return img
Esempio n. 2
0
    def test_size_limiting(self):
        img = Image.save_or_get(_getfile('test-big.png'))
        self.assertIsNotNone(img.id)

        self.assertTrue(img.width <= settings.MAX_IMAGE_SIZE[0])
        self.assertTrue(img.height <= settings.MAX_IMAGE_SIZE[1])
        self.assertTrue(img.width == settings.MAX_IMAGE_SIZE[0]
                        or img.height == settings.MAX_IMAGE_SIZE[1])
Esempio n. 3
0
def upload_image(image_file, uploader_uri, media_root=None, delete_original=False):
    """
    Upload image media API.
    """

    image_db = Image(image_file=image_file, uploader_uri=uploader_uri)
    image_db.save()

    original_image_file = '%s/%s' % (media_root, image_db.image_file.name, )
    image_file = process_image(original_image_file)

    image_file = image_file.replace(media_root, '')[1:]
    image_db.image_file.name = image_file
    image_db.save()

    if delete_original:
        os.remove(original_image_file)

    image_uri = _get_image_uri(image_db.id)

    return get_image(image_uri)
Esempio n. 4
0
    def test_image_saving(self):
        img = Image.save_or_get(_getfile('test1.png'))
        self.assertIsNotNone(img.id)

        self.assertEquals(img.width, 32)
        self.assertEquals(img.height, 48)

        img2 = Image.save_or_get(_getfile('test2.png'))
        self.assertIsNotNone(img2.id)
        self.assertNotEqual(img2.id, img.id)
        self.assertEquals(img2.width, 32)
        self.assertEquals(img2.height, 32)

        # Identical images should be deduplicated
        img1_2 = Image.save_or_get(_getfile('test1.png'))
        self.assertEqual(img1_2.id, img.id)

        # Test JPEG and GIF formats
        img3 = Image.save_or_get(_getfile('test3.jpeg'))
        self.assertIsNotNone(img3.id)

        img4 = Image.save_or_get(_getfile('test4.gif'))
        self.assertIsNotNone(img4.id)
Esempio n. 5
0
def upload_image(request):
    if request.method == "POST":
        from forms import UploadImage
        form = UploadImage(request.POST, request.FILES)

        if form.is_valid():
            user_id = request.POST.get('user')
            user = User.objects.get(pk=int(user_id))

            new_image = Image(
                path=request.FILES['file'],
                title=request.FILES['file'].name,
                user=user
            )
            new_image.save()

            from django.conf import settings
            url = settings.MEDIA_URL + new_image.path.name

            response = {
                'status': 'true',
                'url': url
            }

            return HttpResponse(json.dumps(
                response), 200, content_type="application/json")

        else:
            print form.errors
            return HttpResponse(json.dumps(
                {'status': 'false', 'error': form.errors}
            ), 401, content_type="application/json")

    else:
        return HttpResponse(json.dumps(
            {'status': 'false'}), 401, content_type="application/json")
            # Download the image.
            img_web = urllib2.urlopen(image['src']).read()

            # Write the image to disk.
            # TODO(Varun): This variable gets a name.
            image_path = settings.MEDIA_ROOT + 'resource_thumbnail/tmp/' + str(key) + "-resource.jpg"
            localImage = open(image_path, 'w')
            localImage.write(img_web)
            localImage.close()

            final_image = ImageFile(open(image_path))

            # Upload new image as media item.
            new_image = Image(
                path=final_image,
                title=final_image.name,
                user=user
            )
            new_image.save()

            url = settings.MEDIA_URL + new_image.path.name
            image['src'] = url

            # Delete the image from disk.
            os.remove(image_path)

    contents = task_body.find('div', class_="taskContents")
    contents['class'] = 'task-contents'
    task.append(remove_class(contents))

    commentary = task_body.find('div', class_="taskCommentary")