From f4b411e73d3374fe24ecf1fa9d8752fea135d2f8 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Mon, 11 Nov 2019 21:29:34 +0300 Subject: [PATCH] added tags, image urls to product serializers --- apps/collection/models.py | 2 +- apps/product/models.py | 15 ++++++++++++++- apps/product/serializers/common.py | 8 ++++++-- project/settings/base.py | 1 + 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/apps/collection/models.py b/apps/collection/models.py index a051a5c1..d98f8a59 100644 --- a/apps/collection/models.py +++ b/apps/collection/models.py @@ -75,8 +75,8 @@ class Collection(ProjectBaseMixin, CollectionDateMixin, default=None, help_text='{"en-GB":"some text"}') slug = models.SlugField(max_length=50, unique=True, verbose_name=_('Collection slug'), editable=True, null=True) + old_id = models.IntegerField(null=True, blank=True) - old_id=models.IntegerField(null=True, blank=True) objects = CollectionQuerySet.as_manager() class Meta: diff --git a/apps/product/models.py b/apps/product/models.py index fee50169..f2a50935 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -245,7 +245,20 @@ class Product(TranslatedFieldsMixin, BaseAttributes): def main_image(self): qs = ProductGallery.objects.filter(product=self, is_main=True) if qs.exists(): - return qs.first().image.image + return qs.first().image + + @property + def main_image_url(self): + return self.main_image.image if self.main_image else None + + @property + def preview_main_image_url(self): + return self.main_image.get_image_url('product_preview') if self.main_image else None + + @property + def related_tags(self): + return self.tags.exclude( + category__index_name__in=['sugar-content', 'wine-color', 'bottles-produced']) class OnlineProductManager(ProductManager): diff --git a/apps/product/serializers/common.py b/apps/product/serializers/common.py index 959bc3d3..690aea99 100644 --- a/apps/product/serializers/common.py +++ b/apps/product/serializers/common.py @@ -75,6 +75,8 @@ class ProductBaseSerializer(serializers.ModelSerializer): product_type = serializers.CharField(source='product_type_translated_name') subtypes = ProductSubTypeBaseSerializer(many=True) establishment = EstablishmentShortSerializer() + tags = TagBaseSerializer(source='related_tags', many=True) + preview_image_url = serializers.ImageField(source='preview_main_image_url', allow_null=True) class Meta: """Meta class.""" @@ -88,6 +90,8 @@ class ProductBaseSerializer(serializers.ModelSerializer): 'public_mark', 'establishment', 'vintage', + 'tags', + 'preview_image_url', ] @@ -104,7 +108,7 @@ class ProductDetailSerializer(ProductBaseSerializer): wine_colors = TagBaseSerializer(many=True) bottles_produced = TagBaseSerializer(many=True) sugar_contents = TagBaseSerializer(many=True) - main_image = serializers.ImageField(allow_null=True) + image_url = serializers.ImageField(source='main_image_url', allow_null=True) class Meta(ProductBaseSerializer.Meta): fields = ProductBaseSerializer.Meta.fields + [ @@ -118,7 +122,7 @@ class ProductDetailSerializer(ProductBaseSerializer): 'wine_colors', 'bottles_produced', 'sugar_contents', - 'main_image', + 'image_url', ] diff --git a/project/settings/base.py b/project/settings/base.py index ad96a2fe..51920866 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -357,6 +357,7 @@ THUMBNAIL_ALIASES = { 'news_editor_web': {'size': (940, 430), }, # при загрузке через контент эдитор 'news_editor_mobile': {'size': (343, 260), }, # через контент эдитор в мобильном браузерe 'avatar_comments_web': {'size': (116, 116), }, + 'product_preview': {'size': (300, 260), }, } }