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
+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