add watchlist

This commit is contained in:
Priyatham-sai-chand 2020-10-19 19:29:55 +05:30
parent a25a71a7e2
commit 46cd6948dc
22 changed files with 259 additions and 23 deletions

View File

@ -0,0 +1,27 @@
# Generated by Django 3.0.8 on 2020-10-19 05:39
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('auctions', '0005_auto_20201018_2304'),
]
operations = [
migrations.RemoveField(
model_name='auctionlisting',
name='watchlist',
),
migrations.AddField(
model_name='user',
name='watchlist',
field=models.ManyToManyField(blank=True, related_name='watchlist', to='auctions.AuctionListing'),
),
migrations.AlterField(
model_name='auctionlisting',
name='category',
field=models.CharField(choices=[('Fashion', 'Fashion'), ('Electronics', 'Electronics'), ('Home', 'Home'), ('Sports', 'Sports'), ('Toys', 'Toys'), ('Automobile', 'Automobile'), ('Books', 'Books'), ('Video Games', 'Video Games'), ('Other', 'Other')], max_length=64),
),
]

View File

@ -0,0 +1,34 @@
# Generated by Django 3.0.8 on 2020-10-19 11:53
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('auctions', '0006_auto_20201019_1109'),
]
operations = [
migrations.AlterField(
model_name='auctionlisting',
name='picture',
field=models.URLField(default='https://www.riobeauty.co.uk/images/product_image_not_found.gif', null=True),
),
migrations.AlterField(
model_name='user',
name='watchlist',
field=models.ManyToManyField(blank=True, related_name='watchlists', to='auctions.AuctionListing'),
),
migrations.CreateModel(
name='Bids',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('bid_value', models.DecimalField(decimal_places=2, max_digits=10)),
('listing', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bidding_listing', to='auctions.AuctionListing')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bidding_user', to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.8 on 2020-10-19 11:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('auctions', '0007_auto_20201019_1723'),
]
operations = [
migrations.AlterField(
model_name='auctionlisting',
name='picture',
field=models.URLField(default='https://www.riobeauty.co.uk/images/product_image_not_found.gif'),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.8 on 2020-10-19 12:10
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('auctions', '0008_auto_20201019_1725'),
]
operations = [
migrations.AlterField(
model_name='auctionlisting',
name='category',
field=models.CharField(choices=[('Fashion', 'Fashion'), ('Electronics', 'Electronics'), ('Home', 'Home'), ('Sports', 'Sports'), ('Toys', 'Toys'), ('Automobile', 'Automobile'), ('Books', 'Books'), ('Video-Games', 'Video-Games'), ('Other', 'Other')], max_length=64),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.8 on 2020-10-19 12:14
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('auctions', '0009_auto_20201019_1740'),
]
operations = [
migrations.AlterField(
model_name='auctionlisting',
name='category',
field=models.CharField(choices=[('Fashion', 'Fashion'), ('Electronics', 'Electronics'), ('Home', 'Home'), ('Sports', 'Sports'), ('Toys', 'Toys'), ('Automobile', 'Automobile'), ('Books', 'Books'), ('Videogames', 'Videogames'), ('Other', 'Other')], max_length=64),
),
]

View File

@ -2,17 +2,27 @@ from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
pass
watchlist = models.ManyToManyField('AuctionListing',blank = True,related_name="watchlists")
class AuctionListing(models.Model):
category_choices = (
('Fashion','Fashion'),
('Electronics','Electronics'),
('Home','Home'),
('Sports','Sports'),
('Toys','Toys'),
('Automobile','Automobile'),
('Books','Books'),
('Videogames','Videogames'),
('Other','Other'),
)
title = models.CharField(max_length = 64,primary_key = True)
user = models.ForeignKey(User,on_delete = models.CASCADE)
price = models.DecimalField(max_digits = 10,decimal_places = 2)
desc = models.CharField(max_length = 1000)
picture = models.URLField(null=True)
category = models.CharField(max_length = 64)
picture = models.URLField(default="https://www.riobeauty.co.uk/images/product_image_not_found.gif")
category = models.CharField(max_length = 64,choices=category_choices)
date_added = models.DateTimeField(auto_now_add=True)
watchlist = models.ManyToManyField(User,blank = True,related_name="watchlist")
def __str__(self):
return f"{self.title}: {self.price},{self.desc},{self.picture},{self.category},{self.date_added}"
@ -26,3 +36,8 @@ class Comment(models.Model):
def __str__(self):
return '%s - %s' % (self.listing.title, self.user)
class Bids(models.Model):
bid_value = models.DecimalField(max_digits = 10,decimal_places = 2)
user = models.ForeignKey(User,on_delete = models.CASCADE, related_name="bidding_user")
listing = models.ForeignKey(AuctionListing, on_delete= models.CASCADE, related_name="bidding_listing")

View File

@ -1,11 +1,13 @@
{% extends "auctions/layout.html"%}
{% block body %}
<h2>Listings under category: {{ cat_name }}</h2>
<h2>Listings under category "{{ cat_name }}"</h2>
{% if listings %}
<div class="row">
{% for list in listings %}
<div class="col-sm-3">
<a href="listing/{{ list.title}}" style = "color:inherit;text-decoration: none;">
<a href="{% url 'listing' {{list.title}} %}" style = "color:inherit;text-decoration: none;">
<div class="card border-light" style="width: 18rem;">
<div class="shadow p-2 mb-2 bg-white rounded">
<img class="card-img-top" src="{{ list.picture }}" alt="Preview not found">
@ -20,8 +22,19 @@
<b>created by</b>: {{list.user}}
</p>
<div class = "text-right">
<input class="form-check-input" type="checkbox" value="" id="watch">
<label class="form-check-label" for="watch">Watch this item</label>
{% if user.is_authenticated %}
<form action="{% url 'watchlist' list.title %}" method="POST">
{% csrf_token %}
{% if user not in list.watchlists.all %}
<button type="submit" name="watch", value="{{user}}", class="btn btn-primary btn-sm">Watch</button>
{% else %}
<button type="submit" name="watch", value="{{user}}", class="btn btn-primary btn-sm">Unwatch</button>
{% endif %}
</form>
{% else %}
<small>log in to watch this listing</small>
{% endif %}
</div>
@ -33,4 +46,13 @@
{% endfor %}
</div>
{% else %}
<br/>
<br/>
<br/>
<center>
<h3> Seems to be no listings in this category</h3>
</center>
{% endif %}
{% endblock %}

View File

@ -21,12 +21,24 @@
<small><i> created at {{listing.date_added}}</i> </small>
</p>
<div class = "text-light">
</div>
<div class = "text-right">
<form action="{% url 'watch' request.user %}" method="POST">
{% csrf_token %}
<button type="submit" name="watch", value="{{listing.title}}", class="btn btn-primary btn-sm">Watch</button>
</form>
{% if user.is_authenticated %}
<form action="{% url 'watchlist' listing.title %}" method="POST">
{% csrf_token %}
{% if user not in listing.watchlists.all %}
<button type="submit" name="watch", value="{{user}}", class="btn btn-primary btn-sm">Watch</button>
{% else %}
<button type="submit" name="watch", value="{{user}}", class="btn btn-primary btn-sm">Unwatch</button>
{% endif %}
</form>
{% else %}
<small>log in to watch this listing</small>
{% endif %}
</div>

View File

@ -25,7 +25,7 @@
</li>
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="{% url 'wishlist' %}">WishList</a>
<a class="nav-link" href="{% url 'wishlist' %}">WishList <span class="badge badge-secondary">{{ user.watchlist.count }}</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'create' %}">Create Listing</a>

View File

@ -2,12 +2,14 @@
{% extends "auctions/layout.html" %}
{% block body %}
<h2>Listing:{{ Listing.title}}</h2>
<img src = "{{Listing.picture}}"></img>
<h2>Listing : {{ Listing.title}}</h2>
<img src = "{{Listing.picture}}" width = "100%" height= "100%"></img>
<hr>
<p>{{Listing.desc}}</p>
<h3>${{Listing.price}}</h3>
bids so far
<div class="form-group">
@ -30,11 +32,17 @@
{% endif %}
{% if user.is_authenticated %}
<h5>add comment..</h5>
<p>
<form method="POST">
{% csrf_token %}
<input class = "form-control" name="comment_body" autofocus type="text" rows=4 cols= 50 placeholder="Comment here!">
<input class="btn btn-primary" type="submit" value="Submit">
</form>
</p>
{% else %}
<br/>
<hr>
Log In to comment on this listing
{% endif %}
{% endblock %}

View File

@ -0,0 +1,46 @@
{% extends "auctions/layout.html" %}
{% block body %}
<h2>WatchList </h2>
<div class="row">
{% for listing in Watchlistings %}
<div class="col-sm-3">
<a href="listing/{{ listing.title}}" style = "color:inherit;text-decoration: none;">
<div class="card border-light" style="width: 18rem;">
<div class="shadow p-2 mb-2 bg-white rounded">
<img class="card-img-top" src="{{ listing.picture }}" alt="Preview not found">
<hr>
<div class="card-body">
<h4>{{ listing.title }}</h4><br>
<p class="card-text" style="font-size: large;">
<b>bid price</b> : ${{listing.price}}<br>
<b>created by</b>: {{listing.user}}<br>
<small><i> created at {{listing.date_added}}</i> </small>
</p>
<div class = "text-right">
<form action = "{% url 'watchlist' listing.title %}" method="POST">
{% csrf_token %}
{% if user not in listing.watchlists.all %}
<button type="submit" name="watch", value="{{user}}", class="btn btn-primary btn-sm">Watch</button>
{% else %}
<button type="submit" name="watch", value="{{user}}", class="btn btn-secondary btn-sm">Unwatch</button>
{% endif %}
</form>
</div>
</div>
</div>
</div>
</a>
</div>
{% endfor %}
</div>
{% endblock %}

View File

@ -7,10 +7,11 @@ urlpatterns = [
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register"),
path("wishlist", views.register, name="wishlist"),
path("watchlist", views.watchlist, name="wishlist"),
path("create", views.create, name="create"),
path("categories", views.category, name="categories"),
path("category/<str:cat_name>",views.category,name="individual_categories"),
path("listing/<str:title>",views.listing,name="listing"),
path("watch/<str:user>",views.watch,name="watchlist")
path("category/listing/<str:title>",views.listing,name="listing"),
path("watch/<str:title>",views.watch,name="watchlist"),
]

View File

@ -7,7 +7,7 @@ from django import forms
from .models import User,AuctionListing,Comment
categories = ['Fashion','Electronics','Home','Sports','Toys','Automobile','Books','Video Games', 'Other']
categories = ['Fashion','Electronics','Home','Sports','Toys','Automobile','Books','Videogames', 'Other']
class Bid_Form(forms.Form):
bid = 0.0
def __init__(self,bid_value):
@ -15,11 +15,24 @@ class Bid_Form(forms.Form):
bid_value = forms.DecimalField(decimal_places=2,min_value= bid + 1)
def watch(request,title):
if request.method =="POST":
listing = AuctionListing.objects.get(title=title)
if request.user in listing.watchlists.all():
request.user.watchlist.remove(listing)
else:
request.user.watchlist.add(listing)
return HttpResponseRedirect(reverse("index"))
def watchlist(request):
listings = request.user.watchlist.all()
return render(request,"auctions/watchlist.html",{
"Watchlistings": listings
})
def index(request):
listings = AuctionListing.objects.all()
if request.method == "POST":
return render(request, "auctions/index.html",{
"Listings":listings,
@ -119,10 +132,14 @@ def create(request):
desc = request.POST["desc"]
starting_bid = request.POST["starting_bid"]
photo_url = request.POST["photo"]
if photo_url == None:
photo_url = "https://www.riobeauty.co.uk/images/product_image_not_found.gif"
category = request.POST["category"]
obj = AuctionListing(title = title, desc = desc, user = request.user,price = starting_bid, picture = photo_url,category=category)
obj.save()
return render(request,"auctions/index.html")
return render(request,"auctions/index.html",{
"Listings" : AuctionListing.objects.all()
})
else:
return render(request,"auctions/create_listing.html",{
"categories":categories

Binary file not shown.