optimze diffrend currencys

This commit is contained in:
Parsa Nazer
2025-01-26 03:08:10 +03:30
parent 702ff16367
commit c65540f560
4 changed files with 28 additions and 7 deletions
+5 -3
View File
@@ -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(
+16
View File
@@ -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:
+1 -2
View File
@@ -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."})
+6 -2
View File
@@ -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: