Skip to content

Commit

Permalink
feat: form to place bids
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasFASouza committed Feb 1, 2023
1 parent 7f903d2 commit 3f6417d
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 111 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ Design an eBay-like e-commerce auction site that will allow users to post auctio
## Specification
Complete the implementation of your auction site. You must fulfill the following requirements:

- Models: Your application should have at least three models in addition to the User model: one for auction listings, one for bids, and one for comments made on auction listings. It’s up to you to decide what fields each model should have, and what the types of those fields should be. You may have additional models if you would like.
- Create Listing: Users should be able to visit a page to create a new listing. They should be able to specify a title for the listing, a text-based description, and what the starting bid should be. Users should also optionally be able to provide a URL for an image for the listing and/or a category (e.g. Fashion, Toys, Electronics, Home, etc.).
- Active Listings Page: The default route of your web application should let users view all of the currently active auction listings. For each active listing, this page should display (at minimum) the title, description, current price, and photo (if one exists for the listing).
- Listing Page: Clicking on a listing should take users to a page specific to that listing. On that page, users should be able to view all details about the listing, including the current price for the listing.
- If the user is signed in, the user should be able to add the item to their “Watchlist.” If the item is already on the watchlist, the user should be able to remove it.
- If the user is signed in, the user should be able to bid on the item. The bid must be at least as large as the starting bid, and must be greater than any other bids that have been placed (if any). If the bid doesn’t meet those criteria, the user should be presented with an error.
- If the user is signed in and is the one who created the listing, the user should have the ability to “close” the auction from this page, which makes the highest bidder the winner of the auction and makes the listing no longer active.
- If a user is signed in on a closed listing page, and the user has won that auction, the page should say so.
- Users who are signed in should be able to add comments to the listing page. The listing page should display all comments that have been made on the listing.
- Watchlist: Users who are signed in should be able to visit a Watchlist page, which should display all of the listings that a user has added to their watchlist. Clicking on any of those listings should take the user to that listing’s page.
- Categories: Users should be able to visit a page that displays a list of all listing categories. Clicking on the name of any category should take the user to a page that displays all of the active listings in that category.
- Django Admin Interface: Via the Django admin interface, a site administrator should be able to view, add, edit, and delete any listings, comments, and bids made on the site.
- [ ] Models: Your application should have at least three models in addition to the User model: one for auction listings, one for bids, and one for comments made on auction listings. It’s up to you to decide what fields each model should have, and what the types of those fields should be. You may have additional models if you would like.
- [x] Create Listing: Users should be able to visit a page to create a new listing. They should be able to specify a title for the listing, a text-based description, and what the starting bid should be. Users should also optionally be able to provide a URL for an image for the listing and/or a category (e.g. Fashion, Toys, Electronics, Home, etc.).
- [ ] Active Listings Page: The default route of your web application should let users view all of the currently active auction listings. For each active listing, this page should display (at minimum) the title, description, current price, and photo (if one exists for the listing).
- [ ] Listing Page: Clicking on a listing should take users to a page specific to that listing. On that page, users should be able to view all details about the listing, including the current price for the listing.
- [ ] If the user is signed in, the user should be able to add the item to their “Watchlist.” If the item is already on the watchlist, the user should be able to remove it.
- [ ] If the user is signed in, the user should be able to bid on the item. The bid must be at least as large as the starting bid, and must be greater than any other bids that have been placed (if any). If the bid doesn’t meet those criteria, the user should be presented with an error.
- [ ] If the user is signed in and is the one who created the listing, the user should have the ability to “close” the auction from this page, which makes the highest bidder the winner of the auction and makes the listing no longer active.
- [ ] If a user is signed in on a closed listing page, and the user has won that auction, the page should say so.
- [ ] Users who are signed in should be able to add comments to the listing page. The listing page should display all comments that have been made on the listing.
- [ ] Watchlist: Users who are signed in should be able to visit a Watchlist page, which should display all of the listings that a user has added to their watchlist. Clicking on any of those listings should take the user to that listing’s page.
- [ ] Categories: Users should be able to visit a page that displays a list of all listing categories. Clicking on the name of any category should take the user to a page that displays all of the active listings in that category.
- [ ] Django Admin Interface: Via the Django admin interface, a site administrator should be able to view, add, edit, and delete any listings, comments, and bids made on the site.
18 changes: 18 additions & 0 deletions auctions/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@ class NewListing(forms.ModelForm):
class Meta:
model = Listing
fields = ['title', 'description', 'category', 'photo_url', 'initial_bid']


class NewBid(forms.ModelForm):
def __init__(self, *args, **kwargs):
print(kwargs)
self.item = kwargs.pop('item')
super(NewBid, self).__init__(*args, **kwargs)

def clean(self):
value = self.cleaned_data['value']
if value <= self.item.price:
raise forms.ValidationError("Your bid is lower than current price")

return self.cleaned_data

class Meta:
model = Biding
fields = ['value']
17 changes: 15 additions & 2 deletions auctions/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.1.5 on 2023-01-31 19:09
# Generated by Django 4.1.5 on 2023-02-01 18:21

from django.conf import settings
import django.contrib.auth.models
Expand Down Expand Up @@ -47,9 +47,22 @@ class Migration(migrations.Migration):
name='Listing',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=64)),
('title', models.CharField(max_length=64)),
('description', models.CharField(max_length=256)),
('initial_bid', models.DecimalField(decimal_places=2, default=0.0, max_digits=6)),
('photo_url', models.URLField(blank=True)),
('category', models.CharField(blank=True, choices=[('Fashion', 'Fashion'), ('Home', 'Home'), ('Electronics', 'Electronics'), ('Toys', 'Toys'), ('Other', 'Other')], max_length=24)),
('price', models.DecimalField(blank=True, decimal_places=2, max_digits=6)),
('seller', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='listings', to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Biding',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('value', models.DecimalField(decimal_places=2, max_digits=6)),
('buyer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bidings', to=settings.AUTH_USER_MODEL)),
('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bidings', to='auctions.listing')),
],
),
]
17 changes: 17 additions & 0 deletions auctions/migrations/0002_alter_biding_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.1.5 on 2023-02-01 18:56

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('auctions', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='biding',
options={'ordering': ['value']},
),
]
24 changes: 0 additions & 24 deletions auctions/migrations/0002_biding.py

This file was deleted.

17 changes: 17 additions & 0 deletions auctions/migrations/0003_alter_biding_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.1.5 on 2023-02-01 19:14

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('auctions', '0002_alter_biding_options'),
]

operations = [
migrations.AlterModelOptions(
name='biding',
options={},
),
]
23 changes: 0 additions & 23 deletions auctions/migrations/0003_listing_photo_alter_biding_value.py

This file was deleted.

This file was deleted.

12 changes: 6 additions & 6 deletions auctions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ class User(AbstractUser):


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)
photo_url = models.URLField(blank=True)

CATEGORIES_CHOICES = [
('Fashion', 'Fashion'),
('Home', 'Home'),
('Electronics', 'Electronics'),
('Toys', 'Toys'),
('Other', 'Other'),
]
category = models.CharField(choices=CATEGORIES_CHOICES, max_length=24, blank=True)

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)
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)

def __str__(self):
return f"{self.title}: {self.description}"
Expand Down
2 changes: 0 additions & 2 deletions auctions/templates/auctions/add.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ <h2>New Listing</h2>
{{ form }}
<input type="submit">
</form>

<a href="{% url 'index' %}">All Listings</a>
{% endblock %}
2 changes: 1 addition & 1 deletion auctions/templates/auctions/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h2>Active Listings</h2>
<li>
<img src="{{ listing.photo_url }}" width="128">
<a href="{% url 'listing' listing.id %}">{{ listing.title }}:</a>
{{ listing.description }}, by {{ listing.seller }}
{{ listing.description }}, by {{ listing.seller }} - $ {{ listing.price }}
</li>
{% endfor %}
</ul>
Expand Down
13 changes: 11 additions & 2 deletions auctions/templates/auctions/listing.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ <h2>{{ listing.title }}</h2>
<li>Seller: {{ listing.seller }}</li>
{% if listing.category %}
<li>Category: {{ listing.category }}</li>
{% else %}
<li>Category: Category not listed</li>
{% endif %}
<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>


<h2>Bidings</h2>
<ul>
{% for bid in bidings %}
Expand All @@ -19,6 +30,4 @@ <h2>Bidings</h2>
<li>Minimal bid: {{ listing.initial_bid }}</li>
{% endfor %}
</ul>

<a href="{% url 'index' %}">All Listings</a>
{% endblock %}
3 changes: 2 additions & 1 deletion auctions/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register"),
path("add", views.add_listing, name="add"),
path("<int:listing_id>", views.listing, name="listing"),
path("add", views.add_listing, name="add")
path("<int:listing_id>/bid", views.place_bid, name="bid")
]
30 changes: 29 additions & 1 deletion auctions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ def register(request):
def listing(request, listing_id):
auction = Listing.objects.get(id=listing_id)
bidings = auction.bidings.all()

return render(request, "auctions/listing.html", {
"listing": auction,
"bidings": bidings
"bidings": bidings,
"form": forms.NewBid(item=auction)
})


Expand All @@ -84,6 +86,7 @@ def add_listing(request):
if form.is_valid():
obj = form.save(commit=False)
obj.seller = request.user
obj.price = obj.initial_bid
obj.save()
return HttpResponseRedirect(reverse("index"))
else:
Expand All @@ -92,3 +95,28 @@ def add_listing(request):
return render(request, "auctions/add.html", {
"form": form
})


@login_required
def place_bid(request, listing_id):
if request.method == "POST":
item = Listing.objects.get(id=listing_id)
form = forms.NewBid(request.POST, item=item)

if form.is_valid():
obj = form.save(commit=False)
obj.buyer = request.user
obj.item = item
obj.save()

item.price = obj.value
item.save()

return HttpResponseRedirect(reverse("listing", kwargs={'listing_id': listing_id}))

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

0 comments on commit 3f6417d

Please sign in to comment.