71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
"""Product app back-office serializers."""
|
|
from django.utils.translation import gettext_lazy as _
|
|
from rest_framework import serializers
|
|
|
|
from product import models
|
|
from product.serializers import ProductDetailSerializer
|
|
from gallery.models import Image
|
|
|
|
|
|
class ProductBackOfficeGallerySerializer(serializers.ModelSerializer):
|
|
"""Serializer class for model ProductGallery."""
|
|
|
|
class Meta:
|
|
"""Meta class"""
|
|
|
|
model = models.ProductGallery
|
|
fields = [
|
|
'id',
|
|
'is_main',
|
|
]
|
|
|
|
def get_request_kwargs(self):
|
|
"""Get url kwargs from request."""
|
|
return self.context.get('request').parser_context.get('kwargs')
|
|
|
|
def validate(self, attrs):
|
|
"""Override validate method."""
|
|
product_pk = self.get_request_kwargs().get('pk')
|
|
image_id = self.get_request_kwargs().get('image_id')
|
|
|
|
product_qs = models.Product.objects.filter(pk=product_pk)
|
|
image_qs = Image.objects.filter(id=image_id)
|
|
|
|
if not product_qs.exists():
|
|
raise serializers.ValidationError({'detail': _('Product not found')})
|
|
if not image_qs.exists():
|
|
raise serializers.ValidationError({'detail': _('Image not found')})
|
|
|
|
product = product_qs.first()
|
|
image = image_qs.first()
|
|
|
|
attrs['product'] = product
|
|
attrs['image'] = image
|
|
|
|
return attrs
|
|
|
|
|
|
class ProductBackOfficeDetailSerializer(ProductDetailSerializer):
|
|
|
|
class Meta(ProductDetailSerializer.Meta):
|
|
fields = ProductDetailSerializer.Meta.fields + [
|
|
'description',
|
|
'available',
|
|
'product_type',
|
|
'establishment',
|
|
'wine_region',
|
|
'wine_sub_region',
|
|
'wine_village',
|
|
'state',
|
|
]
|
|
extra_kwargs = {
|
|
'description': {'write_only': True},
|
|
'available': {'write_only': True},
|
|
'product_type': {'write_only': True},
|
|
'establishment': {'write_only': True},
|
|
'wine_region': {'write_only': True},
|
|
'wine_sub_region': {'write_only': True},
|
|
'wine_village': {'write_only': True},
|
|
'state': {'write_only': True},
|
|
}
|