Priyatham-sai-chand/auctions/views.py

182 lines
5.7 KiB
Python
Raw Normal View History

2020-10-15 10:18:39 -07:00
from django.contrib.auth import authenticate, login, logout
2020-10-22 10:35:00 -07:00
from django.core.validators import MinValueValidator, MaxValueValidator
2020-10-15 10:18:39 -07:00
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
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-18 09:27:28 -07:00
class Bid_Form(forms.Form):
2020-10-22 10:35:00 -07:00
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)
2020-10-18 09:27:28 -07:00
2020-10-19 06:59:55 -07:00
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)
2020-10-20 10:11:58 -07:00
previous_url = request.POST.get('previous','/')
return HttpResponseRedirect(previous_url)
2020-10-19 06:59:55 -07:00
def watchlist(request):
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):
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
})
def listing(request,title):
"""
docstring
"""
2020-10-20 10:11:58 -07:00
listing = AuctionListing.objects.get(pk = title)
bids = Bids.objects.get(listing = listing)
2020-10-18 13:06:31 -07:00
if request.method == "POST":
body = request.POST["comment_body"]
2020-10-20 10:11:58 -07:00
comment = Comment(user=request.user,listing=listing,body = body)
2020-10-18 13:06:31 -07:00
comment.save()
2020-10-20 10:11:58 -07:00
2020-10-18 13:06:31 -07:00
2020-10-15 22:04:50 -07:00
return render(request,"auctions/listing.html",{
2020-10-20 10:11:58 -07:00
"Listing": listing,
2020-10-22 10:35:00 -07:00
"bids" : bids,
"bid_form":Bid_Form(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):
"""
docstring
"""
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):
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"]
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-16 09:38:28 -07:00
category = request.POST["category"]
2020-10-20 10:11:58 -07:00
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()
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):
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,
2020-10-22 10:35:00 -07:00
"bids" : bid,
"bid_form":Bid_Form(bid.bid_value)
2020-10-20 10:11:58 -07:00
})
def close_bid(request,title):
2020-10-21 08:59:14 -07:00
listing = AuctionListing.objects.get(title=title)
listing.closed = True
listing.save()
previous_url = request.POST.get('previous','/')
print(previous_url)
return HttpResponseRedirect(previous_url)
2020-10-18 13:06:31 -07:00