259 lines
8.7 KiB
Python
259 lines
8.7 KiB
Python
"""Product app serializers."""
|
|
from django.utils.translation import gettext_lazy as _
|
|
from rest_framework import serializers
|
|
|
|
from comment.models import Comment
|
|
from comment.serializers import CommentSerializer
|
|
from establishment.serializers import EstablishmentShortSerializer, EstablishmentProductSerializer
|
|
from gallery.models import Image
|
|
from product import models
|
|
from review.serializers import ReviewShortSerializer
|
|
from utils import exceptions as utils_exceptions
|
|
from utils.serializers import TranslatedField, FavoritesCreateSerializer
|
|
from main.serializers import AwardSerializer
|
|
from location.serializers import WineRegionBaseSerializer, WineSubRegionBaseSerializer
|
|
from tag.serializers import TagBaseSerializer
|
|
|
|
|
|
class ProductSubTypeBaseSerializer(serializers.ModelSerializer):
|
|
"""ProductSubType base serializer"""
|
|
name_translated = TranslatedField()
|
|
index_name_display = serializers.CharField(source='get_index_name_display')
|
|
|
|
class Meta:
|
|
model = models.ProductSubType
|
|
fields = [
|
|
'id',
|
|
'name_translated',
|
|
'index_name_display',
|
|
]
|
|
|
|
|
|
class ProductTypeBaseSerializer(serializers.ModelSerializer):
|
|
"""ProductType base serializer"""
|
|
name_translated = TranslatedField()
|
|
index_name_display = serializers.CharField(source='get_index_name_display')
|
|
|
|
class Meta:
|
|
model = models.ProductType
|
|
fields = [
|
|
'id',
|
|
'name_translated',
|
|
'index_name_display',
|
|
]
|
|
|
|
|
|
class ProductClassificationBaseSerializer(serializers.ModelSerializer):
|
|
"""Serializer for model ProductClassification."""
|
|
|
|
name = serializers.CharField(source='classification_type.name')
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
model = models.ProductClassification
|
|
fields = (
|
|
'name',
|
|
)
|
|
|
|
|
|
class ProductStandardBaseSerializer(serializers.ModelSerializer):
|
|
"""Serializer for model ProductStandard."""
|
|
|
|
standard_type = serializers.CharField(source='get_standard_type_display')
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
model = models.ProductStandard
|
|
fields = (
|
|
'name',
|
|
'standard_type',
|
|
)
|
|
|
|
|
|
class ProductBaseSerializer(serializers.ModelSerializer):
|
|
"""Product base serializer."""
|
|
product_type = serializers.CharField(source='product_type_translated_name')
|
|
subtypes = ProductSubTypeBaseSerializer(many=True)
|
|
establishment = EstablishmentShortSerializer()
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
model = models.Product
|
|
fields = [
|
|
'id',
|
|
'slug',
|
|
'name',
|
|
'product_type',
|
|
'subtypes',
|
|
'public_mark',
|
|
'establishment',
|
|
'vintage',
|
|
]
|
|
|
|
|
|
class ProductDetailSerializer(ProductBaseSerializer):
|
|
"""Product detail serializer."""
|
|
description_translated = TranslatedField()
|
|
review = ReviewShortSerializer(source='last_published_review')
|
|
awards = AwardSerializer(many=True)
|
|
establishment = EstablishmentProductSerializer()
|
|
classifications = ProductClassificationBaseSerializer(many=True)
|
|
standards = ProductStandardBaseSerializer(many=True)
|
|
wine_region = WineRegionBaseSerializer()
|
|
wine_sub_region = WineSubRegionBaseSerializer()
|
|
wine_colors = TagBaseSerializer(many=True)
|
|
bottles_produced = TagBaseSerializer(many=True)
|
|
sugar_contents = TagBaseSerializer(many=True)
|
|
main_image = serializers.ImageField(allow_null=True)
|
|
|
|
class Meta(ProductBaseSerializer.Meta):
|
|
fields = ProductBaseSerializer.Meta.fields + [
|
|
'description_translated',
|
|
'review',
|
|
'awards',
|
|
'classifications',
|
|
'standards',
|
|
'wine_region',
|
|
'wine_sub_region',
|
|
'wine_colors',
|
|
'bottles_produced',
|
|
'sugar_contents',
|
|
'main_image',
|
|
]
|
|
|
|
|
|
class ProductFavoritesCreateSerializer(FavoritesCreateSerializer):
|
|
"""Serializer to create favorite object w/ model Product."""
|
|
|
|
def validate(self, attrs):
|
|
"""Overridden validate method"""
|
|
# Check product object
|
|
product_qs = models.Product.objects.filter(slug=self.slug)
|
|
|
|
# Check product obj by slug from lookup_kwarg
|
|
if not product_qs.exists():
|
|
raise serializers.ValidationError({'detail': _('Object not found.')})
|
|
else:
|
|
product = product_qs.first()
|
|
|
|
# Check existence in favorites
|
|
if product.favorites.filter(user=self.user).exists():
|
|
raise utils_exceptions.FavoritesError()
|
|
|
|
attrs['product'] = product
|
|
return attrs
|
|
|
|
def create(self, validated_data, *args, **kwargs):
|
|
"""Overridden create method"""
|
|
validated_data.update({
|
|
'user': self.user,
|
|
'content_object': validated_data.pop('product')
|
|
})
|
|
return super().create(validated_data)
|
|
|
|
|
|
# class CropImageSerializer(serializers.Serializer):
|
|
# """Serializer for crop images for News object."""
|
|
#
|
|
# preview_url = serializers.SerializerMethodField()
|
|
# promo_horizontal_web_url = serializers.SerializerMethodField()
|
|
# promo_horizontal_mobile_url = serializers.SerializerMethodField()
|
|
# tile_horizontal_web_url = serializers.SerializerMethodField()
|
|
# tile_horizontal_mobile_url = serializers.SerializerMethodField()
|
|
# tile_vertical_web_url = serializers.SerializerMethodField()
|
|
# highlight_vertical_web_url = serializers.SerializerMethodField()
|
|
# editor_web_url = serializers.SerializerMethodField()
|
|
# editor_mobile_url = serializers.SerializerMethodField()
|
|
#
|
|
# def get_preview_url(self, obj):
|
|
# """Get crop preview."""
|
|
# return obj.instance.get_image_url('news_preview')
|
|
#
|
|
# def get_promo_horizontal_web_url(self, obj):
|
|
# """Get crop promo_horizontal_web."""
|
|
# return obj.instance.get_image_url('news_promo_horizontal_web')
|
|
#
|
|
# def get_promo_horizontal_mobile_url(self, obj):
|
|
# """Get crop promo_horizontal_mobile."""
|
|
# return obj.instance.get_image_url('news_promo_horizontal_mobile')
|
|
#
|
|
# def get_tile_horizontal_web_url(self, obj):
|
|
# """Get crop tile_horizontal_web."""
|
|
# return obj.instance.get_image_url('news_tile_horizontal_web')
|
|
#
|
|
# def get_tile_horizontal_mobile_url(self, obj):
|
|
# """Get crop tile_horizontal_mobile."""
|
|
# return obj.instance.get_image_url('news_tile_horizontal_mobile')
|
|
#
|
|
# def get_tile_vertical_web_url(self, obj):
|
|
# """Get crop tile_vertical_web."""
|
|
# return obj.instance.get_image_url('news_tile_vertical_web')
|
|
#
|
|
# def get_highlight_vertical_web_url(self, obj):
|
|
# """Get crop highlight_vertical_web."""
|
|
# return obj.instance.get_image_url('news_highlight_vertical_web')
|
|
#
|
|
# def get_editor_web_url(self, obj):
|
|
# """Get crop editor_web."""
|
|
# return obj.instance.get_image_url('news_editor_web')
|
|
#
|
|
# def get_editor_mobile_url(self, obj):
|
|
# """Get crop editor_mobile."""
|
|
# return obj.instance.get_image_url('news_editor_mobile')
|
|
|
|
|
|
class ProductImageSerializer(serializers.ModelSerializer):
|
|
"""Serializer for returning crop images of product image."""
|
|
|
|
orientation_display = serializers.CharField(source='get_orientation_display',
|
|
read_only=True)
|
|
original_url = serializers.URLField(source='image.url')
|
|
# auto_crop_images = CropImageSerializer(source='image', allow_null=True)
|
|
|
|
class Meta:
|
|
model = Image
|
|
fields = [
|
|
'id',
|
|
'title',
|
|
'orientation_display',
|
|
'original_url',
|
|
# 'auto_crop_images',
|
|
]
|
|
extra_kwargs = {
|
|
'orientation': {'write_only': True}
|
|
}
|
|
|
|
|
|
class ProductCommentCreateSerializer(CommentSerializer):
|
|
"""Create comment serializer"""
|
|
mark = serializers.IntegerField()
|
|
|
|
class Meta:
|
|
"""Serializer for model Comment"""
|
|
model = Comment
|
|
fields = [
|
|
'id',
|
|
'created',
|
|
'text',
|
|
'mark',
|
|
'nickname',
|
|
'profile_pic',
|
|
]
|
|
|
|
def validate(self, attrs):
|
|
"""Override validate method"""
|
|
# Check product object
|
|
product_slug = self.context.get('request').parser_context.get('kwargs').get('slug')
|
|
product_qs = models.Product.objects.filter(slug=product_slug)
|
|
if not product_qs.exists():
|
|
raise serializers.ValidationError({'detail': _('Product not found.')})
|
|
attrs['product'] = product_qs.first()
|
|
return attrs
|
|
|
|
def create(self, validated_data, *args, **kwargs):
|
|
"""Override create method"""
|
|
validated_data.update({
|
|
'user': self.context.get('request').user,
|
|
'content_object': validated_data.pop('product')
|
|
})
|
|
return super().create(validated_data) |