Priyatham-sai-chand/auctions/views.py

182 lines
5.7 KiB
Python

from django.contrib.auth import authenticate, login, logout
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django import forms
from .models import User,AuctionListing,Comment,Bids
categories = ['Fashion','Electronics','Home','Sports','Toys','Automobile','Books','Videogames', 'Other']
class Bid_Form(forms.Form):
bid_value = 0.00
def __init__(self, *args, **kwargs):
super(Bid_Form, self).__init__(*args, **kwargs)
self.fields['bid'].widget.attrs['min'] = 10
bid = forms.DecimalField(decimal_places=2)
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)
previous_url = request.POST.get('previous','/')
return HttpResponseRedirect(previous_url)
def watchlist(request):
listings = request.user.watchlist.all()
return render(request,"auctions/watchlist.html",{
"Watchlistings": listings
})
def index(request):
listings = AuctionListing.objects.all()
return render(request, "auctions/index.html",{
"Listings":listings,
})
def listing(request,title):
"""
docstring
"""
listing = AuctionListing.objects.get(pk = title)
bids = Bids.objects.get(listing = listing)
if request.method == "POST":
body = request.POST["comment_body"]
comment = Comment(user=request.user,listing=listing,body = body)
comment.save()
return render(request,"auctions/listing.html",{
"Listing": listing,
"bids" : bids,
"bid_form":Bid_Form(bids.bid_value)
})
def category(request,cat_name = None):
"""
docstring
"""
if( cat_name != None):
if cat_name.capitalize() in categories:
listings = AuctionListing.objects.filter(category=cat_name.capitalize())
print(listings)
return render(request, "auctions/category.html",{
"listings":listings,
"cat_name": cat_name
})
else:
return render(request,"auctions/category_listing.html",{
"categories": categories
})
def login_view(request):
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):
logout(request)
return HttpResponseRedirect(reverse("index"))
def register(request):
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")
def create(request):
"""
docstring
"""
if request.method == "POST":
title = request.POST["title"]
desc = request.POST["desc"]
starting_bid = request.POST["starting_bid"]
photo_url = request.POST["photo"]
if photo_url == "":
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"
category = request.POST["category"]
listing_obj = AuctionListing(title = title, desc = desc, user = request.user,price = starting_bid, picture = photo_url,category=category)
listing_obj.save()
bid_obj = Bids(bid_value = starting_bid, listing = listing_obj, user = request.user)
bid_obj.save()
return render(request,"auctions/index.html",{
"Listings" : AuctionListing.objects.all()
})
else:
return render(request,"auctions/create_listing.html",{
"categories":categories
})
def bid(request, title):
listing = AuctionListing.objects.get(title=title)
bid = Bids.objects.get(listing = listing)
new_bid_value = request.POST["bid"]
bid.bid_value = new_bid_value
bid.user = request.user
return render(request,"auctions/listing.html",{
"Listing": listing,
"bids" : bid,
"bid_form":Bid_Form(bid.bid_value)
})
def close_bid(request,title):
listing = AuctionListing.objects.get(title=title)
listing.closed = True
listing.save()
previous_url = request.POST.get('previous','/')
print(previous_url)
return HttpResponseRedirect(previous_url)