Changed the cancel button text color for the link for the forms.

Added an error 404 for past auctions.
Added clean code for each view in views.py.
parent 1e688426
...@@ -582,3 +582,7 @@ nav a { ...@@ -582,3 +582,7 @@ nav a {
.help-text { .help-text {
font-size: 15px; font-size: 15px;
} }
.link-text {
color: white;
}
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
{{ form.as_p }} {{ form.as_p }}
</div> </div>
<div class="button-space"> <div class="button-space">
<button type="button" class="btn btn-danger"><a href= "{% url 'storeid' request.user.id %}">Cancel</a></button> <button type="button" class="btn btn-danger"><a class="link-text" href= "{% url 'storeid' request.user.id %}">Cancel</a></button>
<button type="submit" class="btn btn-dark" name="Add Item"> Add Item </button> <button type="submit" class="btn btn-dark" name="Add Item"> Add Item </button>
</div> </div>
</form> </form>
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
{% csrf_token %} {% csrf_token %}
{{ form.as_p }} {{ form.as_p }}
<div class="btn-group"> <div class="btn-group">
<button type="button" class="btn btn-danger"><a href= "{% url 'profileid' request.user.id %}">Cancel</a></button> <button type="button" class="btn btn-danger"><a class="link-text" href= "{% url 'profileid' request.user.id %}">Cancel</a></button>
<button class="btn profile-btn" type="submit" name="Add Item"> Save Changes </button> <button class="btn profile-btn" type="submit" name="Add Item"> Save Changes </button>
</div> </div>
</form> </form>
......
{% extends 'boodlesite\templates\error404\base_error404.html' %}
{% load static %}
{% block title %}{% endblock %}
{% block styles %}
{% endblock %}
{% block content %}
<div class="container">
<div class="error404__icon">
<img src="../../static/media/cashier.png" alt="">
</div>
<div class="error404__message">
<h1>This auction has already passed.</h1>
<a href="/">Back to HomePage</a>
</div>
</div>
{% endblock %}
\ No newline at end of file
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
{{ form.as_p }} {{ form.as_p }}
</div> </div>
<div class="button-space"> <div class="button-space">
<button type="button" class="btn btn-danger"><a href= "{% url 'storeid' request.user.id %}">Cancel</a></button> <button type="button" class="btn btn-danger"><a class="link-text" href= "{% url 'storeid' request.user.id %}">Cancel</a></button>
<button type="submit" class="btn btn-dark" name="savesauction">Save Auction</button> <button type="submit" class="btn btn-dark" name="savesauction">Save Auction</button>
</div> </div>
</form> </form>
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
{{ form.as_p }} {{ form.as_p }}
</div> </div>
<div class="button-space"> <div class="button-space">
<button type="button" class="btn btn-danger"><a href= "{% url 'storeid' request.user.id %}">Cancel</a></button> <button type="button" class="btn btn-danger"><a class="link-text" href= "{% url 'storeid' request.user.id %}">Cancel</a></button>
<button type="submit" class="btn btn-dark" name="Add Item"> Save Changes </button> <button type="submit" class="btn btn-dark" name="Add Item"> Save Changes </button>
</div> </div>
</form> </form>
......
...@@ -7,7 +7,8 @@ urlpatterns = [ ...@@ -7,7 +7,8 @@ urlpatterns = [
path('', homepage, name='index'), path('', homepage, name='index'),
path('auction', auction, name='auction'), path('auction', auction, name='auction'),
path('auction/<int:pk>/',auction, name='auctionid'), path('auction/<int:pk>/',auction, name='auctionid'),
path('error404', error404, name='error404'), path('future_auction_error404', future_auction_error404, name='future_auction_error404'),
path('passed_auction_error404', passed_auction_error404, name='passed_auction_error404'),
path('store/<int:pk>', mystore, name='storeid'), path('store/<int:pk>', mystore, name='storeid'),
path('additem', addItem, name='additem'), path('additem', addItem, name='additem'),
path('additem/<int:pk>', addItem, name='additemid'), path('additem/<int:pk>', addItem, name='additemid'),
......
...@@ -16,7 +16,9 @@ from .forms import * ...@@ -16,7 +16,9 @@ from .forms import *
from datetime import datetime, timedelta from datetime import datetime, timedelta
def registerPage(request): def registerPage(request):
# dont want a logged in user to see this '''
Any user can only register an account on Boodle. They must give a unique username or they will be asked to input another one. A logged-in user cannot see the registration page unless they log-out of Boodle.
'''
if request.user.is_authenticated: if request.user.is_authenticated:
return redirect('/') return redirect('/')
else: else:
...@@ -29,9 +31,6 @@ def registerPage(request): ...@@ -29,9 +31,6 @@ def registerPage(request):
password = form.cleaned_data.get('password1') password = form.cleaned_data.get('password1')
messages.success(request, 'Account was create for ' + user_name) messages.success(request, 'Account was create for ' + user_name)
# boodleuser_inst = BoodleUser.objects.create(displayname=user_name, pword=password, username=user_name)
# boodleuser_inst.save()
return redirect('login') return redirect('login')
...@@ -39,8 +38,12 @@ def registerPage(request): ...@@ -39,8 +38,12 @@ def registerPage(request):
return render(request, 'boodlesite/templates/registration/register.html', context) return render(request, 'boodlesite/templates/registration/register.html', context)
def loginPage(request): def loginPage(request):
'''
# dont want a logged in user to see this A user can only log in if they have registered account on Boodle.
They must input the correct password and username or else they won't be let in the site.
They will be redirected to Boodle's home page if credentials are valid.
A logged-in user cannot see the log-in page unless they log-out of Boodle.
'''
if request.user.is_authenticated: if request.user.is_authenticated:
return redirect('/') return redirect('/')
else: else:
...@@ -53,29 +56,32 @@ def loginPage(request): ...@@ -53,29 +56,32 @@ def loginPage(request):
login(request, boodle_user) login(request, boodle_user)
return redirect('/') return redirect('/')
else: else:
messages.info(request, 'Username OR Password is incorrect') # all msgs get sent here will be output messages.info(request, 'Username OR Password is incorrect')
context = {} return render(request, 'boodlesite/templates/registration/login.html')
return render(request, 'boodlesite/templates/registration/login.html', context)
def logoutUser(request): def logoutUser(request):
'''
Logs auser out of their Boodle account.
Redirects a user to the login page when activated.
'''
logout(request) logout(request)
return redirect('login') return redirect('login')
@login_required(login_url='login') @login_required(login_url='login')
def homepage(request): def homepage(request):
print(Auction.objects.all()) '''
Only a logged-in user has access to this view.
# Filter by auctions happening right now A logged-in user has access to the current and future auctions. They may choose from the selection on which they would like to participate in.
These auctions are available only at the end time set by the seller. This applies to both current and future auctions.
A user also has access to the other parts of the site from here through the navigation bar.
'''
auctions_now = Auction.objects.filter(auctionstart__lt=datetime.now(),auctionend__gt=datetime.now()) auctions_now = Auction.objects.filter(auctionstart__lt=datetime.now(),auctionend__gt=datetime.now())
for auction in auctions_now: for auction in auctions_now:
print(auction) print(auction)
# Filter by auctions scheduled at most a week from now
week_range = datetime.now() + timedelta(days=7) week_range = datetime.now() + timedelta(days=7)
auctions_soon = Auction.objects.filter(auctionstart__lt=week_range).exclude(auctionstart__lte=datetime.now()) auctions_soon = Auction.objects.filter(auctionstart__lt=week_range).exclude(auctionstart__lte=datetime.now())
for auction in auctions_soon:
print(auction)
context = { context = {
'auctions_now': auctions_now, 'auctions_now': auctions_now,
...@@ -86,20 +92,18 @@ def homepage(request): ...@@ -86,20 +92,18 @@ def homepage(request):
@login_required(login_url='login') @login_required(login_url='login')
def auction(request,pk): def auction(request,pk):
'''
# Current auction ID Only a logged-in user has access to this view.
An auction is viewable only when it is ongoing. A user cannot view past nor future auctions -- they will receive an error page if they try to access future auctions. This indicates they should go back to the home screen.
A user also has access to the other parts of the site from here through the navigation bar.
'''
auction = Auction.objects.get(pk=pk) auction = Auction.objects.get(pk=pk)
# Item for auction auction_item = auction.itemid
auction_item = auction.itemid # this is the itemfk thru auction
# Host
auction_host = auction_item.storeid auction_host = auction_item.storeid
# Auction bids
auction_bids = AuctionBid.objects.filter(auctionid=pk).order_by('-bidtime') auction_bids = AuctionBid.objects.filter(auctionid=pk).order_by('-bidtime')
highest_bid = auction_item.floorprice highest_bid = auction_item.floorprice
## ⭐ the user that is logged in
users = AuthUser.objects.get(id=request.user.id) users = AuthUser.objects.get(id=request.user.id)
userid = users.id userid = users.id
...@@ -113,11 +117,10 @@ def auction(request,pk): ...@@ -113,11 +117,10 @@ def auction(request,pk):
if form.is_valid(): if form.is_valid():
try: try:
amount = form.cleaned_data['amount'] amount = form.cleaned_data['amount']
# saves the bid by auctionid, amount, bidtime, boodleuserid
new_bid = AuctionBid( new_bid = AuctionBid(
amount=amount, bidtime=datetime.now(), amount=amount, bidtime=datetime.now(),
auctionid=auction, userid=users) auctionid=auction, userid=users)
new_bid.save() new_bid.save() # saves the bid by auctionid, amount, bidtime, boodleuserid
return redirect(f"/auction/{pk}") return redirect(f"/auction/{pk}")
except Exception as e: except Exception as e:
print("Error:", e) print("Error:", e)
...@@ -136,29 +139,58 @@ def auction(request,pk): ...@@ -136,29 +139,58 @@ def auction(request,pk):
} }
if auction.auctionend < datetime.now(): if auction.auctionend < datetime.now():
return HttpResponse("This auction has already passed.") return render(request, "boodlesite/templates/error404/passed_error404.html")
elif auction.auctionstart > datetime.now(): elif auction.auctionstart > datetime.now():
return render(request, "boodlesite/templates/error404/notstarted_error404.html") return render(request, "boodlesite/templates/error404/notstarted_error404.html")
else: else:
return render(request, "boodlesite/templates/auction.html",context) return render(request, "boodlesite/templates/auction.html",context)
@login_required(login_url='login') @login_required(login_url='login')
def error404(request): def passed_auction_error404(request):
'''
Only a logged-in user has access to this view.
A user receives this view when they try to view passed auctions.
A user also has access to the other parts of the site from here through the navigation bar.
'''
return render(request, "boodlesite/templates/error404/passed_error404.html")
@login_required(login_url='login')
def future_auction_error404(request):
'''
Only a logged-in user has access to this view.
A user receives this view when they try to view future auctions.
A user also has access to the other parts of the site from here through the navigation bar.
'''
return render(request, "boodlesite/templates/error404/notstarted_error404.html") return render(request, "boodlesite/templates/error404/notstarted_error404.html")
@login_required(login_url='login') @login_required(login_url='login')
def about(request): def about(request):
'''
Only a logged-in user has access to this view.
This shows relevant information on Boodle and the creators.
A user also has access to the other parts of the site from here through the navigation bar.
'''
return render(request, "boodlesite/templates/about.html") return render(request, "boodlesite/templates/about.html")
@login_required(login_url='login') @login_required(login_url='login')
def help(request): def help(request):
'''
Only a logged-in user has access to this view.
This shows relevant information on how to use Boodle.
A user also has access to the other parts of the site from here through the navigation bar.
'''
return render(request, "boodlesite/templates/help.html") return render(request, "boodlesite/templates/help.html")
@login_required(login_url='login') @login_required(login_url='login')
def mystore(request, pk): def mystore(request, pk):
'''
#### Access to store 1 [ edit accordingly when it becomes accessible thru a user ] #### Only a logged-in user has access to this view.
# pk is storeid This shows the store owned by a user. If a user has not made a store, this view is not available to them. The store contains items they want to/currently put on auction.
This view is where a user [seller] can start an auction to be put up in the homepage for every user [buyers and sellers] to see.
The user [seller] can also edit the item details and store details in this view.
A user also has access to the other parts of the site from here through the navigation bar.
'''
current_store = Store.objects.get(pk=pk) current_store = Store.objects.get(pk=pk)
store_owner = current_store.userid store_owner = current_store.userid
store_items = Item.objects.filter(storeid=pk) store_items = Item.objects.filter(storeid=pk)
...@@ -197,8 +229,11 @@ def mystore(request, pk): ...@@ -197,8 +229,11 @@ def mystore(request, pk):
@login_required(login_url='login') @login_required(login_url='login')
def addItem(request, pk): def addItem(request, pk):
'''
# Current Store, pk here is the storeid Only a logged-in user has access to this view.
This view is a form wherin users [sellers] can add items to their store. After successfully making the item, they will be redirected back to the store.
A user also has access to the other parts of the site from here through the navigation bar.
'''
current_store = Store.objects.get(pk=pk) current_store = Store.objects.get(pk=pk)
form = AddItemForm(initial={'storeid':current_store}) form = AddItemForm(initial={'storeid':current_store})
...@@ -219,6 +254,11 @@ def addItem(request, pk): ...@@ -219,6 +254,11 @@ def addItem(request, pk):
@login_required(login_url='login') @login_required(login_url='login')
def editItem(request, pk): def editItem(request, pk):
'''
Only a logged-in user has access to this view.
This view is a form wherin users [sellers] can edit item details on their store. After successfully editing the item, they will be redirected back to the store.
A user also has access to the other parts of the site from here through the navigation bar.
'''
item = Item.objects.get(itemid=pk) item = Item.objects.get(itemid=pk)
current_store = item.storeid current_store = item.storeid
...@@ -239,19 +279,19 @@ def editItem(request, pk): ...@@ -239,19 +279,19 @@ def editItem(request, pk):
@login_required(login_url='login') @login_required(login_url='login')
def startAuction(request, pk): def startAuction(request, pk):
'''
Only a logged-in user has access to this view.
This view is a form wherin users [sellers] can start an auction to be viewed and accessed by the public.
They will have to fill in the details of their auction such as when they want it to start and end, and the particular item that is up for grabs.
A user also has access to the other parts of the site from here through the navigation bar.
'''
# pk is store id
current_store = Store.objects.get(pk=pk) current_store = Store.objects.get(pk=pk)
store_id = current_store.storeid store_id = current_store.storeid
# get items under this store
store_items = Item.objects.filter(storeid=store_id) store_items = Item.objects.filter(storeid=store_id)
# Current userid, change as per ⭐ whoever is logged in
user = AuthUser.objects.get(id=request.user.id) user = AuthUser.objects.get(id=request.user.id)
userid = user.id userid = user.id
# temp: all auctions
all_auctions = Auction.objects.all()
form = StartAuctionForm(initial={'auctionstart':datetime.now()}) form = StartAuctionForm(initial={'auctionstart':datetime.now()})
form.fields["itemid"].queryset = store_items form.fields["itemid"].queryset = store_items
...@@ -281,10 +321,13 @@ def startAuction(request, pk): ...@@ -281,10 +321,13 @@ def startAuction(request, pk):
@login_required(login_url='login') @login_required(login_url='login')
def profile(request, pk): def profile(request, pk):
'''
Only a logged-in user has access to this view.
This view is where the user can see the details of their profile. They have the option to make a store, access their store (only when they've made one) and edit their profile details.
The user can also see the auctions they have participated in.
A user also has access to the other parts of the site from here through the navigation bar.
'''
current_user = AuthUser.objects.get(pk=pk) current_user = AuthUser.objects.get(pk=pk)
#auction bid user id = 3 --> bids user made --> know auctions g
## ⭐ the user that is logged in
bids_by_user = AuctionBid.objects.filter(userid=pk).distinct('auctionid') bids_by_user = AuctionBid.objects.filter(userid=pk).distinct('auctionid')
auctions_of_user = Auction.objects.all().distinct('auctionid') auctions_of_user = Auction.objects.all().distinct('auctionid')
...@@ -294,28 +337,18 @@ def profile(request, pk): ...@@ -294,28 +337,18 @@ def profile(request, pk):
for auction in auctions_of_user: for auction in auctions_of_user:
if bid.auctionid == auction: if bid.auctionid == auction:
ids_of_auction.append(bid.auctionid) ids_of_auction.append(bid.auctionid)
# print("These are the distinct auction IDs: ", idsOfAuction)
#💫auctionsOfUser = Auction.objects.all().distinct('auctionid')
# get existing auctions for user's bids
auctions = Auction.objects.all() auctions = Auction.objects.all()
### Purchase History ### # ITEMS USER BID ON
# Get auctionstart < currentdate auctions
current_date = datetime.now() current_date = datetime.now()
won_itemids = [] won_itemids = []
won_auctions = [] won_auctions = []
for aucid in ids_of_auction: for aucid in ids_of_auction:
tempAuction = Auction.objects.get(pk=aucid.auctionid) tempAuction = Auction.objects.get(pk=aucid.auctionid)
auctionend = tempAuction.auctionend auctionend = tempAuction.auctionend
# finished auction, auctionend
if auctionend < current_date: if auctionend < current_date:
bids = AuctionBid.objects.filter(auctionid=aucid).order_by('-bidtime') bids = AuctionBid.objects.filter(auctionid=aucid).order_by('-bidtime')
highest_bidder = bids[0].userid highest_bidder = bids[0].userid
...@@ -326,7 +359,6 @@ def profile(request, pk): ...@@ -326,7 +359,6 @@ def profile(request, pk):
won_itemids.append(itemid) won_itemids.append(itemid)
# 🔥Current Store, pk here is the storeid
current_user = AuthUser.objects.get(pk=pk) current_user = AuthUser.objects.get(pk=pk)
form = CreateStoreForm(initial={'userid':pk}) form = CreateStoreForm(initial={'userid':pk})
...@@ -338,11 +370,9 @@ def profile(request, pk): ...@@ -338,11 +370,9 @@ def profile(request, pk):
if request.method == 'POST': if request.method == 'POST':
form = CreateStoreForm(request.POST, initial={'userid':pk}) form = CreateStoreForm(request.POST, initial={'userid':pk})
# putting a default value
if form.is_valid(): if form.is_valid():
form.save() form.save()
return redirect('profileid', pk=pk) return redirect('profileid', pk=pk)
# 🔥
context = { context = {
'displayname': current_user.username, 'displayname': current_user.username,
...@@ -362,6 +392,11 @@ def profile(request, pk): ...@@ -362,6 +392,11 @@ def profile(request, pk):
@login_required(login_url='login') @login_required(login_url='login')
def editStore(request, pk): def editStore(request, pk):
'''
Only a logged-in user has access to this view.
This view is a form wherin users [sellers] can edit their store details such as the name of the store and the store description. After successfully saving, the user is redirected to the store page.
AA user also has access to the other parts of the site from here through the navigation bar.
'''
store= Store.objects.get(storeid=pk) store= Store.objects.get(storeid=pk)
current_store = store.storeid current_store = store.storeid
...@@ -382,9 +417,15 @@ def editStore(request, pk): ...@@ -382,9 +417,15 @@ def editStore(request, pk):
@login_required(login_url='login') @login_required(login_url='login')
def editProfile(request, pk): def editProfile(request, pk):
'''
user= AuthUser.objects.get(id=pk) # authuser object Only a logged-in user has access to this view.
current_user = user.id # auth user id This view is a form wherin users [sellers] can edit their profile details.
After successfully saving, the user is redirected to their profile page.
A user also has access to the other parts of the site from here through the navigation bar.
'''
user= AuthUser.objects.get(id=pk)
current_user = user.id
form = editBoodleUserForm(instance=user) form = editBoodleUserForm(instance=user)
if request.method == 'POST': if request.method == 'POST':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment