cropbox image
This commit is contained in:
parent
3da4dfd54b
commit
8f9b1f8145
18
apps/gallery/migrations/0010_auto_20200206_1944.py
Normal file
18
apps/gallery/migrations/0010_auto_20200206_1944.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 2.2.7 on 2020-02-06 19:44
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('gallery', '0009_auto_20200206_1749'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='image',
|
||||
name='is_public',
|
||||
field=models.BooleanField(default=True, verbose_name='Is media source public'),
|
||||
),
|
||||
]
|
||||
20
apps/gallery/migrations/0011_image_cropbox.py
Normal file
20
apps/gallery/migrations/0011_image_cropbox.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# Generated by Django 2.2.7 on 2020-02-08 19:00
|
||||
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
import re
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('gallery', '0010_auto_20200206_1944'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='image',
|
||||
name='cropbox',
|
||||
field=models.CharField(default=None, max_length=500, null=True, validators=[django.core.validators.RegexValidator(re.compile('^\\d+(?:,\\d+)*\\Z'), code='invalid', message='Enter only digits separated by commas.')], verbose_name='x1,y1,x2,y2 crop settings'),
|
||||
),
|
||||
]
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.core import validators
|
||||
from sorl.thumbnail import get_thumbnail
|
||||
from botocore.exceptions import ClientError
|
||||
from django.conf import settings
|
||||
from project.storage_backends import PublicMediaStorage
|
||||
|
|
@ -43,6 +45,8 @@ class Image(BaseAttributes, SORLImageMixin, PlatformMixin):
|
|||
default=None)
|
||||
link = models.URLField(blank=True, null=True, default=None, verbose_name=_('mp4 or youtube video link'))
|
||||
order = models.PositiveIntegerField(default=0, verbose_name=_('Sorting order'))
|
||||
cropbox = models.CharField(max_length=500, validators=[validators.validate_comma_separated_integer_list], null=True,
|
||||
default=None, verbose_name=_('x1,y1,x2,y2 crop settings'))
|
||||
objects = ImageQuerySet.as_manager()
|
||||
|
||||
class Meta:
|
||||
|
|
@ -55,6 +59,16 @@ class Image(BaseAttributes, SORLImageMixin, PlatformMixin):
|
|||
"""String representation"""
|
||||
return f'{self.id}'
|
||||
|
||||
@property
|
||||
def image_by_cropbox(self):
|
||||
"""Returns cropped image if cropbox is set"""
|
||||
if self.cropbox and self.image:
|
||||
x1, y1, x2, y2 = map(int, self.cropbox.split(','))
|
||||
return get_thumbnail(self.image,
|
||||
geometry_string=f'{round(x2 - x1)}x{round(y2 - y1)}',
|
||||
cropbox=self.cropbox,
|
||||
quality=100)
|
||||
|
||||
def set_pubic(self, is_public=True):
|
||||
if not settings.AWS_STORAGE_BUCKET_NAME:
|
||||
"""Backend doesn't use aws s3"""
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ class EstablishmentGallerySerializer(serializers.ModelSerializer):
|
|||
created_by = UserBaseSerializer(read_only=True, allow_null=True)
|
||||
image_size_in_KB = serializers.DecimalField(read_only=True, decimal_places=2, max_digits=20)
|
||||
is_main = serializers.BooleanField()
|
||||
cropped_image = serializers.ImageField(source='image_by_cropbox', allow_null=True, read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = models.Image
|
||||
|
|
@ -69,6 +70,8 @@ class EstablishmentGallerySerializer(serializers.ModelSerializer):
|
|||
'is_main',
|
||||
'created_by',
|
||||
'image_size_in_KB',
|
||||
'cropbox',
|
||||
'cropped_image',
|
||||
)
|
||||
extra_kwargs = {
|
||||
'created': {'read_only': True},
|
||||
|
|
@ -80,7 +83,9 @@ class EstablishmentGallerySerializer(serializers.ModelSerializer):
|
|||
|
||||
if image and image.size >= settings.FILE_UPLOAD_MAX_MEMORY_SIZE:
|
||||
raise serializers.ValidationError({'detail': _('File size too large: %s bytes') % image.size})
|
||||
|
||||
if attrs.get('cropbox'):
|
||||
if len(attrs['cropbox'].split(',')) != 4:
|
||||
raise serializers.ValidationError({'detail': _('Cropbox contains 4 integer values separated by comma.')})
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data):
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user