product refactor bullshit
This commit is contained in:
+40
-40
@@ -100,14 +100,6 @@ class InPackItems(models.Model):
|
||||
class ProductModel(models.Model):
|
||||
name = models.CharField(max_length=255, verbose_name='نام')
|
||||
description = models.TextField(verbose_name='توضیحات')
|
||||
price = models.PositiveIntegerField(default=0, verbose_name='قیمت')
|
||||
min_price = models.PositiveIntegerField(verbose_name='قیمت کف', help_text='این قیمت برای کف قیمتی محصول در نظر گرفته میشود')
|
||||
currency_type = (
|
||||
('dollor', 'دلار'),
|
||||
('toman', 'تومان'),
|
||||
('derham', 'درهم')
|
||||
)
|
||||
currency = models.CharField(verbose_name='نوع ارز', max_length=20, choices=currency_type)
|
||||
image1 = models.ImageField(upload_to='product_images/', verbose_name='عکس اول')
|
||||
image2 = models.ImageField(upload_to='product_images/', blank=True, null=True, verbose_name='عکس دوم')
|
||||
image3 = models.ImageField(upload_to='product_images/', blank=True, null=True, verbose_name='عکس سوم')
|
||||
@@ -115,8 +107,6 @@ class ProductModel(models.Model):
|
||||
rating = models.PositiveIntegerField(default=0, verbose_name='امتیاز')
|
||||
show = models.BooleanField(default=False, verbose_name='نمایش در خانه')
|
||||
view = models.IntegerField(default=0, verbose_name='بازدید')
|
||||
sell = models.IntegerField(default=0, verbose_name='فروش')
|
||||
discount = models.SmallIntegerField(default=0, verbose_name='تخفیف')
|
||||
slug = models.SlugField(max_length=50, unique=True, blank=True, null=True, allow_unicode=True,
|
||||
verbose_name='نام یکتا', help_text="این فیلد را خالی بگذارید")
|
||||
meta_description = models.CharField(max_length=300, blank=True, null=True, help_text='این فیلد را حتما پر کنید', verbose_name='متا دیسکریپشن')
|
||||
@@ -127,35 +117,11 @@ class ProductModel(models.Model):
|
||||
related_products = models.ManyToManyField('self', blank=True, verbose_name='محصولات مرتبط')
|
||||
in_pack_items = models.ManyToManyField(InPackItems, blank=True, verbose_name='ایتم های داخل پک')
|
||||
|
||||
def format_discount_price(self):
|
||||
discount_price = int(self.price * (100 - self.discount) / 100)
|
||||
formatted_num = "{:,.0f}".format(discount_price)
|
||||
return formatted_num
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def get_toman_price(self, dollor_price=None):
|
||||
if not dollor_price:
|
||||
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
|
||||
toman_price = toman_price if toman_price > self.min_price else self.min_price
|
||||
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:
|
||||
self.slug = slugify(self.name, allow_unicode=True)
|
||||
@@ -209,10 +175,16 @@ class ProductDetailModel(models.Model):
|
||||
|
||||
class CommentModel(models.Model):
|
||||
product = models.ForeignKey(ProductModel, on_delete=models.CASCADE, related_name='comments', verbose_name='محصول')
|
||||
title = models.CharField(max_length=50)
|
||||
content = models.TextField(verbose_name='محتوای نظر')
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='کاربر')
|
||||
timestamp = models.DateTimeField(auto_now_add=True, verbose_name='زمان ثبت کامنت')
|
||||
show = models.BooleanField(default=True, verbose_name='نشان دادن کامنت')
|
||||
status_types = (
|
||||
('reviewed_and_confirmed', 'بررسی و تایید شده'),
|
||||
('reviewed_and_rejected', 'بررسی شده و رد شده'),
|
||||
('not_reviwed', 'بررسی نشده'),
|
||||
)
|
||||
review_status = models.CharField(default='not_reviwed', verbose_name='نشان دادن کامنت', max_length=30, choices=status_types)
|
||||
class Meta:
|
||||
verbose_name = 'نظر'
|
||||
verbose_name_plural = 'نظرات'
|
||||
@@ -228,8 +200,8 @@ class AttributeType(models.Model):
|
||||
return self.name
|
||||
|
||||
class AttributeValue(models.Model):
|
||||
attribute_type = models.ForeignKey(AttributeType, on_delete=models.CASCADE)
|
||||
value = models.CharField(verbose_name='مقدار نوع اتربیوت', max_length=100)
|
||||
attribute_type = models.ForeignKey(AttributeType, on_delete=models.CASCADE, blank=True, null=True)
|
||||
value = models.CharField(verbose_name='مقدار نوع اتربیوت', max_length=100, blank=True, null=True)
|
||||
color = models.CharField(verbose_name='رنک', max_length=7, blank=True, null=True)
|
||||
|
||||
class Meta:
|
||||
@@ -242,11 +214,39 @@ class ProductVariant(models.Model):
|
||||
product = models.ForeignKey(ProductModel, on_delete=models.CASCADE, related_name='variants', verbose_name='محصول')
|
||||
attributes = models.ManyToManyField(AttributeValue, verbose_name='ویژگیها')
|
||||
in_stock = models.PositiveIntegerField(default=0, verbose_name='تعداد موجود')
|
||||
price = models.PositiveIntegerField(null=True, blank=True, verbose_name='قیمت (اختیاری)')
|
||||
|
||||
price = models.PositiveIntegerField(default=0, verbose_name='قیمت')
|
||||
min_price = models.PositiveIntegerField(verbose_name='قیمت کف', help_text='این قیمت برای کف قیمتی محصول در نظر گرفته میشود')
|
||||
currency_type = (
|
||||
('dollor', 'دلار'),
|
||||
('toman', 'تومان'),
|
||||
('derham', 'درهم')
|
||||
)
|
||||
sell = models.IntegerField(default=0, verbose_name='فروش')
|
||||
currency = models.CharField(verbose_name='نوع ارز', max_length=20, choices=currency_type)
|
||||
discount = models.SmallIntegerField(default=0, verbose_name='تخفیف')
|
||||
class Meta:
|
||||
verbose_name = 'تنوع محصول'
|
||||
verbose_name_plural = 'تنوعهای محصول'
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.product.name} - {', '.join(str(attr) for attr in self.attributes.all())}"
|
||||
return f"{self.product.name} - {', '.join(str(attr) for attr in self.attributes.all())}"
|
||||
|
||||
def get_toman_price(self, dollor_price=None):
|
||||
if not dollor_price:
|
||||
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
|
||||
toman_price = toman_price if toman_price > self.min_price else self.min_price
|
||||
return toman_price
|
||||
|
||||
def get_toman_price_after_discount(self):
|
||||
return self.get_toman_price() * ((100 - self.discount) / 100)
|
||||
Reference in New Issue
Block a user