optimze diffrend currencys
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from account.models import User
|
from account.models import User
|
||||||
from product.models import ProductModel
|
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
|
||||||
@@ -21,8 +21,10 @@ class ProductChatModel(models.Model):
|
|||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if not self.thread:
|
if not self.thread:
|
||||||
client = openai.OpenAI(api_key=settings.OPENAI_API_KEY)
|
client = openai.OpenAI(api_key=settings.OPENAI_API_KEY)
|
||||||
product_json = ProductChatSerializer(instance=self.product).data
|
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:
|
try:
|
||||||
|
|
||||||
thread = client.beta.threads.create(
|
thread = client.beta.threads.create(
|
||||||
|
|||||||
@@ -97,6 +97,22 @@ class ProductModel(models.Model):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
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):
|
def save(self, *args, **kwargs):
|
||||||
if not self.slug:
|
if not self.slug:
|
||||||
|
|||||||
@@ -10,8 +10,7 @@ class ProductChatSerializer(serializers.ModelSerializer):
|
|||||||
model = ProductModel
|
model = ProductModel
|
||||||
fields = ['name', 'description', 'price', 'in_stock', 'discount', ]
|
fields = ['name', 'description', 'price', 'in_stock', 'discount', ]
|
||||||
def get_price(self, obj):
|
def get_price(self, obj):
|
||||||
dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique')
|
dollor_price = self.content.get('dollor_price')
|
||||||
dollor_price = dollor_object.price
|
|
||||||
dollar_to_dirham = 0.27
|
dollar_to_dirham = 0.27
|
||||||
if dollor_price is None:
|
if dollor_price is None:
|
||||||
raise ValidationError({"dollor_price": "The 'dollor_price' must be provided in the context for dollar pricing."})
|
raise ValidationError({"dollor_price": "The 'dollor_price' must be provided in the context for dollar pricing."})
|
||||||
|
|||||||
@@ -54,7 +54,9 @@ class ProductView(APIView):
|
|||||||
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)
|
||||||
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)
|
return Response(product_ser.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
@@ -182,7 +184,9 @@ class AllProductsView(APIView):
|
|||||||
# Pagination
|
# Pagination
|
||||||
paginator = self.pagination_class()
|
paginator = self.pagination_class()
|
||||||
paginated_products = paginator.paginate_queryset(products, request)
|
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)
|
return paginator.get_paginated_response(serializer.data)
|
||||||
|
|
||||||
except MainCategoryModel.DoesNotExist:
|
except MainCategoryModel.DoesNotExist:
|
||||||
|
|||||||
Reference in New Issue
Block a user