optimize images at least for establishments
This commit is contained in:
parent
391df439c6
commit
ea485d9960
|
|
@ -122,7 +122,7 @@ class EstablishmentQuerySet(models.QuerySet):
|
|||
def with_base_related(self):
|
||||
"""Return qs with related objects."""
|
||||
return self.select_related('address', 'establishment_type'). \
|
||||
prefetch_related('tags', 'tags__translation')
|
||||
prefetch_related('tags', 'tags__translation').with_main_image()
|
||||
|
||||
def with_schedule(self):
|
||||
"""Return qs with related schedule."""
|
||||
|
|
@ -506,6 +506,13 @@ class EstablishmentQuerySet(models.QuerySet):
|
|||
to_attr=attr_name)
|
||||
)
|
||||
|
||||
def with_main_image(self):
|
||||
return self.prefetch_related(
|
||||
models.Prefetch('establishment_gallery',
|
||||
queryset=EstablishmentGallery.objects.main_image(),
|
||||
to_attr='main_image')
|
||||
)
|
||||
|
||||
|
||||
class Establishment(GalleryMixin, ProjectBaseMixin, URLImageMixin,
|
||||
TranslatedFieldsMixin, HasTagsMixin, FavoritesMixin):
|
||||
|
|
@ -772,7 +779,8 @@ class Establishment(GalleryMixin, ProjectBaseMixin, URLImageMixin,
|
|||
return self.products.wines()
|
||||
|
||||
@property
|
||||
def main_image(self):
|
||||
def _main_image(self):
|
||||
"""Please consider using prefetched query_set instead due to API performance issues"""
|
||||
qs = self.establishment_gallery.main_image()
|
||||
image_model = qs.first()
|
||||
if image_model is not None:
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
"""Establishment serializers."""
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from phonenumber_field.phonenumber import to_python as str_to_phonenumber
|
||||
from rest_framework import serializers
|
||||
|
|
@ -19,6 +22,7 @@ from utils.serializers import ImageBaseSerializer, CarouselCreateSerializer
|
|||
from utils.serializers import (ProjectModelSerializer, TranslatedField,
|
||||
FavoritesCreateSerializer)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class ContactPhonesSerializer(serializers.ModelSerializer):
|
||||
"""Contact phone serializer"""
|
||||
|
|
@ -319,16 +323,46 @@ class EstablishmentBaseSerializer(ProjectModelSerializer):
|
|||
currency = CurrencySerializer()
|
||||
type = EstablishmentTypeBaseSerializer(source='establishment_type', read_only=True)
|
||||
subtypes = EstablishmentSubTypeBaseSerializer(many=True, source='establishment_subtypes')
|
||||
image = serializers.URLField(source='image_url', read_only=True)
|
||||
image = serializers.SerializerMethodField(read_only=True)
|
||||
wine_regions = EstablishmentWineRegionBaseSerializer(many=True, source='wine_origins_unique',
|
||||
read_only=True, allow_null=True)
|
||||
preview_image = serializers.URLField(source='preview_image_url',
|
||||
allow_null=True,
|
||||
read_only=True)
|
||||
tz = serializers.CharField(read_only=True, source='timezone_as_str')
|
||||
new_image = ImageBaseSerializer(source='crop_main_image', allow_null=True, read_only=True)
|
||||
new_image = serializers.SerializerMethodField(allow_null=True, read_only=True)
|
||||
distillery_type = TagBaseSerializer(read_only=True, many=True, allow_null=True)
|
||||
|
||||
def get_image(self, obj):
|
||||
if obj.main_image:
|
||||
return obj.main_image[0].image.image.url if len(obj.main_image) else None
|
||||
logging.info('Possibly not optimal image reading')
|
||||
return obj._main_image # backwards compatibility
|
||||
|
||||
def get_new_image(self, obj):
|
||||
if hasattr(self, 'main_image') and hasattr(self, '_meta'):
|
||||
if obj.main_image and len(obj.main_image):
|
||||
main_image = obj.main_image[0].image
|
||||
else:
|
||||
logging.info('Possibly not optimal image reading')
|
||||
main_image = obj._main_image
|
||||
if main_image:
|
||||
image = main_image
|
||||
image_property = {
|
||||
'id': image.id,
|
||||
'title': image.title,
|
||||
'original_url': image.image.url,
|
||||
'orientation_display': image.get_orientation_display(),
|
||||
'auto_crop_images': {},
|
||||
}
|
||||
crop_parameters = [p for p in settings.SORL_THUMBNAIL_ALIASES
|
||||
if p.startswith(self._meta.model_name.lower())]
|
||||
for crop in crop_parameters:
|
||||
image_property['auto_crop_images'].update(
|
||||
{crop: image.get_image_url(crop)}
|
||||
)
|
||||
return image_property
|
||||
|
||||
class Meta:
|
||||
"""Meta class."""
|
||||
|
||||
|
|
|
|||
|
|
@ -221,7 +221,7 @@ class SORLImageMixin(models.Model):
|
|||
"""Get image thumbnail url."""
|
||||
crop_image = self.get_image(thumbnail_key)
|
||||
if hasattr(crop_image, 'url'):
|
||||
return self.get_image(thumbnail_key).url
|
||||
return crop_image.url
|
||||
|
||||
def image_tag(self):
|
||||
"""Admin preview tag."""
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user