from django.contrib.auth.models import AbstractUser from django.db import models from django.utils.translation import gettext_lazy as _ class User(AbstractUser): watchlist = models.ManyToManyField('Listing', related_name="user_wishlist", blank=True) def __str__(self): return f"{self.username}" class Listing(models.Model): class Categories(models.TextChoices): APPLI = "Appliances", _("Appliances") CLOTH = "Clothes", _("Clothes") ELECT = "Electronics", _("Electronics") FURNI = "Furniture", _("Furniture") OTHER = "Other", _("Other") OUTDO = "Outdoors", _("Outdoors") SHOES = "Shoes", _("Shoes") TOYS = "Toys", _("Toys") user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_listing') title = models.CharField(max_length=100, editable=True) description = models.TextField(editable=True) start_bid = models.DecimalField(max_digits=6, decimal_places=2, editable=True) image = models.URLField() category = models.CharField(max_length=20, choices=Categories.choices, default=Categories.OTHER) active = models.BooleanField(default=True) max_bid = models.ForeignKey('Bid', on_delete=models.PROTECT, blank=True, null=True, editable=True) def __str__(self): return f"{self.title} ({self.description}) : ${self.start_bid}" class Bid(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_bidding') listing_item = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name='auc_listing') bid = models.DecimalField(max_digits=6, decimal_places=2) def __str__(self): return f"{self.user} bid ${self.bid} on {self.listing_item}" def save(self, *args, **kwargs): check_bid = self.listing_item.max_bid if self.bid < self.listing_item.start_bid: raise ValueError("Bid is too low.") elif check_bid is not None and self.bid <= check_bid.bid: raise ValueError("Bid is too low.") super().save(*args, **kwargs) self.listing_item.max_bid = self self.listing_item.save() class Comment(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_commenting') listing_item = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name='where') comment = models.TextField(editable=True) def __str__(self): return f"{self.user}: {self.comment}"