Skip to content

Commit

Permalink
Update models.py
Browse files Browse the repository at this point in the history
  • Loading branch information
minghansun1 authored Oct 22, 2024
1 parent 124f751 commit feee513
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions backend/market/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,74 @@

class Offer(models.Model):
class Meta:
constraints = [models.UniqueConstraint(fields=["user", "sublet"], name="unique_offer")]
constraints = [models.UniqueConstraint(fields=["user", "item"], name="unique_offer")]

user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="offers_made")
sublet = models.ForeignKey("Sublet", on_delete=models.CASCADE, related_name="offers")
item = models.ForeignKey("Item", on_delete=models.CASCADE, related_name="offers")
email = models.EmailField(max_length=255, null=True, blank=True)
phone_number = PhoneNumberField(null=True, blank=True)
message = models.CharField(max_length=255, blank=True)
created_date = models.DateTimeField(auto_now_add=True)

def __str__(self):
return f"Offer for {self.sublet} made by {self.user}"
return f"Offer for {self.item} made by {self.user}"


class Amenity(models.Model):
class Category(models.TextChoices):
SUBLET = "sublet"
APPLIANCE = "appliance"
COOKWARE = "cookware"
FOOD = "food"
BOOK = "book"
ELECTRONICS = "electronics"
FURNITURE = "furniture"
CLOTHING = "clothing"
TRANSPORTATION = "transportation"
ROOMDECOR = "roomdecor"
SPORTS = "sports"
TICKETS = "tickets"
GIFTCARD = "giftcard"
OTHER = "other"


class Tag(models.Model):
name = models.CharField(max_length=255, primary_key=True)

def __str__(self):
return self.name


class Sublet(models.Model):
subletter = models.ForeignKey(User, on_delete=models.CASCADE)
sublettees = models.ManyToManyField(
User, through=Offer, related_name="sublets_offered", blank=True
class Item(models.Model):
seller = models.ForeignKey(User, on_delete=models.CASCADE)
tags = models.ManyToManyField(Tag, related_name="items", blank=True)
category = models.CharField(
max_length=50,
choices=Category.choices,
default=Category.OTHER,
)
favorites = models.ManyToManyField(User, related_name="sublets_favorited", blank=True)
amenities = models.ManyToManyField(Amenity, blank=True)
favorites = models.ManyToManyField(User, related_name="items_favorited", blank=True)

title = models.CharField(max_length=255)
address = models.CharField(max_length=255, null=True, blank=True)
beds = models.IntegerField(null=True, blank=True)
baths = models.DecimalField(max_digits=3, decimal_places=1, null=True, blank=True)
description = models.TextField(null=True, blank=True)
external_link = models.URLField(max_length=255, null=True, blank=True)
price = models.IntegerField()
negotiable = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
expires_at = models.DateTimeField()
start_date = models.DateField()
end_date = models.DateField()

def __str__(self):
return f"{self.title} by {self.subletter}"
return f"{self.title} by {self.seller}"


class Sublet(models.Model):
item = models.OneToOneField(Item, on_delete=models.CASCADE, related_name="sublet")
address = models.CharField(max_length=255)
beds = models.IntegerField()
baths = models.IntegerField()
start_date = models.DateTimeField()
end_date = models.DateTimeField()


class SubletImage(models.Model):
sublet = models.ForeignKey(Sublet, on_delete=models.CASCADE, related_name="images")
image = models.ImageField(upload_to="sublet/images")
class ItemImage(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name="images")
image = models.ImageField(upload_to="marketplace/images")

0 comments on commit feee513

Please sign in to comment.