Skip to content

Commit

Permalink
feat: add comment model and form
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasFASouza committed Feb 2, 2023
1 parent 916dab9 commit f1f4a74
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 9 deletions.
3 changes: 2 additions & 1 deletion auctions/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.contrib import admin

from .models import User, Listing, Biding
from .models import User, Listing, Biding, Comment

admin.site.register(User)
admin.site.register(Listing)
admin.site.register(Biding)
admin.site.register(Comment)
9 changes: 8 additions & 1 deletion auctions/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import forms
from .models import Listing, Biding
from .models import Listing, Biding, Comment


class NewListing(forms.ModelForm):
Expand Down Expand Up @@ -28,3 +28,10 @@ def clean(self):
class Meta:
model = Biding
fields = ['value']


class NewComment(forms.ModelForm):
class Meta:
model = Comment
fields = ['text']

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by Django 4.1.5 on 2023-02-02 17:50

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


class Migration(migrations.Migration):

dependencies = [
('auctions', '0005_rename_isactive_listing_active'),
]

operations = [
migrations.AlterField(
model_name='biding',
name='value',
field=models.DecimalField(decimal_places=2, max_digits=8),
),
migrations.AlterField(
model_name='listing',
name='initial_bid',
field=models.DecimalField(decimal_places=2, default=0.0, max_digits=8),
),
migrations.AlterField(
model_name='listing',
name='price',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=8),
),
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.CharField(max_length=256)),
('auction', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='auctions.listing')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to=settings.AUTH_USER_MODEL)),
],
),
]
9 changes: 9 additions & 0 deletions auctions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ class Biding(models.Model):

def __str__(self):
return f"{self.buyer} bids ${self.value} in {self.item.title}"


class Comment(models.Model):
text = models.TextField()
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="comments")
auction = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name="comments")

def __str__(self):
return f"{self.text} - by: {self.user}"
13 changes: 12 additions & 1 deletion auctions/templates/auctions/listing.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,22 @@ <h2>{{ listing.title }}</h2>
<h2>Place bid</h2>
<form action="{% url 'bid' listing.id %}" method="post">
{% csrf_token %}
{{ form }}
{{ bid_form }}
<input type="submit">
</form>
{% endif %}

<h2>Comments</h2>
<form action="{% url 'comment' listing.id %}" method="post">
{% csrf_token %}
{{ comment_form }}
<input type="submit">
</form>
<ul>
{% for comment in comments %}
<li>{{ comment }}</li>
{% endfor %}
</ul>

<h2>Bidings</h2>
<ul>
Expand Down
3 changes: 2 additions & 1 deletion auctions/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
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>/close", views.close_auction, name="close")
path("<int:listing_id>/close", views.close_auction, name="close"),
path("<int:listing_id>/comment", views.add_comment, name="comment"),
]
35 changes: 30 additions & 5 deletions auctions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.contrib.auth.decorators import login_required


from .models import User, Listing
from .models import User, Listing, Comment
from . import forms


Expand Down Expand Up @@ -71,12 +71,20 @@ def register(request):
def listing(request, listing_id):
auction = Listing.objects.get(id=listing_id)
bidings = auction.bidings.all()
comments = auction.comments.all()

if not bidings:
last_bid = ''
else:
last_bid = bidings.latest('value')

return render(request, "auctions/listing.html", {
"listing": auction,
"bidings": bidings,
"last_bid": bidings.latest('value'),
"form": forms.NewBid(item=auction, buyer=request.user),
"last_bid": last_bid,
"bid_form": forms.NewBid(item=auction, buyer=request.user),
"comment_form": forms.NewComment(),
"comments": comments,
})


Expand Down Expand Up @@ -105,6 +113,7 @@ def place_bid(request, listing_id):
buyer = request.user
bidings = item.bidings.all()
form = forms.NewBid(request.POST, item=item, buyer=buyer)
comments = item.comments.all()

if form.is_valid():
obj = form.save(commit=False)
Expand All @@ -123,14 +132,30 @@ def place_bid(request, listing_id):
"listing": item,
"bidings": bidings,
"last_bid": bidings.latest('value'),
"form": form
"bid_form": form,
"comment_form": forms.NewComment(),
"comments": comments,
})


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

return HttpResponseRedirect(reverse("index"))


@login_required()
def add_comment(request, listing_id):
if request.method == "POST":
auction = Listing.objects.get(id=listing_id)
form = forms.NewComment(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.user = request.user
obj.auction = auction
obj.save()

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

0 comments on commit f1f4a74

Please sign in to comment.