dynamic pricing

This commit is contained in:
Parsa Nazer
2025-01-11 19:59:10 +03:30
parent 28f15ac869
commit 385f4b1f78
4 changed files with 73 additions and 14 deletions
+5
View File
@@ -295,6 +295,11 @@ UNFOLD = {
"icon": "chat",
"link": reverse_lazy("admin:product_commentmodel_changelist"),
},
{
"title": _("قیمت دلار"),
"icon": "payments",
"link": reverse_lazy("admin:product_dollormodel_changelist"),
},
],
},
+5 -1
View File
@@ -14,4 +14,8 @@ class CategoryModelAdmin(ModelAdmin):
@admin.register(CommentModel)
class CommentAdmin(ModelAdmin):
pass
pass
@admin.register(DollorModel)
class DollorAdmin(ModelAdmin):
readonly_fields = ('price',)
+45 -9
View File
@@ -2,6 +2,8 @@ from django.db import models
from django.utils.text import slugify
from account.models import User
from django.urls import reverse
import requests
class CategoryModel(models.Model):
name = models.CharField(max_length=50, verbose_name='نام دسته‌بندی')
@@ -53,10 +55,52 @@ class CategoryModel(models.Model):
category = category.parent
return breadcrumb[::-1]
class DollorModel(models.Model):
price = models.FloatField(null=True, blank=True)
defualt_price = models.FloatField(null=True, blank=True, default=80000.0)
# these fields will avoid dublicate of this model
unique = (('unique', 'unique'),)
unique_filed = models.CharField(max_length=20, choices=unique, unique=True, default='unique')
def __str__(self):
return str(self.price)
def save(self, *args, **kwargs):
if not self.price:
self.update_price()
super().save(*args, **kwargs)
def update_price(self):
self.price = self.get_usd_price()
def get_usd_price(self):
try:
api_usd = "https://api.nobitex.ir/v2/orderbook/USDTIRT"
response = requests.get(api_usd)
data = response.json()
price = int(data["lastTradePrice"])
price_in_usd = price / 10.0
print('\n\nprice from api \n\n')
except:
if self.price:
print('\n\nprice from last price \n\n')
return self.price
else:
print('\n\nprice from defualt price \n\n')
return self.defualt_price
return price_in_usd
class ProductModel(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
price = models.PositiveIntegerField(default=0)
price = models.PositiveIntegerField(default=0, help_text='قیمت')
currency_type = (
('dollor', 'دلار'),
('toman', 'تومان'),
('derham', 'درهم')
)
currency = models.CharField(verbose_name='نوع ارز', max_length=20, choices=currency_type)
image = models.ImageField(upload_to='product_images/')
rating = models.PositiveIntegerField(default=0)
view = models.IntegerField(default=0, verbose_name='بازدید')
@@ -75,14 +119,6 @@ class ProductModel(models.Model):
formatted_num = "{:,.0f}".format(discount_price)
return formatted_num
def discount_price(self):
discount_price = int(self.price * (100 - self.discount) / 100)
return discount_price
def format_price(self):
price = self.price
formatted_num = "{:,.0f}".format(price)
return formatted_num
def __str__(self):
return self.name
+18 -4
View File
@@ -1,14 +1,28 @@
from .models import *
from rest_framework import serializers
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = ProductModel
fields = "__all__"
class ProductChatSerializer(serializers.ModelSerializer):
price = serializers.SerializerMethodField()
class Meta:
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
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 obj.currency == 'toman':
return obj.price
elif obj.currency == 'dollor':
return obj.price * dollor_price
elif obj.currency == 'derham':
return obj.price * dollor_price * dollar_to_dirham
class ProductSerializer(ProductChatSerializer):
class Meta:
model = ProductModel
fields = "__all__"
class CommentSerializer(serializers.ModelSerializer):
class Meta: