96 lines
3.2 KiB
Python
96 lines
3.2 KiB
Python
"""Product app serializers."""
|
|
from rest_framework import serializers
|
|
from utils.serializers import TranslatedField, FavoritesCreateSerializer
|
|
from product.models import Product, ProductSubType, ProductType
|
|
from utils import exceptions as utils_exceptions
|
|
from django.utils.translation import gettext_lazy as _
|
|
from location.serializers import (WineRegionBaseSerializer, WineAppellationBaseSerializer,
|
|
CountrySimpleSerializer)
|
|
|
|
|
|
class ProductSubTypeBaseSerializer(serializers.ModelSerializer):
|
|
"""ProductSubType base serializer"""
|
|
name_translated = TranslatedField()
|
|
index_name_display = serializers.CharField(source='get_index_name_display')
|
|
|
|
class Meta:
|
|
model = 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 = ProductType
|
|
fields = [
|
|
'id',
|
|
'name_translated',
|
|
'index_name_display',
|
|
]
|
|
|
|
|
|
class ProductBaseSerializer(serializers.ModelSerializer):
|
|
"""Product base serializer."""
|
|
name_translated = TranslatedField()
|
|
description_translated = TranslatedField()
|
|
category_display = serializers.CharField(source='get_category_display')
|
|
product_type = ProductTypeBaseSerializer()
|
|
subtypes = ProductSubTypeBaseSerializer(many=True)
|
|
wine_region = WineRegionBaseSerializer(allow_null=True)
|
|
wine_appellation = WineAppellationBaseSerializer(allow_null=True)
|
|
available_countries = CountrySimpleSerializer(source='country', many=True)
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
model = Product
|
|
fields = [
|
|
'id',
|
|
'slug',
|
|
'name_translated',
|
|
'category_display',
|
|
'description_translated',
|
|
'available',
|
|
'product_type',
|
|
'subtypes',
|
|
'public_mark',
|
|
'wine_region',
|
|
'wine_appellation',
|
|
'available_countries',
|
|
]
|
|
|
|
|
|
class ProductFavoritesCreateSerializer(FavoritesCreateSerializer):
|
|
"""Serializer to create favorite object w/ model Product."""
|
|
|
|
def validate(self, attrs):
|
|
"""Overridden validate method"""
|
|
# Check establishment object
|
|
product_qs = Product.objects.filter(slug=self.slug)
|
|
|
|
# Check establishment 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) |