diff --git a/backend/chat/models.py b/backend/chat/models.py index a0e4bc4..f61143e 100644 --- a/backend/chat/models.py +++ b/backend/chat/models.py @@ -1,6 +1,6 @@ from django.db import models from account.models import User -from product.models import ProductModel +from product.models import ProductModel, DollorModel from django.conf import settings import openai from time import sleep @@ -21,8 +21,10 @@ class ProductChatModel(models.Model): def save(self, *args, **kwargs): if not self.thread: - client = openai.OpenAI(api_key=settings.OPENAI_API_KEY) - product_json = ProductChatSerializer(instance=self.product).data + client = openai.OpenAI(api_key=settings.OPENAI_API_KEY) + dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique') + dollor_price = dollor_object.price + product_json = ProductChatSerializer(instance=self.product, context={'dollor_price': dollor_price}).data try: thread = client.beta.threads.create( diff --git a/backend/product/models.py b/backend/product/models.py index 99d838a..62c02a4 100644 --- a/backend/product/models.py +++ b/backend/product/models.py @@ -97,6 +97,22 @@ class ProductModel(models.Model): def __str__(self): return self.name + def get_toman_price(self): + dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique') + dollor_price = dollor_object.price + dollar_to_dirham = 0.27 + if dollor_price is None: + raise ValidationError({"dollor_price": "The 'dollor_price' must be provided in the context for dollar pricing."}) + if self.currency == 'toman': + toman_price = self.price + elif self.currency == 'dollor': + toman_price = self.price * dollor_price + elif self.currency == 'derham': + toman_price = self.price * dollor_price * dollar_to_dirham + return toman_price + + def get_toman_price_after_discount(self): + return self.get_toman_price() * ((100 - self.discount) / 100) def save(self, *args, **kwargs): if not self.slug: diff --git a/backend/product/serializers.py b/backend/product/serializers.py index 982506e..a5b4b1c 100644 --- a/backend/product/serializers.py +++ b/backend/product/serializers.py @@ -10,8 +10,7 @@ class ProductChatSerializer(serializers.ModelSerializer): model = ProductModel fields = ['name', 'description', 'price', 'in_stock', 'discount', ] def get_price(self, obj): - dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique') - dollor_price = dollor_object.price + dollor_price = self.content.get('dollor_price') dollar_to_dirham = 0.27 if dollor_price is None: raise ValidationError({"dollor_price": "The 'dollor_price' must be provided in the context for dollar pricing."}) diff --git a/backend/product/views.py b/backend/product/views.py index f9099e1..b10983a 100644 --- a/backend/product/views.py +++ b/backend/product/views.py @@ -54,7 +54,9 @@ class ProductView(APIView): authentication_classes = [] def get(self, request, pk): product = get_object_or_404(ProductModel, id=pk) - product_ser = self.serializer_class(instance=product, many=False) + 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}) return Response(product_ser.data, status=status.HTTP_200_OK) @@ -182,7 +184,9 @@ class AllProductsView(APIView): # Pagination paginator = self.pagination_class() paginated_products = paginator.paginate_queryset(products, request) - serializer = self.serializer_class(paginated_products, many=True) + 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}) return paginator.get_paginated_response(serializer.data) except MainCategoryModel.DoesNotExist: