optimze diffrend currencys
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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."})
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user