94 lines
3.0 KiB
Python
94 lines
3.0 KiB
Python
from decimal import Decimal
|
|
|
|
from django.conf import settings
|
|
from django.db import models
|
|
|
|
CATEGORY_CHOICES = [
|
|
('Electronics', 'Electronics'),
|
|
('Mobiles', 'Mobiles'),
|
|
('Fashion', 'Fashion'),
|
|
('Home', 'Home'),
|
|
('Beauty', 'Beauty'),
|
|
('Sports', 'Sports'),
|
|
('Books', 'Books'),
|
|
('Groceries', 'Groceries'),
|
|
('Pets', 'Pets'),
|
|
('Tools', 'Tools'),
|
|
('Office', 'Office'),
|
|
('Kitchen', 'Kitchen'),
|
|
('Travel', 'Travel'),
|
|
('Automotive', 'Automotive'),
|
|
('Garden', 'Garden'),
|
|
('Party', 'Party'),
|
|
('General', 'General'),
|
|
]
|
|
|
|
LISTING_TYPE_CHOICES = [
|
|
('rent', 'Rent'),
|
|
('sale', 'Sale'),
|
|
]
|
|
|
|
|
|
class Product(models.Model):
|
|
CATEGORY_CHOICES = CATEGORY_CHOICES
|
|
LISTING_TYPE_CHOICES = LISTING_TYPE_CHOICES
|
|
|
|
name = models.CharField(max_length=200)
|
|
description = models.TextField()
|
|
price = models.DecimalField(max_digits=10, decimal_places=2)
|
|
discount_price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
|
|
rental_price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
|
|
rent_duration = models.CharField(max_length=50, blank=True, default='per day')
|
|
listing_type = models.CharField(max_length=10, choices=LISTING_TYPE_CHOICES, default='rent')
|
|
image = models.ImageField(upload_to='products/')
|
|
category = models.CharField(max_length=100, choices=CATEGORY_CHOICES, default='General')
|
|
stock = models.IntegerField(default=0)
|
|
featured = models.BooleanField(default=False)
|
|
rating = models.DecimalField(max_digits=3, decimal_places=2, default=0)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
class Meta:
|
|
ordering = ['-featured', '-created_at']
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
@property
|
|
def display_price(self):
|
|
return self.discount_price if self.discount_price else self.price
|
|
|
|
@property
|
|
def is_low_stock(self):
|
|
return 0 < self.stock < 5
|
|
|
|
@property
|
|
def savings(self):
|
|
if self.discount_price and self.discount_price < self.price:
|
|
return self.price - self.discount_price
|
|
return Decimal('0')
|
|
|
|
@property
|
|
def discount_percent(self):
|
|
if self.discount_price and self.discount_price < self.price and self.price:
|
|
percent = ((self.price - self.discount_price) / self.price) * Decimal('100')
|
|
return int(percent.quantize(Decimal('1')))
|
|
return 0
|
|
|
|
@property
|
|
def available(self):
|
|
return self.stock > 0
|
|
|
|
|
|
class WishlistItem(models.Model):
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='wishlist_items')
|
|
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='wishlisted_by')
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
unique_together = ('user', 'product')
|
|
ordering = ['-created_at']
|
|
|
|
def __str__(self):
|
|
return f"{self.user} - {self.product.name}"
|