Skip to content

Commit

Permalink
feat: shows to the user if they won the closed auction
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasFASouza committed Feb 2, 2023
1 parent f071514 commit 916dab9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 16 deletions.
18 changes: 18 additions & 0 deletions auctions/migrations/0005_rename_isactive_listing_active.py
Original file line number Diff line number Diff line change
@@ -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',
),
]
8 changes: 4 additions & 4 deletions auctions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion auctions/templates/auctions/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h2>Active Listings</h2>

<ul>
{% for listing in listings %}
{% if listing.isActive %}
{% if listing.active %}
<li>
<img src="{{ listing.photo_url }}" width="128">
<div>
Expand Down
22 changes: 14 additions & 8 deletions auctions/templates/auctions/listing.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{% extends "auctions/layout.html" %}

{% block body %}
{% if user.is_authenticated and listing.seller == user %}
<a>Close auction</a>
{% if user.is_authenticated %}
{% if listing.active and listing.seller == user %}
<a href="{% url 'close' listing.id %}">Close auction</a>
{% elif not listing.active and bidings and last_bid.buyer == user %}
<h5>You won this auction!</h5>
{% endif %}
{% endif %}

<h2>{{ listing.title }}</h2>
Expand All @@ -18,12 +22,14 @@ <h2>{{ listing.title }}</h2>
<li>Price: {{ listing.price }}</li>
</ul>

<h2>Place bid</h2>
<form action="{% url 'bid' listing.id %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit">
</form>
{% if listing.active %}
<h2>Place bid</h2>
<form action="{% url 'bid' listing.id %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit">
</form>
{% endif %}


<h2>Bidings</h2>
Expand Down
3 changes: 2 additions & 1 deletion auctions/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
path("register", views.register, name="register"),
path("add", views.add_listing, name="add"),
path("<int:listing_id>", views.listing, name="listing"),
path("<int:listing_id>/bid", views.place_bid, name="bid")
path("<int:listing_id>/bid", views.place_bid, name="bid"),
path("<int:listing_id>/close", views.close_auction, name="close")
]
17 changes: 15 additions & 2 deletions auctions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})

Expand Down Expand Up @@ -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():
Expand All @@ -111,13 +113,24 @@ 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}))

else:
return render(request, "auctions/listing.html", {
"listing": item,
"bidings": item.bidings.all(),
"bidings": bidings,
"last_bid": bidings.latest('value'),
"form": form
})
})


@login_required
def close_auction(request, listing_id):
auction = Listing.objects.get(id=listing_id)
auction.active = False
auction.save()

return HttpResponseRedirect(reverse("index"))

0 comments on commit 916dab9

Please sign in to comment.