Priyatham-sai-chand/auctions/views.py

228 lines
6.6 KiB
Python
Raw Normal View History

2020-10-15 10:18:39 -07:00
from django.contrib.auth import authenticate, login, logout
from django.db import IntegrityError
2020-10-25 11:05:22 -07:00
from django.http import HttpResponseRedirect
2020-10-15 10:18:39 -07:00
from django.shortcuts import render
from django.urls import reverse
2020-10-18 09:27:28 -07:00
from django import forms
2020-10-20 10:11:58 -07:00
from .models import User,AuctionListing,Comment,Bids
2020-10-15 10:18:39 -07:00
2020-10-19 06:59:55 -07:00
categories = ['Fashion','Electronics','Home','Sports','Toys','Automobile','Books','Videogames', 'Other']
2020-10-25 11:05:22 -07:00
class BidForm(forms.Form):
2020-10-22 10:35:00 -07:00
bid_value = 0.00
2020-10-25 11:05:22 -07:00
def __init__(self, bid_value,*args,**kwargs ):
super().__init__(*args,**kwargs)
self.bid_value = bid_value + 1
self.fields['bid'].widget.attrs['min'] = self.bid_value
2020-10-22 10:35:00 -07:00
bid = forms.DecimalField(decimal_places=2)
2020-10-18 09:27:28 -07:00
2020-10-19 06:59:55 -07:00
def watch(request,title):
2020-10-25 11:05:22 -07:00
"""
Used to assign watchlist and unwatch for a user
"""
2020-10-19 06:59:55 -07:00
if request.method =="POST":
2020-10-25 11:05:22 -07:00
listing_obj = AuctionListing.objects.get(title=title)
if request.user in listing_obj.watchlists.all():
request.user.watchlist.remove(listing_obj)
2020-10-19 06:59:55 -07:00
else:
2020-10-25 11:05:22 -07:00
request.user.watchlist.add(listing_obj)
2020-10-20 10:11:58 -07:00
previous_url = request.POST.get('previous','/')
return HttpResponseRedirect(previous_url)
2020-10-25 11:05:22 -07:00
2020-10-19 06:59:55 -07:00
def watchlist(request):
2020-10-25 11:05:22 -07:00
"""
Used to display the page Watchlist
"""
2020-10-19 06:59:55 -07:00
listings = request.user.watchlist.all()
2020-10-18 09:27:28 -07:00
2020-10-19 06:59:55 -07:00
return render(request,"auctions/watchlist.html",{
"Watchlistings": listings
})
2020-10-15 10:18:39 -07:00
def index(request):
2020-10-25 11:05:22 -07:00
"""
Used to Display the homepage
"""
2020-10-15 10:18:39 -07:00
listings = AuctionListing.objects.all()
2020-10-19 06:59:55 -07:00
2020-10-15 10:18:39 -07:00
return render(request, "auctions/index.html",{
2020-10-18 09:27:28 -07:00
"Listings":listings,
2020-10-18 13:06:31 -07:00
2020-10-15 10:18:39 -07:00
})
2020-10-25 11:05:22 -07:00
def all_listings(request):
"""
Used to display all the listings
"""
listings = AuctionListing.objects.all()
return render(request,"auctions/all_listings.html",{
"Listings":listings
})
2020-10-15 10:18:39 -07:00
def listing(request,title):
"""
2020-10-25 11:05:22 -07:00
Used to display the listing page and commenting
2020-10-15 10:18:39 -07:00
"""
2020-10-25 11:05:22 -07:00
listing_obj = AuctionListing.objects.get(pk = title)
bids = Bids.objects.get(listing = listing_obj)
2020-10-18 13:06:31 -07:00
if request.method == "POST":
body = request.POST["comment_body"]
2020-10-25 11:05:22 -07:00
comment = Comment(user=request.user,listing=listing_obj,body = body)
2020-10-18 13:06:31 -07:00
comment.save()
2020-10-20 10:11:58 -07:00
2020-10-15 22:04:50 -07:00
return render(request,"auctions/listing.html",{
2020-10-25 11:05:22 -07:00
"Listing": listing_obj,
2020-10-22 10:35:00 -07:00
"bids" : bids,
2020-10-25 11:05:22 -07:00
"bid_form":BidForm(bids.bid_value)
2020-10-15 10:18:39 -07:00
})
2020-10-18 13:06:31 -07:00
2020-10-16 09:38:28 -07:00
def category(request,cat_name = None):
"""
2020-10-25 11:05:22 -07:00
Used to dispaly the category page
2020-10-16 09:38:28 -07:00
"""
if( cat_name != None):
if cat_name.capitalize() in categories:
2020-10-18 09:27:28 -07:00
listings = AuctionListing.objects.filter(category=cat_name.capitalize())
2020-10-16 09:38:28 -07:00
print(listings)
2020-10-15 10:18:39 -07:00
2020-10-16 09:38:28 -07:00
return render(request, "auctions/category.html",{
2020-10-18 09:27:28 -07:00
"listings":listings,
"cat_name": cat_name
2020-10-16 09:38:28 -07:00
})
else:
return render(request,"auctions/category_listing.html",{
"categories": categories
})
2020-10-18 13:06:31 -07:00
2020-10-15 10:18:39 -07:00
def login_view(request):
2020-10-25 11:05:22 -07:00
"""
Used to authenticate a user
"""
2020-10-15 10:18:39 -07:00
if request.method == "POST":
# Attempt to sign user in
username = request.POST["username"]
password = request.POST["password"]
user = authenticate(request, username=username, password=password)
# Check if authentication successful
if user is not None:
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return render(request, "auctions/login.html", {
"message": "Invalid username and/or password."
})
else:
return render(request, "auctions/login.html")
def logout_view(request):
2020-10-25 11:05:22 -07:00
"""
Used to log out a user
"""
2020-10-15 10:18:39 -07:00
logout(request)
return HttpResponseRedirect(reverse("index"))
def register(request):
2020-10-25 11:05:22 -07:00
"""
Used to register a user
"""
2020-10-15 10:18:39 -07:00
if request.method == "POST":
username = request.POST["username"]
email = request.POST["email"]
# Ensure password matches confirmation
password = request.POST["password"]
confirmation = request.POST["confirmation"]
if password != confirmation:
return render(request, "auctions/register.html", {
"message": "Passwords must match."
})
# Attempt to create new user
try:
user = User.objects.create_user(username, email, password)
user.save()
except IntegrityError:
return render(request, "auctions/register.html", {
"message": "Username already taken."
})
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return render(request, "auctions/register.html")
2020-10-25 11:05:22 -07:00
2020-10-15 10:18:39 -07:00
def create(request):
"""
2020-10-25 11:05:22 -07:00
Used to create a Listing
2020-10-15 10:18:39 -07:00
"""
2020-10-25 11:05:22 -07:00
if request.method == "POST":
2020-10-15 10:18:39 -07:00
title = request.POST["title"]
desc = request.POST["desc"]
starting_bid = request.POST["starting_bid"]
photo_url = request.POST["photo"]
2020-10-25 11:05:22 -07:00
# alternate photo to display
2020-10-20 10:11:58 -07:00
if photo_url == "":
2020-10-22 10:35:00 -07:00
photo_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/No_image_available_600_x_450.svg/1280px-No_image_available_600_x_450.svg.png"
2020-10-25 11:05:22 -07:00
category_value = request.POST["category"]
listing_obj = AuctionListing(title = title, desc = desc, user = request.user,price = starting_bid, picture = photo_url,category=category_value)
2020-10-20 10:11:58 -07:00
listing_obj.save()
bid_obj = Bids(bid_value = starting_bid, listing = listing_obj, user = request.user)
bid_obj.save()
2020-10-19 06:59:55 -07:00
return render(request,"auctions/index.html",{
"Listings" : AuctionListing.objects.all()
})
2020-10-15 10:18:39 -07:00
else:
2020-10-16 09:38:28 -07:00
return render(request,"auctions/create_listing.html",{
"categories":categories
})
2020-10-20 10:11:58 -07:00
def bid(request, title):
2020-10-25 11:05:22 -07:00
"""
Used to bid on a listing
"""
listing_value = AuctionListing.objects.get(title=title)
bid_obj = Bids.objects.get(listing = listing_value)
form = BidForm(bid_obj.bid_value,request.POST)
if form.is_valid():
new_bid_value = form.cleaned_data["bid"]
bid_obj.bid_value = float("{0:.2f}".format(new_bid_value))
bid_obj.user = request.user
bid_obj.save()
2020-10-20 10:11:58 -07:00
return render(request,"auctions/listing.html",{
2020-10-25 11:05:22 -07:00
"Listing": listing_value,
"bids" : bid_obj,
"bid_form":BidForm(bid_obj.bid_value)
2020-10-20 10:11:58 -07:00
})
def close_bid(request,title):
2020-10-25 11:05:22 -07:00
"""
Used to end the listing and declare a winner
"""
listing_value = AuctionListing.objects.get(title=title)
listing_value.closed = True
listing_value.save()
2020-10-21 08:59:14 -07:00
2020-10-25 11:05:22 -07:00
# get a hidden input to return back to same page
previous_url = request.POST.get('previous','/')
2020-10-21 08:59:14 -07:00
return HttpResponseRedirect(previous_url)
2020-10-18 13:06:31 -07:00