38386-vm/core/models.py
2026-02-12 16:53:09 +00:00

46 lines
1.7 KiB
Python

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from datetime import timedelta
class CarAuction(models.Model):
title = models.CharField(max_length=200)
make = models.CharField(max_length=100)
model = models.CharField(max_length=100)
year = models.IntegerField()
description = models.TextField()
image_url = models.URLField(max_length=500, blank=True, null=True)
starting_bid = models.DecimalField(max_digits=10, decimal_places=2)
current_bid = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
seller = models.ForeignKey(User, on_delete=models.CASCADE, related_name='auctions')
created_at = models.DateTimeField(auto_now_add=True)
end_date = models.DateTimeField()
def is_active(self):
return timezone.now() < self.end_date
def time_left(self):
if not self.is_active():
return "Closed"
delta = self.end_date - timezone.now()
days = delta.days
hours = delta.seconds // 3600
minutes = (delta.seconds // 60) % 60
if days > 0:
return f"{days}d {hours}h"
return f"{hours}h {minutes}m"
def __str__(self):
return f"{self.year} {self.make} {self.model}"
class Bid(models.Model):
auction = models.ForeignKey(CarAuction, on_delete=models.CASCADE, related_name='bids')
user = models.ForeignKey(User, on_delete=models.CASCADE)
amount = models.DecimalField(max_digits=10, decimal_places=2)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-amount']
def __str__(self):
return f"{self.user.username} - {self.amount} on {self.auction.title}"