diff --git a/auctions/migrations/0005_rename_isactive_listing_active.py b/auctions/migrations/0005_rename_isactive_listing_active.py new file mode 100644 index 0000000..116fb19 --- /dev/null +++ b/auctions/migrations/0005_rename_isactive_listing_active.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.5 on 2023-02-02 17:03 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('auctions', '0004_listing_isactive'), + ] + + operations = [ + migrations.RenameField( + model_name='listing', + old_name='isActive', + new_name='active', + ), + ] diff --git a/auctions/models.py b/auctions/models.py index a1ae47d..509bb4e 100644 --- a/auctions/models.py +++ b/auctions/models.py @@ -17,19 +17,19 @@ class Listing(models.Model): title = models.CharField(max_length=64) description = models.CharField(max_length=256) - initial_bid = models.DecimalField(max_digits=6, decimal_places=2, default=0.00) + initial_bid = models.DecimalField(max_digits=8, decimal_places=2, default=0.00) photo_url = models.URLField(blank=True) category = models.CharField(choices=CATEGORIES_CHOICES, max_length=24, blank=True) seller = models.ForeignKey(User, on_delete=models.CASCADE, related_name="listings") - price = models.DecimalField(max_digits=6, decimal_places=2, blank=True) - isActive = models.BooleanField(default=True) + price = models.DecimalField(max_digits=8, decimal_places=2, blank=True) + active = models.BooleanField(default=True) def __str__(self): return f"{self.title}: {self.description}" class Biding(models.Model): - value = models.DecimalField(max_digits=6, decimal_places=2) + value = models.DecimalField(max_digits=8, decimal_places=2) buyer = models.ForeignKey(User, on_delete=models.CASCADE, related_name="bidings") item = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name="bidings") diff --git a/auctions/templates/auctions/index.html b/auctions/templates/auctions/index.html index b270cb9..5196d3d 100644 --- a/auctions/templates/auctions/index.html +++ b/auctions/templates/auctions/index.html @@ -5,7 +5,7 @@

Active Listings

-

Place bid

-
- {% csrf_token %} - {{ form }} - -
+ {% if listing.active %} +

Place bid

+
+ {% csrf_token %} + {{ form }} + +
+ {% endif %}

Bidings

diff --git a/auctions/urls.py b/auctions/urls.py index 26fc778..45cea57 100644 --- a/auctions/urls.py +++ b/auctions/urls.py @@ -9,5 +9,6 @@ path("register", views.register, name="register"), path("add", views.add_listing, name="add"), path("", views.listing, name="listing"), - path("/bid", views.place_bid, name="bid") + path("/bid", views.place_bid, name="bid"), + path("/close", views.close_auction, name="close") ] diff --git a/auctions/views.py b/auctions/views.py index a4f70c3..e98015e 100644 --- a/auctions/views.py +++ b/auctions/views.py @@ -75,6 +75,7 @@ def listing(request, listing_id): return render(request, "auctions/listing.html", { "listing": auction, "bidings": bidings, + "last_bid": bidings.latest('value'), "form": forms.NewBid(item=auction, buyer=request.user), }) @@ -102,6 +103,7 @@ def place_bid(request, listing_id): if request.method == "POST": item = Listing.objects.get(id=listing_id) buyer = request.user + bidings = item.bidings.all() form = forms.NewBid(request.POST, item=item, buyer=buyer) if form.is_valid(): @@ -111,6 +113,7 @@ def place_bid(request, listing_id): obj.save() item.price = obj.value + item.buyer = buyer item.save() return HttpResponseRedirect(reverse("listing", kwargs={'listing_id': listing_id})) @@ -118,6 +121,16 @@ def place_bid(request, listing_id): else: return render(request, "auctions/listing.html", { "listing": item, - "bidings": item.bidings.all(), + "bidings": bidings, + "last_bid": bidings.latest('value'), "form": form - }) \ No newline at end of file + }) + + +@login_required +def close_auction(request, listing_id): + auction = Listing.objects.get(id=listing_id) + auction.active = False + auction.save() + + return HttpResponseRedirect(reverse("index"))