Skip to content

Commit

Permalink
feat: add watchlist
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasFASouza committed Feb 3, 2023
1 parent bacad3b commit 7e88ea0
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 3 deletions.
19 changes: 19 additions & 0 deletions auctions/migrations/0008_listing_favorite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.1.5 on 2023-02-03 19:29

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


class Migration(migrations.Migration):

dependencies = [
('auctions', '0007_alter_comment_text_alter_listing_description'),
]

operations = [
migrations.AddField(
model_name='listing',
name='favorite',
field=models.ManyToManyField(blank=True, related_name='user_favorite', to=settings.AUTH_USER_MODEL),
),
]
2 changes: 2 additions & 0 deletions auctions/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.conf import settings


class User(AbstractUser):
Expand All @@ -23,6 +24,7 @@ class Listing(models.Model):
seller = models.ForeignKey(User, on_delete=models.CASCADE, related_name="listings")
price = models.DecimalField(max_digits=8, decimal_places=2, blank=True)
active = models.BooleanField(default=True)
favorite = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="user_favorite", blank=True)

def __str__(self):
return f"{self.title}: {self.description}"
Expand Down
5 changes: 5 additions & 0 deletions auctions/templates/auctions/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ <h6>Categories</h6>
<li class="nav-item">
<a class="nav-link" href="{% url 'categories' 'Other' %}">Other</a>
</li>
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="{% url 'watchlist' %}">Watchlist</a>
</li>
{% endif %}
</ul>
<hr>
{% block body %}
Expand Down
12 changes: 9 additions & 3 deletions auctions/templates/auctions/listing.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

{% block body %}
{% 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 %}
{% if listing.active %}
{% if listing.seller == user %}
<a href="{% url 'close' listing.id %}">Close auction</a>
{% elif is_favorite %}
<a href="{% url 'favorite' listing.id %}">Remove from watchlist</a>
{% else %}
<a href="{% url 'favorite' listing.id %}">Add to watchlist</a>
{% endif %}
{% elif bidings and last_bid.buyer == user %}
<h5>You won this auction!</h5>
{% endif %}
{% endif %}
Expand Down
17 changes: 17 additions & 0 deletions auctions/templates/auctions/watchlist.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "auctions/layout.html" %}

{% block body %}
<h2>Watchlist</h2>

<ul>
{% for listing in watchlist %}
<li>
<img src="{{ listing.photo_url }}" width="128">
<div>
<a href="{% url 'listing' listing.id %}">{{ listing.title }}:</a>
{{ listing.description }}, by {{ listing.seller }} - $ {{ listing.price }}
</div>
</li>
{% endfor %}
</ul>
{% endblock %}
2 changes: 2 additions & 0 deletions auctions/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
path("add", views.add_listing, name="add"),
path("<int:listing_id>", views.listing, name="listing"),
path("<int:listing_id>/close", views.close_auction, name="close"),
path("<int:listing_id>/favorite", views.favorite, name="favorite"),
path("categories/<str:category>", views.categories, name="categories"),
path("watchlist", views.watchlist, name="watchlist"),
]
23 changes: 23 additions & 0 deletions auctions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def listing(request, listing_id):
bidings = auction.bidings.all()
comments = auction.comments.all()
user = request.user
is_favorite = user in auction.favorite.all()

bid_form = forms.NewBid(item=auction, buyer=user)
comment_form = forms.NewComment()
Expand Down Expand Up @@ -116,6 +117,7 @@ def listing(request, listing_id):
"bid_form": bid_form,
"comment_form": comment_form,
"comments": comments,
"is_favorite": is_favorite,
})


Expand Down Expand Up @@ -154,3 +156,24 @@ def categories(request, category):
"category": category,
"listings": Listing.objects.all()
})


def favorite(request, listing_id):
user = request.user

if user.is_authenticated:
auction = Listing.objects.get(id=listing_id)

if user in auction.favorite.all():
auction.favorite.remove(user)
else:
auction.favorite.add(user)

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


def watchlist(request):
return render(request, "auctions/watchlist.html", {
"watchlist": request.user.user_favorite.all()
})

0 comments on commit 7e88ea0

Please sign in to comment.