product dynanmic serializer
This commit is contained in:
@@ -3,12 +3,39 @@ from rest_framework import serializers
|
||||
from django.utils import timezone
|
||||
from datetime import timedelta
|
||||
|
||||
class ProductChatSerializer(serializers.ModelSerializer):
|
||||
|
||||
class InStuckColorsSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = InStuckColors
|
||||
fields = ['color', 'in_stuck']
|
||||
|
||||
|
||||
class DynamicProductSerializer(serializers.ModelSerializer):
|
||||
colors = InStuckColorsSerializer(many=True, read_only=True)
|
||||
price = serializers.SerializerMethodField()
|
||||
is_new = serializers.SerializerMethodField()
|
||||
related_products = serializers.SerializerMethodField()
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
view_type = self.context.get('view_type', 'all')
|
||||
if view_type != 'all':
|
||||
allowed_fields = self.Meta.view_type[view_type]
|
||||
allowed = set(allowed_fields)
|
||||
existing = set(self.fields.keys())
|
||||
|
||||
for field_name in existing - allowed:
|
||||
self.fields.pop(field_name)
|
||||
|
||||
|
||||
class Meta:
|
||||
model = ProductModel
|
||||
fields = ['name', 'description', 'price', 'in_stock', 'discount', 'is_new']
|
||||
fields = "__all__"
|
||||
view_type = {
|
||||
'list': ['name', 'price', 'image1', 'video', 'rating', 'discount', 'slug', 'category', 'colors'],
|
||||
'instance': ['name', 'description', 'price', 'image1', 'image2', 'image3', 'video', 'rating', 'discount', 'slug', 'meta_description', 'meta_keywords', 'meta_rating', 'category', 'colors', 'related_products'],
|
||||
'chat': ['name', 'description', 'price', 'in_stock', 'discount', 'colors']
|
||||
}
|
||||
|
||||
def get_price(self, obj):
|
||||
dollor_price = self.context.get('dollor_price')
|
||||
dollar_to_dirham = 0.27
|
||||
@@ -21,27 +48,25 @@ class ProductChatSerializer(serializers.ModelSerializer):
|
||||
elif obj.currency == 'derham':
|
||||
toman_price = obj.price * dollor_price * dollar_to_dirham
|
||||
return "{:,.0f} تومان".format(toman_price)
|
||||
|
||||
def get_is_new(self, obj):
|
||||
return timezone.now() < obj.created_at + timedelta(days=7)
|
||||
|
||||
class ProductSerializer(ProductChatSerializer):
|
||||
class Meta:
|
||||
model = ProductModel
|
||||
fields = "__all__"
|
||||
# many_fields = ['name', 'price']
|
||||
# one_fields = ['description',]
|
||||
# def __init__(self, *args, **kwargs):
|
||||
def get_related_products(self, obj):
|
||||
if obj.related_products.all().count() >= 5:
|
||||
related_products = obj.related_products.all()
|
||||
else:
|
||||
related_products = obj.category.products
|
||||
serializer = DynamicProductSerializer(
|
||||
related_products,
|
||||
many=True,
|
||||
context={
|
||||
'view_type': 'list',
|
||||
'dollor_price': self.context.get('dollor_price')
|
||||
}
|
||||
)
|
||||
return serializer.data
|
||||
|
||||
# many = kwargs.pop('many', True)
|
||||
# super().__init__(*args, **kwargs)
|
||||
|
||||
# allowed_fields = self.Meta.many_fields if many else self.Meta.one_fields
|
||||
|
||||
# allowed = set(allowed_fields)
|
||||
# existing = set(self.fields.keys())
|
||||
|
||||
# for field_name in existing - allowed:
|
||||
# self.fields.pop(field_name)
|
||||
|
||||
class CommentSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
|
||||
@@ -49,19 +49,19 @@ class AllCategories(APIView):
|
||||
return Response(categories_ser.data, status=status.HTTP_200_OK)
|
||||
|
||||
class ProductView(APIView):
|
||||
serializer_class = ProductSerializer
|
||||
serializer_class = DynamicProductSerializer
|
||||
permission_classes = [AllowAny]
|
||||
authentication_classes = []
|
||||
def get(self, request, pk):
|
||||
product = get_object_or_404(ProductModel, id=pk)
|
||||
dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique')
|
||||
dollor_price = dollor_object.price
|
||||
product_ser = self.serializer_class(instance=product, many=False, context={'dollor_price': dollor_price, 'request': request})
|
||||
product_ser = self.serializer_class(instance=product, many=False, context={'dollor_price': dollor_price, 'request': request, 'view_type': 'instance'})
|
||||
return Response(product_ser.data, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class AllProductsView(APIView):
|
||||
serializer_class = ProductSerializer
|
||||
serializer_class = DynamicProductSerializer
|
||||
pagination_class = StructurePagination
|
||||
authentication_classes = []
|
||||
@extend_schema(
|
||||
@@ -137,7 +137,7 @@ class AllProductsView(APIView):
|
||||
"Provide a list of category IDs to filter products by those categories and their subcategories."
|
||||
),
|
||||
responses={
|
||||
200: ProductSerializer(many=True),
|
||||
200: DynamicProductSerializer(many=True, context={'view_type': 'list'}),
|
||||
404: OpenApiTypes.OBJECT,
|
||||
},
|
||||
)
|
||||
@@ -191,7 +191,7 @@ class AllProductsView(APIView):
|
||||
paginated_products = paginator.paginate_queryset(products, request)
|
||||
dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique')
|
||||
dollor_price = dollor_object.price
|
||||
serializer = self.serializer_class(paginated_products, many=True, context={'dollor_price': dollor_price, 'request': request})
|
||||
serializer = self.serializer_class(paginated_products, many=True, context={'dollor_price': dollor_price, 'request': request, 'view_type': 'list'})
|
||||
return paginator.get_paginated_response(serializer.data)
|
||||
|
||||
except MainCategoryModel.DoesNotExist:
|
||||
|
||||
Reference in New Issue
Block a user