dynamic pricing
This commit is contained in:
@@ -295,6 +295,11 @@ UNFOLD = {
|
|||||||
"icon": "chat",
|
"icon": "chat",
|
||||||
"link": reverse_lazy("admin:product_commentmodel_changelist"),
|
"link": reverse_lazy("admin:product_commentmodel_changelist"),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"title": _("قیمت دلار"),
|
||||||
|
"icon": "payments",
|
||||||
|
"link": reverse_lazy("admin:product_dollormodel_changelist"),
|
||||||
|
},
|
||||||
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -15,3 +15,7 @@ class CategoryModelAdmin(ModelAdmin):
|
|||||||
@admin.register(CommentModel)
|
@admin.register(CommentModel)
|
||||||
class CommentAdmin(ModelAdmin):
|
class CommentAdmin(ModelAdmin):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@admin.register(DollorModel)
|
||||||
|
class DollorAdmin(ModelAdmin):
|
||||||
|
readonly_fields = ('price',)
|
||||||
@@ -2,6 +2,8 @@ from django.db import models
|
|||||||
from django.utils.text import slugify
|
from django.utils.text import slugify
|
||||||
from account.models import User
|
from account.models import User
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
class CategoryModel(models.Model):
|
class CategoryModel(models.Model):
|
||||||
name = models.CharField(max_length=50, verbose_name='نام دستهبندی')
|
name = models.CharField(max_length=50, verbose_name='نام دستهبندی')
|
||||||
@@ -53,10 +55,52 @@ class CategoryModel(models.Model):
|
|||||||
category = category.parent
|
category = category.parent
|
||||||
return breadcrumb[::-1]
|
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):
|
class ProductModel(models.Model):
|
||||||
name = models.CharField(max_length=255)
|
name = models.CharField(max_length=255)
|
||||||
description = models.TextField()
|
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/')
|
image = models.ImageField(upload_to='product_images/')
|
||||||
rating = models.PositiveIntegerField(default=0)
|
rating = models.PositiveIntegerField(default=0)
|
||||||
view = models.IntegerField(default=0, verbose_name='بازدید')
|
view = models.IntegerField(default=0, verbose_name='بازدید')
|
||||||
@@ -75,14 +119,6 @@ class ProductModel(models.Model):
|
|||||||
formatted_num = "{:,.0f}".format(discount_price)
|
formatted_num = "{:,.0f}".format(discount_price)
|
||||||
return formatted_num
|
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):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|||||||
@@ -1,14 +1,28 @@
|
|||||||
from .models import *
|
from .models import *
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
class ProductSerializer(serializers.ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = ProductModel
|
|
||||||
fields = "__all__"
|
|
||||||
class ProductChatSerializer(serializers.ModelSerializer):
|
class ProductChatSerializer(serializers.ModelSerializer):
|
||||||
|
price = serializers.SerializerMethodField()
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ProductModel
|
model = ProductModel
|
||||||
fields = ['name', 'description', 'price', 'in_stock', 'discount', ]
|
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 CommentSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|||||||
Reference in New Issue
Block a user