Skip to content

Commit

Permalink
Fix and add missing models
Browse files Browse the repository at this point in the history
  • Loading branch information
ltan02 committed Nov 25, 2023
1 parent c35bc3c commit e02cdf0
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Generated by Django 4.2.6 on 2023-11-25 01:48

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):
dependencies = [
("contenttypes", "0002_remove_content_type_name"),
("auction", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="auction",
name="cover_image",
field=models.CharField(blank=True, max_length=500, null=True),
),
migrations.AddField(
model_name="auction",
name="is_published",
field=models.BooleanField(default=False),
),
migrations.AlterField(
model_name="auction",
name="name",
field=models.CharField(blank=True, max_length=150, null=True),
),
migrations.CreateModel(
name="AuctionItem",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
unique=True,
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("object_id", models.UUIDField()),
(
"auction_id",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to="auction.auction",
),
),
(
"content_type",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to="contenttypes.contenttype",
),
),
],
options={
"unique_together": {("auction_id", "content_type", "object_id")},
},
),
]
31 changes: 30 additions & 1 deletion backend/auction/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError

from core.models import MainModel

Expand All @@ -8,13 +11,39 @@ class Auction(MainModel):
Auction that bidders can place bid on vehicles
"""

name = models.CharField(max_length=150)
name = models.CharField(max_length=150, null=True, blank=True)
start_date = models.DateTimeField(null=False)
end_date = models.DateTimeField(null=False)
cover_image = models.CharField(max_length=500, null=True, blank=True)
is_published = models.BooleanField(default=False)

def __str__(self):
return (
self.name
if self.name is not None
else "Auction at {start_date} to {end_date}"
)


class AuctionItem(MainModel):
"""
Items that bidders can bid on in a given auction
"""

auction_id = models.ForeignKey(Auction, on_delete=models.PROTECT)
content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
object_id = models.UUIDField()
content_object = GenericForeignKey("content_type", "object_id")

class Meta:
unique_together = ('auction_id', 'content_type', 'object_id')

def clean(self):
# Restrict content_type to specific models
valid_models = ['vehicle', 'equipment', 'trailer']
if self.content_type.model not in valid_models:
raise ValidationError(f"ContentType must be one of {valid_models}")

def save(self, *args, **kwargs):
self.clean()
super().save(*args, **kwargs)
24 changes: 24 additions & 0 deletions backend/bid/migrations/0002_bid_auction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.6 on 2023-11-25 01:48

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
("auction", "0002_auction_cover_image_auction_is_published_and_more"),
("bid", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="bid",
name="auction",
field=models.ForeignKey(
default=0,
on_delete=django.db.models.deletion.PROTECT,
to="auction.auction",
),
preserve_default=False,
),
]
11 changes: 11 additions & 0 deletions backend/bid/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db import models

from core.models import MainModel
Expand All @@ -17,3 +18,13 @@ class Bid(MainModel):
content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
object_id = models.UUIDField()
content_object = GenericForeignKey("content_type", "object_id")

def clean(self):
# Restrict content_type to specific models
valid_models = ['vehicle', 'equipment', 'trailer']
if self.content_type.model not in valid_models:
raise ValidationError(f"ContentType must be one of {valid_models}")

def save(self, *args, **kwargs):
self.clean()
super().save(*args, **kwargs)
3 changes: 2 additions & 1 deletion backend/user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ def save(self, *args, **kwargs):

def generate_unique_bidder_number(self):
# Generate an 8-digit number and check if it's unique
while True:
for _ in range(99999999):
bidder_number = random.randint(10000000, 99999999)
if not Bidder.objects.filter(bidder_number=bidder_number).exists():
return bidder_number
raise Exception("Could not generate unique bidder number")

def __str__(self):
return f"Bidder: {self.first_name} {self.last_name}"
61 changes: 61 additions & 0 deletions backend/vehicle/migrations/0003_savedunits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Generated by Django 4.2.6 on 2023-11-25 01:48

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):
dependencies = [
("user", "0003_remove_admin_password"),
("contenttypes", "0002_remove_content_type_name"),
("auction", "0002_auction_cover_image_auction_is_published_and_more"),
(
"vehicle",
"0002_rename_hydraulic_cylindar_condition_equipment_hydraulic_cylinder_condition",
),
]

operations = [
migrations.CreateModel(
name="SavedUnits",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
unique=True,
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("object_id", models.UUIDField()),
(
"auction_id",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to="auction.auction",
),
),
(
"bidder_id",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT, to="user.bidder"
),
),
(
"content_type",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to="contenttypes.contenttype",
),
),
],
options={
"unique_together": {("auction_id", "bidder_id", "object_id")},
},
),
]
32 changes: 32 additions & 0 deletions backend/vehicle/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db import models

from core.models import MainModel
Expand Down Expand Up @@ -77,3 +78,34 @@ class UnitImage(MainModel):
content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
object_id = models.UUIDField()
content_object = GenericForeignKey("content_type", "object_id")

def clean(self):
# Restrict content_type to specific models
valid_models = ['vehicle', 'equipment', 'trailer']
if self.content_type.model not in valid_models:
raise ValidationError(f"ContentType must be one of {valid_models}")

def save(self, *args, **kwargs):
self.clean()
super().save(*args, **kwargs)


class SavedUnits(MainModel):
bidder_id = models.ForeignKey("user.Bidder", on_delete=models.PROTECT)
auction_id = models.ForeignKey("auction.Auction", on_delete=models.PROTECT)
content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
object_id = models.UUIDField()
content_object = GenericForeignKey("content_type", "object_id")

class Meta:
unique_together = ('auction_id', 'bidder_id', 'object_id')

def clean(self):
# Restrict content_type to specific models
valid_models = ['vehicle', 'equipment', 'trailer']
if self.content_type.model not in valid_models:
raise ValidationError(f"ContentType must be one of {valid_models}")

def save(self, *args, **kwargs):
self.clean()
super().save(*args, **kwargs)

0 comments on commit e02cdf0

Please sign in to comment.