reassign sorl-thumbnail engine
This commit is contained in:
parent
c2bb9f0814
commit
65bb22bb21
|
|
@ -39,11 +39,11 @@ class ImageSerializer(serializers.ModelSerializer):
|
|||
class CropImageSerializer(ImageSerializer):
|
||||
"""Serializers for image crops."""
|
||||
|
||||
width = serializers.IntegerField(write_only=True)
|
||||
height = serializers.IntegerField(write_only=True)
|
||||
margin = serializers.CharField(write_only=True, allow_null=True,
|
||||
required=False,
|
||||
default='center')
|
||||
width = serializers.IntegerField(write_only=True, required=False)
|
||||
height = serializers.IntegerField(write_only=True, required=False)
|
||||
crop = serializers.CharField(write_only=True, allow_null=True,
|
||||
required=False,
|
||||
default='center')
|
||||
quality = serializers.IntegerField(write_only=True, allow_null=True, required=False,
|
||||
default=settings.THUMBNAIL_QUALITY,
|
||||
validators=[
|
||||
|
|
@ -59,7 +59,7 @@ class CropImageSerializer(ImageSerializer):
|
|||
'orientation_display',
|
||||
'width',
|
||||
'height',
|
||||
'margin',
|
||||
'crop',
|
||||
'quality',
|
||||
'cropped_image',
|
||||
]
|
||||
|
|
@ -69,16 +69,16 @@ class CropImageSerializer(ImageSerializer):
|
|||
file = self._image.image
|
||||
crop_width = attrs.get('width')
|
||||
crop_height = attrs.get('height')
|
||||
margin = attrs.get('margin')
|
||||
crop = attrs.get('crop')
|
||||
|
||||
if crop_height and crop_width and margin:
|
||||
if crop_height and crop_width and crop:
|
||||
xy_image = (file.width, file.width)
|
||||
xy_window = (crop_width, crop_height)
|
||||
try:
|
||||
parse_crop(margin, xy_image, xy_window)
|
||||
parse_crop(crop, xy_image, xy_window)
|
||||
attrs['image'] = file
|
||||
except ThumbnailParseError:
|
||||
raise serializers.ValidationError({'margin': _('Unrecognized crop option: %s') % margin})
|
||||
raise serializers.ValidationError({'margin': _('Unrecognized crop option: %s') % crop})
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data):
|
||||
|
|
@ -86,7 +86,7 @@ class CropImageSerializer(ImageSerializer):
|
|||
width = validated_data.pop('width', None)
|
||||
height = validated_data.pop('height', None)
|
||||
quality = validated_data.pop('quality')
|
||||
margin = validated_data.pop('margin')
|
||||
crop = validated_data.pop('crop')
|
||||
|
||||
image = self._image
|
||||
|
||||
|
|
@ -96,7 +96,7 @@ class CropImageSerializer(ImageSerializer):
|
|||
image.get_cropped_image(
|
||||
geometry=f'{width}x{height}',
|
||||
quality=quality,
|
||||
margin=margin))
|
||||
crop=crop))
|
||||
return image
|
||||
|
||||
@property
|
||||
|
|
|
|||
|
|
@ -11,11 +11,11 @@ from django.contrib.postgres.fields.jsonb import KeyTextTransform
|
|||
from django.utils import timezone
|
||||
from django.utils.html import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _, get_language
|
||||
from configuration.models import TranslationSettings
|
||||
from easy_thumbnails.fields import ThumbnailerImageField
|
||||
from sorl.thumbnail import get_thumbnail
|
||||
from sorl.thumbnail.fields import ImageField as SORLImageField
|
||||
|
||||
from configuration.models import TranslationSettings
|
||||
from utils.methods import image_path, svg_image_path
|
||||
from utils.validators import svg_image_validator
|
||||
|
||||
|
|
@ -227,16 +227,16 @@ class SORLImageMixin(models.Model):
|
|||
else:
|
||||
return None
|
||||
|
||||
def get_cropped_image(self, geometry: str, quality: int, margin: str) -> dict:
|
||||
def get_cropped_image(self, geometry: str, quality: int, crop: str) -> dict:
|
||||
cropped_image = get_thumbnail(self.image,
|
||||
geometry_string=geometry,
|
||||
crop=margin,
|
||||
crop=crop,
|
||||
quality=quality)
|
||||
return {
|
||||
'geometry_string': geometry,
|
||||
'crop_url': cropped_image.url,
|
||||
'quality': quality,
|
||||
'margin': margin
|
||||
'crop': crop
|
||||
}
|
||||
|
||||
image_tag.short_description = _('Image')
|
||||
|
|
@ -455,4 +455,5 @@ class FavoritesMixin:
|
|||
def favorites_for_users(self):
|
||||
return self.favorites.aggregate(arr=ArrayAgg('user_id')).get('arr')
|
||||
|
||||
|
||||
timezone.datetime.now().date().isoformat()
|
||||
19
apps/utils/thumbnail_engine.py
Normal file
19
apps/utils/thumbnail_engine.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
"""Overridden thumbnail engine."""
|
||||
from sorl.thumbnail.engines.pil_engine import Engine as PILEngine
|
||||
|
||||
|
||||
class GMEngine(PILEngine):
|
||||
|
||||
def create(self, image, geometry, options):
|
||||
"""
|
||||
Processing conductor, returns the thumbnail as an image engine instance
|
||||
"""
|
||||
image = self.cropbox(image, geometry, options)
|
||||
image = self.orientation(image, geometry, options)
|
||||
image = self.colorspace(image, geometry, options)
|
||||
image = self.remove_border(image, options)
|
||||
image = self.crop(image, geometry, options)
|
||||
image = self.rounded(image, geometry, options)
|
||||
image = self.blur(image, geometry, options)
|
||||
image = self.padding(image, geometry, options)
|
||||
return image
|
||||
|
|
@ -520,3 +520,4 @@ ESTABLISHMENT_CHOSEN_TAGS = ['gastronomic', 'en_vogue', 'terrace', 'streetfood',
|
|||
NEWS_CHOSEN_TAGS = ['eat', 'drink', 'cook', 'style', 'international', 'event', 'partnership']
|
||||
INTERNATIONAL_COUNTRY_CODES = ['www', 'main', 'next']
|
||||
|
||||
THUMBNAIL_ENGINE = 'utils.thumbnail_engine.GMEngine'
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user