Here is the complete view:
from django.contrib.auth import authenticate, login, logout
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.contrib.auth.decorators import login_required
from .forms import *
from django.contrib.auth.models import User
from django.forms import modelform_factory
from django.db import connections
import logging
from django.utils import timezone
from datetime import datetime, timedelta, tzinfo
from .models import *
#Show the login screen to the user
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")
# Log out function
def logout_view(request):
logout(request)
return HttpResponseRedirect(reverse("index"))
# Register function
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")
# Dashboard view
def index(request):
return render(request, "auctions/index.html",{
"listings": Listings.objects.filter(status='Active')
})
# Create Listing function
@login_required(login_url='/login')
def CreateListing (request):
if request.method == 'POST':
form1 = CreateListingForm(request.POST)
if form1.is_valid():
listing = Listings()
listing.title = form1.cleaned_data["title"]
listing.description = form1.cleaned_data["description"]
listing.Price = form1.cleaned_data["Price"]
listing.seller = request.user
listing.category = form1.cleaned_data["category"]
listing.created_at = datetime.now()
listing.Image = form1.cleaned_data["Image"]
listing.startingPrice = listing.Price
listing.save()
return HttpResponseRedirect(reverse("index",))
else:
form1 = CreateListingForm()
return render(request,"auctions/createListing.html",{"form": form1})
# save listing changes
@login_required(login_url='/login')
def SaveListingChanges (request,Listing_id):
if request.method == 'POST':
form1 = CreateListingForm(request.POST)
if form1.is_valid():
listing = Listings.objects.get(pk=Listing_id)
listing.title = form1.cleaned_data["title"]
listing.description = form1.cleaned_data["description"]
listing.Price = form1.cleaned_data["Price"]
listing.seller = request.user
listing.category = form1.cleaned_data["category"]
listing.created_at = datetime.now()
listing.Image = form1.cleaned_data["Image"]
listing.save()
return HttpResponseRedirect(reverse("index",))
# Edit listing view
@login_required(login_url='/login')
def EditListing(request,Listing_id):
objList = Listings.objects.get(pk=Listing_id)
form1 = CreateListingForm(initial={'title': objList.title, 'description': objList.description, 'category': objList.category, 'Price': objList.Price, 'Image': objList.Image}, instance=objList)
return render(request, "auctions/EditListing.html",{
"listing": objList,
"form": form1,
})
#Show active listings
@login_required
def activeListings(request):
return render(request, "auctions/ActiveListings.html",{
"listings":Listings.objects.filter(status='Active')
})
# View Listing function
@login_required
def ViewListing (request,Listing_id):
listing = Listings.objects.get(pk=Listing_id)
objWatchList = WatchLists.objects.filter(listing_id=Listing_id,user=request.user)
if not objWatchList:
watchlist_flag = 0
else:
watchlist_flag = 1
msg = ""
if request.method == "POST":
form = BidForm(request.POST)
msg=2
if form.is_valid():
bid = form.cleaned_data["Bid"]
if listing.Price >= bid:
msg = 0
else:
listing.Price = bid
listing.save()
objBid = Bids()
objBid.listing_id = listing
objBid.user = request.user
objBid.Bid = bid
objBid.save()
msg = 1
BidsForm1 = BidForm()
CommentForm1 = CommentForm()
comments = Comments.objects.filter(listing_id = Listing_id)
return render(request,"auctions/ViewListing.html",{
"Bidform": BidsForm1,
"listing":listing,
"Commentform":CommentForm1,
"comments":comments,
"msg":msg,
"watchlist_flag":watchlist_flag,
})
def WatchList(request, Listing_id):
listing = Listings.objects.get(pk=Listing_id)
objWatchList = WatchLists.objects.filter(user = request.user, listing_id = Listing_id)
objW = WatchLists()
objW.listing_id = Listing_id
objW.user = request.user
objW.save()
# View items in the WatchList tab
@login_required
def WatchListing(request):
WatchListing = WatchLists.objects.all()
#Create empty list to add items which are in 'Watchlists' of a user
return render(request, "auctions/WatchList.html",{
"WatchList_listing": WatchListing,
"Total_watchlistings":len(WatchListing),
})
# View 'Close' listings
@login_required
def CloseListing (request, Listing_id):
listing_item = Listings.objects.get(pk=Listing_id)
try:
bidwinner = Bids.objects.get(listing_id=Listing_id,Bid=listing_item.Price)
listing_item.winner=bidwinner.user
except Bids.DoesNotExist:
bidwinner=None
listing_item.winner = None
listing_item.status = 'Closed'
listing_item.save()
return render(request, "auctions/ClosedListing.html",{
"listings":Listings.objects.filter(status='Closed'),
})
# View 'Closed' listings
@login_required
def ClosedListing (request):
listings = Listings.objects.filter(status="Closed")
# Bid winner should be updated if the 'Admin' delete the last bid.
for listing in listings:
bidOnListing = Bids.objects.filter(listing_id=listing).last()
if bidOnListing == None:
objBid = False
else:
objBid = bidOnListing.Bid
if objBid:
listing.Price=objBid
bidwinner = Bids.objects.get(listing_id=listing,Bid=objBid)
listing.winner = bidwinner.user
listing.save()
else:
listing.Price = listing.startingPrice
listing.winner = None
listing.save()
return render(request, "auctions/ClosedListing.html",{
"listings":Listings.objects.filter(status='Closed'),
})
# view for comments
@login_required
def AddComment(request, Listing_id):
listing = Listings.objects.get(pk=Listing_id)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.user = request.user
comment.listing_id=Listing_id
comment.created_at=datetime.now()
comment.save()
listing.comments.add(comment)
listing.save()
return HttpResponseRedirect(reverse("ViewListing", args=(Listing_id,)))
else:
return HttpResponseRedirect(reverse("ViewListing", args=(Listing_id,)))
else:
return HttpResponseRedirect(reverse("ViewListing", args=(Listing_id,)))
# Show the listing categories available in the commerce site
@login_required
def CategoriesFn (request):
return render(request, "auctions/Categories.html",{
"categories":CATEGORY_CHOICES,
})
# Show listings sorted as per Category
@login_required
def CatFn(request,category):
ListingsPerCat= Listings.objects.filter(category=category,status="Active")
return render(request, "auctions/CategoryView.html",{
"listings": ListingsPerCat,
"cat": category
})