Example #1
0
    def perform_create(self, serializer):
        """Download and validate image URL."""
        imgs = []
        for image_field, hash_field, suffix in self.image_fields:
            if serializer.validated_data.get(image_field):
                img_url = serializer.validated_data[image_field]
                img, hash_ = image_from_url(img_url)
                # Store img for `post_save` where we have access to the pk so
                # we can save img in appropriate directory.
                imgs.append((suffix, img, hash_))
                serializer.validated_data[hash_field] = hash_
            elif (
                serializer.validated_data.get("type")
                or (serializer.instance and getattr(serializer.instance, "type", None))
            ) == feed.COLLECTION_PROMO:
                # Remove background images for promo collections.
                serializer.validated_data[hash_field] = None
            if image_field in serializer.validated_data:
                del serializer.validated_data[image_field]

        obj = serializer.save()

        for suffix, image, hash_ in imgs:
            if image:
                i = Image.open(image)
                path = obj.image_path(suffix)
                with public_storage.open(path, "wb") as f:
                    i.save(f, "png")
                pngcrush_image.delay(path, set_modified_on=[obj])
Example #2
0
    def perform_create(self, serializer):
        """Download and validate image URL."""
        imgs = []
        for image_field, hash_field, suffix in self.image_fields:
            if serializer.validated_data.get(image_field):
                img_url = serializer.validated_data[image_field]
                img, hash_ = image_from_url(img_url)
                # Store img for `post_save` where we have access to the pk so
                # we can save img in appropriate directory.
                imgs.append((suffix, img, hash_))
                serializer.validated_data[hash_field] = hash_
            elif ((serializer.validated_data.get('type')
                   or (serializer.instance
                       and getattr(serializer.instance, 'type',
                                   None))) == feed.COLLECTION_PROMO):
                # Remove background images for promo collections.
                serializer.validated_data[hash_field] = None
            if image_field in serializer.validated_data:
                del serializer.validated_data[image_field]

        obj = serializer.save()

        for suffix, image, hash_ in imgs:
            if image:
                i = Image.open(image)
                path = obj.image_path(suffix)
                with public_storage.open(path, 'wb') as f:
                    i.save(f, 'png')
                pngcrush_image.delay(path, set_modified_on=[obj])
Example #3
0
    def post_save(self, obj, created=True):
        """Store background image that we attached to the obj in pre_save."""
        if hasattr(obj, '_background_image_upload'):
            i = Image.open(obj._background_image_upload)
            with storage.open(obj.image_path(), 'wb') as f:
                i.save(f, 'png')
            pngcrush_image.delay(obj.image_path(), set_modified_on=[obj])

        return super(ImageURLUploadMixin, self).post_save(obj, created)
Example #4
0
    def post_save(self, obj, created=True):
        """Store image that we attached to the obj in pre_save."""
        for image_field, hash_field, suffix in self.image_fields:
            image = getattr(obj, '_%s' % image_field, None)
            if image:
                i = Image.open(image)
                path = obj.image_path(suffix)
                with storage.open(path, 'wb') as f:
                    i.save(f, 'png')
                pngcrush_image.delay(path, set_modified_on=[obj])

        return super(ImageURLUploadMixin, self).post_save(obj, created)
Example #5
0
    def post_save(self, obj, created=True):
        """Store image that we attached to the obj in pre_save."""
        for image_field, hash_field, suffix in self.image_fields:
            image = getattr(obj, '_%s' % image_field, None)
            if image:
                i = Image.open(image)
                path = obj.image_path(suffix)
                with public_storage.open(path, 'wb') as f:
                    i.save(f, 'png')
                pngcrush_image.delay(path, set_modified_on=[obj])

        return super(ImageURLUploadMixin, self).post_save(obj, created)
Example #6
0
    def update(self, request, *args, **kwargs):
        obj = self.get_object()
        try:
            img, hash_ = DataURLImageField().from_native(request.read())
        except ValidationError:
            return Response(status=status.HTTP_400_BAD_REQUEST)
        i = Image.open(img)
        with storage.open(obj.image_path(self.image_suffix), 'wb') as f:
            i.save(f, 'png')
        # Store the hash of the original image data sent.
        obj.update(**{self.hash_field: hash_})

        pngcrush_image.delay(obj.image_path(self.image_suffix))
        return Response(status=status.HTTP_204_NO_CONTENT)
Example #7
0
    def update(self, request, *args, **kwargs):
        obj = self.get_object()
        try:
            img, hash_ = image_from_data_url(request.read())
        except ValidationError:
            return Response(status=status.HTTP_400_BAD_REQUEST)
        i = Image.open(img)
        with public_storage.open(obj.image_path(self.image_suffix), 'wb') as f:
            i.save(f, 'png')
        # Store the hash of the original image data sent.
        obj.update(**{self.hash_field: hash_})

        pngcrush_image.delay(obj.image_path(self.image_suffix))
        return Response(status=status.HTTP_204_NO_CONTENT)
Example #8
0
def _regenerate_icons_and_thumbnails(pk):
    try:
        webapp = Webapp.objects.get(pk=pk)
    except Webapp.DoesNotExist:
        _log(id, u'Webapp does not exist')
        return

    # Previews.
    for preview in webapp.all_previews:
        # Re-resize each preview by calling the task with the image that we
        # have and asking the task to only deal with the thumbnail. We no
        # longer have the original, but it's fine, the image should be large
        # enough for us to generate a thumbnail.
        resize_preview.delay(preview.image_path, preview, generate_image=False)

    # Icons. The only thing we need to do is crush the 64x64 icon.
    icon_path = os.path.join(webapp.get_icon_dir(), '%s-64.png' % webapp.id)
    pngcrush_image.delay(icon_path)
Example #9
0
def _regenerate_icons_and_thumbnails(pk):
    try:
        webapp = Webapp.objects.get(pk=pk)
    except Webapp.DoesNotExist:
        _log(id, u'Webapp does not exist')
        return

    # Previews.
    for preview in webapp.all_previews:
        # Re-resize each preview by calling the task with the image that we
        # have and asking the task to only deal with the thumbnail. We no
        # longer have the original, but it's fine, the image should be large
        # enough for us to generate a thumbnail.
        resize_preview.delay(preview.image_path, preview, generate_image=False)

    # Icons. The only thing we need to do is crush the 64x64 icon.
    icon_path = os.path.join(webapp.get_icon_dir(), '%s-64.png' % webapp.id)
    pngcrush_image.delay(icon_path)