product dynanmic serializer
This commit is contained in:
@@ -4,7 +4,7 @@ from product.models import ProductModel, DollorModel
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
import openai
|
import openai
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from product.serializers import ProductChatSerializer
|
from product.serializers import DynamicProductSerializer
|
||||||
|
|
||||||
|
|
||||||
ASSISTANT_ID = 'asst_1wOnCKncEHkOfp0FjOIz4Xkp'
|
ASSISTANT_ID = 'asst_1wOnCKncEHkOfp0FjOIz4Xkp'
|
||||||
@@ -26,7 +26,7 @@ class ProductChatModel(models.Model):
|
|||||||
client = openai.OpenAI(api_key=settings.OPENAI_API_KEY)
|
client = openai.OpenAI(api_key=settings.OPENAI_API_KEY)
|
||||||
dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique')
|
dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique')
|
||||||
dollor_price = dollor_object.price
|
dollor_price = dollor_object.price
|
||||||
product_json = ProductChatSerializer(instance=self.product, context={'dollor_price': dollor_price}).data
|
product_json = DynamicProductSerializer(instance=self.product, context={'dollor_price': dollor_price, 'view_type': 'chat'}).data
|
||||||
try:
|
try:
|
||||||
|
|
||||||
thread = client.beta.threads.create(
|
thread = client.beta.threads.create(
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from rest_framework.views import APIView, Response
|
from rest_framework.views import APIView, Response
|
||||||
from product.models import ProductModel, SubCategoryModel, DollorModel
|
from product.models import ProductModel, SubCategoryModel, DollorModel
|
||||||
from product.serializers import SubCategorySerializer, ProductSerializer
|
from product.serializers import SubCategorySerializer, DynamicProductSerializer
|
||||||
from .serializers import SliderSerializer, HomeImageSerializer
|
from .serializers import SliderSerializer, HomeImageSerializer
|
||||||
from .models import SliderModel, HomeImageModel
|
from .models import SliderModel, HomeImageModel
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
@@ -21,7 +21,7 @@ class HomeView(APIView):
|
|||||||
sub_category_ser = SubCategorySerializer(instance=sub_categories, many=True, context={'request': request})
|
sub_category_ser = SubCategorySerializer(instance=sub_categories, many=True, context={'request': request})
|
||||||
|
|
||||||
products_to_show = ProductModel.objects.filter(show=True)
|
products_to_show = ProductModel.objects.filter(show=True)
|
||||||
product_ser = ProductSerializer(instance=products_to_show, many=True, context={'request': request, 'dollor_price': dollor_price})
|
product_ser = DynamicProductSerializer(instance=products_to_show, many=True, context={'request': request, 'dollor_price': dollor_price, 'view_type': 'list'})
|
||||||
|
|
||||||
home_image = HomeImageModel.objects.all().first()
|
home_image = HomeImageModel.objects.all().first()
|
||||||
home_image_ser = HomeImageSerializer(instance=home_image, context={'request': request})
|
home_image_ser = HomeImageSerializer(instance=home_image, context={'request': request})
|
||||||
|
|||||||
@@ -3,12 +3,39 @@ from rest_framework import serializers
|
|||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from datetime import timedelta
|
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()
|
price = serializers.SerializerMethodField()
|
||||||
is_new = 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:
|
class Meta:
|
||||||
model = ProductModel
|
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):
|
def get_price(self, obj):
|
||||||
dollor_price = self.context.get('dollor_price')
|
dollor_price = self.context.get('dollor_price')
|
||||||
dollar_to_dirham = 0.27
|
dollar_to_dirham = 0.27
|
||||||
@@ -21,27 +48,25 @@ class ProductChatSerializer(serializers.ModelSerializer):
|
|||||||
elif obj.currency == 'derham':
|
elif obj.currency == 'derham':
|
||||||
toman_price = obj.price * dollor_price * dollar_to_dirham
|
toman_price = obj.price * dollor_price * dollar_to_dirham
|
||||||
return "{:,.0f} تومان".format(toman_price)
|
return "{:,.0f} تومان".format(toman_price)
|
||||||
|
|
||||||
def get_is_new(self, obj):
|
def get_is_new(self, obj):
|
||||||
return timezone.now() < obj.created_at + timedelta(days=7)
|
return timezone.now() < obj.created_at + timedelta(days=7)
|
||||||
|
|
||||||
class ProductSerializer(ProductChatSerializer):
|
def get_related_products(self, obj):
|
||||||
class Meta:
|
if obj.related_products.all().count() >= 5:
|
||||||
model = ProductModel
|
related_products = obj.related_products.all()
|
||||||
fields = "__all__"
|
else:
|
||||||
# many_fields = ['name', 'price']
|
related_products = obj.category.products
|
||||||
# one_fields = ['description',]
|
serializer = DynamicProductSerializer(
|
||||||
# def __init__(self, *args, **kwargs):
|
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 CommentSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|||||||
@@ -49,19 +49,19 @@ class AllCategories(APIView):
|
|||||||
return Response(categories_ser.data, status=status.HTTP_200_OK)
|
return Response(categories_ser.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
class ProductView(APIView):
|
class ProductView(APIView):
|
||||||
serializer_class = ProductSerializer
|
serializer_class = DynamicProductSerializer
|
||||||
permission_classes = [AllowAny]
|
permission_classes = [AllowAny]
|
||||||
authentication_classes = []
|
authentication_classes = []
|
||||||
def get(self, request, pk):
|
def get(self, request, pk):
|
||||||
product = get_object_or_404(ProductModel, id=pk)
|
product = get_object_or_404(ProductModel, id=pk)
|
||||||
dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique')
|
dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique')
|
||||||
dollor_price = dollor_object.price
|
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)
|
return Response(product_ser.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
class AllProductsView(APIView):
|
class AllProductsView(APIView):
|
||||||
serializer_class = ProductSerializer
|
serializer_class = DynamicProductSerializer
|
||||||
pagination_class = StructurePagination
|
pagination_class = StructurePagination
|
||||||
authentication_classes = []
|
authentication_classes = []
|
||||||
@extend_schema(
|
@extend_schema(
|
||||||
@@ -137,7 +137,7 @@ class AllProductsView(APIView):
|
|||||||
"Provide a list of category IDs to filter products by those categories and their subcategories."
|
"Provide a list of category IDs to filter products by those categories and their subcategories."
|
||||||
),
|
),
|
||||||
responses={
|
responses={
|
||||||
200: ProductSerializer(many=True),
|
200: DynamicProductSerializer(many=True, context={'view_type': 'list'}),
|
||||||
404: OpenApiTypes.OBJECT,
|
404: OpenApiTypes.OBJECT,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@@ -191,7 +191,7 @@ class AllProductsView(APIView):
|
|||||||
paginated_products = paginator.paginate_queryset(products, request)
|
paginated_products = paginator.paginate_queryset(products, request)
|
||||||
dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique')
|
dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique')
|
||||||
dollor_price = dollor_object.price
|
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)
|
return paginator.get_paginated_response(serializer.data)
|
||||||
|
|
||||||
except MainCategoryModel.DoesNotExist:
|
except MainCategoryModel.DoesNotExist:
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
# from unfold.widgets import UnfoldAdminCheckboxSelectMultiple
|
||||||
|
# from django import forms
|
||||||
|
# class DriverAdminForm(forms.ModelForm):
|
||||||
|
# flags = forms.MultipleChoiceField(
|
||||||
|
# label=("Flags"),
|
||||||
|
# choices=[
|
||||||
|
# ("POPULAR", ("Popular")),
|
||||||
|
# ("FASTEST", ("Fastest")),
|
||||||
|
# ("TALENTED", ("Talented")),
|
||||||
|
# ],
|
||||||
|
# required=False,
|
||||||
|
# widget=UnfoldAdminCheckboxSelectMultiple,
|
||||||
|
# )
|
||||||
|
# form = DriverAdminForm
|
||||||
Reference in New Issue
Block a user