You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.3 KiB
66 lines
2.3 KiB
from django.contrib.auth.models import User
|
|
from django.db import models
|
|
from django.conf import settings
|
|
|
|
class ColorChoices(models.TextChoices):
|
|
RED = "Red", "Red"
|
|
BLUE = "Blue", "Blue"
|
|
GREEN = "Green", "Green"
|
|
BLACK = "Black", "Black"
|
|
WHITE = "White", "White"
|
|
|
|
class SizeChoices(models.TextChoices):
|
|
SMALL = "S", "Small"
|
|
MEDIUM = "M", "Medium"
|
|
LARGE = "L", "Large"
|
|
XLARGE = "XL", "X-Large"
|
|
SINGLE = "SINGLE", "Unique"
|
|
|
|
class CutChoices(models.IntegerChoices):
|
|
WOMEN = 1, 'Women'
|
|
MEN = 2, 'Men'
|
|
KIDS = 3, 'Kids'
|
|
|
|
class Color(models.Model):
|
|
name = models.CharField(max_length=20, choices=ColorChoices.choices, unique=True)
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
class Size(models.Model):
|
|
name = models.CharField(max_length=20, choices=SizeChoices.choices, unique=True)
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
class Product(models.Model):
|
|
title = models.CharField(max_length=200)
|
|
image = models.ImageField(upload_to="products/", null=True, blank=True)
|
|
price = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
|
|
|
|
# Use string references to prevent circular imports
|
|
colors = models.ManyToManyField("shop.Color", blank=True, related_name="products")
|
|
sizes = models.ManyToManyField("shop.Size", blank=True, related_name="products")
|
|
order = models.IntegerField(default=0, blank=False)
|
|
cut = models.IntegerField(choices=CutChoices.choices, default=CutChoices.MEN)
|
|
|
|
class Meta:
|
|
ordering = ['order', 'cut'] # Add this line to sort by title
|
|
|
|
def __str__(self):
|
|
return self.title
|
|
|
|
class CartItem(models.Model):
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True)
|
|
product = models.ForeignKey(Product, on_delete=models.CASCADE)
|
|
quantity = models.PositiveIntegerField(default=1)
|
|
color = models.ForeignKey(Color, on_delete=models.SET_NULL, null=True, blank=True)
|
|
size = models.ForeignKey(Size, on_delete=models.SET_NULL, null=True, blank=True)
|
|
session_id = models.CharField(max_length=255, null=True, blank=True)
|
|
date_added = models.DateTimeField(auto_now_add=True)
|
|
|
|
def __str__(self):
|
|
return f"{self.quantity} x {self.product.title}"
|
|
|
|
def get_total_price(self):
|
|
return self.product.price * self.quantity
|
|
|